Trait rkyv::with::ArchiveWith

source ·
pub trait ArchiveWith<F: ?Sized> {
    type Archived;
    type Resolver;

    // Required method
    unsafe fn resolve_with(
        field: &F,
        pos: usize,
        resolver: Self::Resolver,
        out: *mut Self::Archived
    );
}
Expand description

A variant of Archive that works with With wrappers.

Creating a wrapper allows users to customize how fields are archived easily without changing the unarchived type.

This trait allows wrapper types to transparently change the archive behaviors for struct fields. When a field is serialized, its reference may be converted to a With reference, and that reference may be serialized instead. With references look for implementations of ArchiveWith to determine how a wrapped field should be treated.

§Example

use rkyv::{
    archived_root,
    ser::{
        serializers::AllocSerializer,
        Serializer,
    },
    with::{
        ArchiveWith,
        DeserializeWith,
        SerializeWith,
    },
    Archive,
    Archived,
    Deserialize,
    Fallible,
    Infallible,
    Resolver,
    Serialize,
};

struct Incremented;

impl ArchiveWith<i32> for Incremented {
    type Archived = Archived<i32>;
    type Resolver = Resolver<i32>;

    unsafe fn resolve_with(field: &i32, pos: usize, _: (), out: *mut Self::Archived) {
        let incremented = field + 1;
        incremented.resolve(pos, (), out);
    }
}

impl<S: Fallible + ?Sized> SerializeWith<i32, S> for Incremented
where
    i32: Serialize<S>,
{
    fn serialize_with(field: &i32, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
        let incremented = field + 1;
        incremented.serialize(serializer)
    }
}

impl<D: Fallible + ?Sized> DeserializeWith<Archived<i32>, i32, D> for Incremented
where
    Archived<i32>: Deserialize<i32, D>,
{
    fn deserialize_with(field: &Archived<i32>, deserializer: &mut D) -> Result<i32, D::Error> {
        Ok(field.deserialize(deserializer)? - 1)
    }
}

#[derive(Archive, Deserialize, Serialize)]
struct Example {
    #[with(Incremented)]
    a: i32,
    // Another i32 field, but not incremented this time
    b: i32,
}

let value = Example {
    a: 4,
    b: 9,
};

let mut serializer = AllocSerializer::<4096>::default();
serializer.serialize_value(&value).unwrap();
let buf = serializer.into_serializer().into_inner();

let archived = unsafe { archived_root::<Example>(buf.as_ref()) };
// The wrapped field has been incremented
assert_eq!(archived.a, 5);
// ... and the unwrapped field has not
assert_eq!(archived.b, 9);

let deserialized: Example = archived.deserialize(&mut Infallible).unwrap();
// The wrapped field is back to normal
assert_eq!(deserialized.a, 4);
// ... and the unwrapped field is unchanged
assert_eq!(deserialized.b, 9);

Required Associated Types§

source

type Archived

The archived type of a With<F, Self>.

source

type Resolver

The resolver of a With<F, Self>.

Required Methods§

source

unsafe fn resolve_with( field: &F, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived )

Resolves the archived type using a reference to the field type F.

§Safety
  • pos must be the position of out within the archive
  • resolver must be the result of serializing field

Object Safety§

This trait is not object safe.

Implementors§

source§

impl ArchiveWith<Option<NonZero<i8>>> for Niche

source§

impl ArchiveWith<Option<NonZero<i16>>> for Niche

source§

impl ArchiveWith<Option<NonZero<i32>>> for Niche

source§

impl ArchiveWith<Option<NonZero<i64>>> for Niche

source§

impl ArchiveWith<Option<NonZero<i128>>> for Niche

source§

impl ArchiveWith<Option<NonZero<isize>>> for Niche

source§

impl ArchiveWith<Option<NonZero<u8>>> for Niche

source§

impl ArchiveWith<Option<NonZero<u16>>> for Niche

source§

impl ArchiveWith<Option<NonZero<u32>>> for Niche

source§

impl ArchiveWith<Option<NonZero<u64>>> for Niche

source§

impl ArchiveWith<Option<NonZero<u128>>> for Niche

source§

impl ArchiveWith<Option<NonZero<usize>>> for Niche

source§

impl ArchiveWith<AtomicBool> for Atomic

source§

impl ArchiveWith<AtomicI8> for Atomic

source§

impl ArchiveWith<AtomicI16> for Atomic

source§

impl ArchiveWith<AtomicI32> for Atomic

source§

impl ArchiveWith<AtomicI64> for Atomic

source§

impl ArchiveWith<AtomicIsize> for Atomic

source§

impl ArchiveWith<AtomicU8> for Atomic

source§

impl ArchiveWith<AtomicU16> for Atomic

source§

impl ArchiveWith<AtomicU32> for Atomic

source§

impl ArchiveWith<AtomicU64> for Atomic

source§

impl ArchiveWith<AtomicUsize> for Atomic

source§

impl ArchiveWith<OsString> for AsString

source§

impl ArchiveWith<PathBuf> for AsString

source§

impl ArchiveWith<SystemTime> for UnixTimestamp

source§

impl<'a> ArchiveWith<Cow<'a, str>> for AsOwned

source§

impl<'a> ArchiveWith<Cow<'a, CStr>> for AsOwned

source§

impl<'a, F: Archive + Clone> ArchiveWith<Cow<'a, F>> for AsOwned

§

type Archived = <F as Archive>::Archived

§

type Resolver = <F as Archive>::Resolver

source§

impl<'a, T: Archive + Clone> ArchiveWith<Cow<'a, [T]>> for AsOwned

source§

impl<'a, T: Archive> ArchiveWith<With<&'a [T], RefAsBox>> for CopyOptimize

source§

impl<A, O> ArchiveWith<Option<O>> for Map<A>
where A: ArchiveWith<O>,

source§

impl<A, O> ArchiveWith<Vec<O>> for Map<A>
where A: ArchiveWith<O>,

source§

impl<F> ArchiveWith<F> for Skip

§

type Archived = ()

§

type Resolver = ()

source§

impl<F: Archive> ArchiveWith<&F> for Inline

§

type Archived = <F as Archive>::Archived

§

type Resolver = <F as Archive>::Resolver

source§

impl<F: Archive> ArchiveWith<Cell<F>> for Unsafe

§

type Archived = Cell<<F as Archive>::Archived>

§

type Resolver = <F as Archive>::Resolver

source§

impl<F: Archive> ArchiveWith<UnsafeCell<F>> for Unsafe

source§

impl<F: Archive> ArchiveWith<Mutex<F>> for Lock

source§

impl<F: Archive> ArchiveWith<RwLock<F>> for Lock

source§

impl<F: ArchiveUnsized + ?Sized> ArchiveWith<&F> for RefAsBox

source§

impl<F: ArchiveUnsized + ?Sized> ArchiveWith<F> for AsBox

source§

impl<K: Archive, V: Archive> ArchiveWith<BTreeMap<K, V>> for AsVec

source§

impl<K: Archive, V: Archive> ArchiveWith<HashMap<K, V>> for AsVec

source§

impl<T: Archive> ArchiveWith<Box<[T]>> for CopyOptimize

source§

impl<T: Archive> ArchiveWith<BTreeSet<T>> for AsVec

source§

impl<T: Archive> ArchiveWith<Vec<T>> for CopyOptimize

source§

impl<T: Archive> ArchiveWith<Vec<T>> for Raw

source§

impl<T: Archive> ArchiveWith<HashSet<T>> for AsVec

source§

impl<T: ArchiveUnsized + ?Sized> ArchiveWith<Option<Box<T>>> for Niche