rkyv::with

Trait 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

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so 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

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

Source§

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

Source§

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

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