use rkyv::{ ser::{ serializers::{ AlignedSerializer, AllocScratch, CompositeSerializer, FallbackScratch, HeapScratch, }, Serializer, }, AlignedVec, Archive, Infallible, Serialize, }; use rkyv_impl::*; use std::iter::Sum; #[derive(Archive, Serialize)] struct Foo { elements1: Vec, elements2: Vec, } #[archive_impl(transform_bounds(R, T))] impl Foo { #[archive_method(transform_bounds(R, T))] fn sum(&self) -> S where R: Clone, T: Clone, S: Sum, S: Sum, S: std::ops::Add, { self.elements1.iter().cloned().sum::() + self.elements2.iter().cloned().sum::() } } fn main() { let foo = Foo { elements1: vec![1, 2, 3], elements2: vec![4, 5, 6], }; // Serialize. let buf = AlignedVec::new(); let scratch = FallbackScratch::new(HeapScratch::<0>::new(), AllocScratch::new()); let mut serializer = CompositeSerializer::new(AlignedSerializer::new(buf), scratch, Infallible); serializer.serialize_value(&foo).unwrap(); let (serializer, _, _) = serializer.into_components(); let buf = serializer.into_inner(); let archived_foo = unsafe { rkyv::archived_root::>(&buf) }; assert_eq!(foo.sum::(), 21); assert_eq!(archived_foo.sum::(), 21); }