Skip to main content

Crate rkyv_impl

Crate rkyv_impl 

Source
Expand description

Crates.io Docs.rs

Implement methods for Foo and ArchivedFoo in a single impl block.

use rkyv::Archive;
use rkyv_impl::*;
use std::iter::Sum;

#[derive(Archive)]
struct Foo<T> {
    elements: Vec<T>
}

#[archive_impl(transform_bounds(T))]
impl<T> Foo<T> {
    #[archive_method(transform_bounds(T))]
    fn sum<S>(&self) -> S
    where
        T: Clone,
        S: Sum<T>
    {
        self.elements.iter().cloned().sum()
    }
}

// Notice that the trait bounds are transformed so that
// `T` is replaced with `T::Archived`.
fn call_generated_method<T, S>(foo: &ArchivedFoo<T>)
where
    T: Archive,
    T::Archived: Clone,
    S: Sum<T::Archived>
{
    let _ = foo.sum::<S>();
}

Attribute Macrosยง

archive_impl
Decorates an impl T (or impl FooTrait for T) block and generates an equivalent impl T::Archived.
archive_method
Supports the same arguments as archive_impl, but applies to methods on an impl block.