pub trait ArchiveWith<F: ?Sized> {
type Archived: Portable;
type Resolver;
// Required method
fn resolve_with(
field: &F,
resolver: Self::Resolver,
out: Place<Self::Archived>,
);
}
Expand description
A variant of Archive
that works 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 and enum fields. When a field is serialized, it may use the implementations for the wrapper type and the given field instead of the implementation for the type itself.
Only a single implementation of Archive
may be written
for each type, but multiple implementations of ArchiveWith can be written
for the same type because it is parametric over the wrapper type. This is
used with the #[rkyv(with = ..)]
macro attribute to provide a more
flexible interface for serialization.
§Example
use rkyv::{
access_unchecked, deserialize,
rancor::{Error, Fallible, Infallible, ResultExt as _},
to_bytes,
with::{ArchiveWith, DeserializeWith, SerializeWith},
Archive, Archived, Deserialize, Place, Resolver, Serialize,
};
struct Incremented;
impl ArchiveWith<i32> for Incremented {
type Archived = Archived<i32>;
type Resolver = Resolver<i32>;
fn resolve_with(field: &i32, _: (), out: Place<Self::Archived>) {
let incremented = field + 1;
incremented.resolve((), out);
}
}
impl<S> SerializeWith<i32, S> for Incremented
where
S: Fallible + ?Sized,
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> DeserializeWith<Archived<i32>, i32, D> for Incremented
where
D: Fallible + ?Sized,
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 {
#[rkyv(with = Incremented)]
a: i32,
// Another i32 field, but not incremented this time
b: i32,
}
let value = Example { a: 4, b: 9 };
let buf = to_bytes::<Error>(&value).unwrap();
let archived =
unsafe { access_unchecked::<Archived<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 = deserialize::<Example, Infallible>(archived).always_ok();
// 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§
Required Methods§
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<OsString> for AsString
Available on crate feature std
only.
impl ArchiveWith<OsString> for AsString
std
only.type Archived = ArchivedString
type Resolver = StringResolver
Source§impl ArchiveWith<PathBuf> for AsString
Available on crate feature std
only.
impl ArchiveWith<PathBuf> for AsString
std
only.type Archived = ArchivedString
type Resolver = StringResolver
Source§impl ArchiveWith<SystemTime> for AsUnixTime
Available on crate feature std
only.
impl ArchiveWith<SystemTime> for AsUnixTime
std
only.Source§impl<'a> ArchiveWith<Cow<'a, str>> for AsOwned
Available on crate feature alloc
only.
impl<'a> ArchiveWith<Cow<'a, str>> for AsOwned
alloc
only.type Archived = ArchivedString
type Resolver = StringResolver
Source§impl<'a> ArchiveWith<Cow<'a, CStr>> for AsOwned
Available on crate feature std
only.
impl<'a> ArchiveWith<Cow<'a, CStr>> for AsOwned
std
only.type Archived = ArchivedCString
type Resolver = CStringResolver
Source§impl<'a, F: Archive + Clone> ArchiveWith<Cow<'a, F>> for AsOwned
Available on crate feature alloc
only.
impl<'a, F: Archive + Clone> ArchiveWith<Cow<'a, F>> for AsOwned
alloc
only.Source§impl<'a, T: Archive + Clone> ArchiveWith<Cow<'a, [T]>> for AsOwned
Available on crate feature alloc
only.
impl<'a, T: Archive + Clone> ArchiveWith<Cow<'a, [T]>> for AsOwned
alloc
only.type Archived = ArchivedVec<<T as Archive>::Archived>
type Resolver = VecResolver
Source§impl<A, B, K, V> ArchiveWith<BTreeMap<K, V>> for MapKV<A, B>where
A: ArchiveWith<K>,
B: ArchiveWith<V>,
Available on crate feature alloc
only.
impl<A, B, K, V> ArchiveWith<BTreeMap<K, V>> for MapKV<A, B>where
A: ArchiveWith<K>,
B: ArchiveWith<V>,
alloc
only.type Archived = ArchivedBTreeMap<<A as ArchiveWith<K>>::Archived, <B as ArchiveWith<V>>::Archived>
type Resolver = BTreeMapResolver
Source§impl<A, B, K, V, H> ArchiveWith<HashMap<K, V, H>> for MapKV<A, B>
Available on crate feature std
only.
impl<A, B, K, V, H> ArchiveWith<HashMap<K, V, H>> for MapKV<A, B>
std
only.type Archived = ArchivedHashMap<<A as ArchiveWith<K>>::Archived, <B as ArchiveWith<V>>::Archived>
type Resolver = HashMapResolver
Source§impl<A, O> ArchiveWith<Option<O>> for Map<A>where
A: ArchiveWith<O>,
impl<A, O> ArchiveWith<Option<O>> for Map<A>where
A: ArchiveWith<O>,
type Archived = ArchivedOption<<A as ArchiveWith<O>>::Archived>
type Resolver = Option<<A as ArchiveWith<O>>::Resolver>
Source§impl<A, O> ArchiveWith<Vec<O>> for Map<A>where
A: ArchiveWith<O>,
Available on crate feature alloc
only.
impl<A, O> ArchiveWith<Vec<O>> for Map<A>where
A: ArchiveWith<O>,
alloc
only.type Archived = ArchivedVec<<A as ArchiveWith<O>>::Archived>
type Resolver = VecResolver
Source§impl<F: Archive> ArchiveWith<UnsafeCell<F>> for Unsafe
impl<F: Archive> ArchiveWith<UnsafeCell<F>> for Unsafe
Source§impl<F: Archive> ArchiveWith<F> for Identity
impl<F: Archive> ArchiveWith<F> for Identity
Source§impl<F: ArchiveUnsized + ?Sized> ArchiveWith<&F> for InlineAsBox
impl<F: ArchiveUnsized + ?Sized> ArchiveWith<&F> for InlineAsBox
type Archived = ArchivedBox<<F as ArchiveUnsized>::Archived>
type Resolver = BoxResolver
Source§impl<F: ArchiveUnsized + ?Sized> ArchiveWith<F> for AsBox
impl<F: ArchiveUnsized + ?Sized> ArchiveWith<F> for AsBox
type Archived = ArchivedBox<<F as ArchiveUnsized>::Archived>
type Resolver = BoxResolver
Source§impl<K: Archive, V: Archive> ArchiveWith<BTreeMap<K, V>> for AsVec
Available on crate feature alloc
only.
impl<K: Archive, V: Archive> ArchiveWith<BTreeMap<K, V>> for AsVec
alloc
only.Source§impl<K: Archive, V: Archive, H> ArchiveWith<HashMap<K, V, H>> for AsVec
Available on crate feature std
only.
impl<K: Archive, V: Archive, H> ArchiveWith<HashMap<K, V, H>> for AsVec
std
only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicBool> for AtomicLoad<SO>
Available on target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
only.
impl<SO: LoadOrdering> ArchiveWith<AtomicBool> for AtomicLoad<SO>
target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicI8> for AtomicLoad<SO>
Available on target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
only.
impl<SO: LoadOrdering> ArchiveWith<AtomicI8> for AtomicLoad<SO>
target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicI16> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicI16> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicI32> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicI32> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicI64> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicI64> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicIsize> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicIsize> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicU8> for AtomicLoad<SO>
Available on target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
only.
impl<SO: LoadOrdering> ArchiveWith<AtomicU8> for AtomicLoad<SO>
target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicU16> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicU16> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicU32> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicU32> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicU64> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicU64> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicUsize> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicUsize> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicI16_be> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicI16_be> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicI16_le> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicI16_le> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicI32_be> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicI32_be> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicI32_le> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicI32_le> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicI64_be> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicI64_be> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicI64_le> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicI64_le> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicU16_be> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicU16_be> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicU16_le> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicU16_le> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicU32_be> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicU32_be> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicU32_le> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicU32_le> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicU64_be> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicU64_be> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<SO: LoadOrdering> ArchiveWith<AtomicU64_le> for AtomicLoad<SO>
Available on (target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.
impl<SO: LoadOrdering> ArchiveWith<AtomicU64_le> for AtomicLoad<SO>
target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) and (target_has_atomic="8"
or target_has_atomic="16"
or target_has_atomic="32"
or target_has_atomic="64"
) only.Source§impl<T> ArchiveWith<Option<Box<T>>> for Niche
Available on crate feature alloc
only.
impl<T> ArchiveWith<Option<Box<T>>> for Niche
alloc
only.type Archived = ArchivedOptionBox<<T as ArchiveUnsized>::Archived>
type Resolver = OptionBoxResolver
Source§impl<T> ArchiveWith<Option<T>> for DefaultNiche
impl<T> ArchiveWith<Option<T>> for DefaultNiche
Source§impl<T, N> ArchiveWith<Option<T>> for NicheInto<N>
impl<T, N> ArchiveWith<Option<T>> for NicheInto<N>
Source§impl<T, W, N> ArchiveWith<Option<T>> for MapNiche<W, N>
impl<T, W, N> ArchiveWith<Option<T>> for MapNiche<W, N>
type Archived = NichedOption<<W as ArchiveWith<T>>::Archived, N>
type Resolver = Option<<W as ArchiveWith<T>>::Resolver>
Source§impl<T: Archive> ArchiveWith<BTreeSet<T>> for AsVec
Available on crate feature alloc
only.
impl<T: Archive> ArchiveWith<BTreeSet<T>> for AsVec
alloc
only.type Archived = ArchivedVec<<T as Archive>::Archived>
type Resolver = VecResolver
Source§impl<T: Archive, H> ArchiveWith<HashSet<T, H>> for AsVec
Available on crate feature std
only.
impl<T: Archive, H> ArchiveWith<HashSet<T, H>> for AsVec
std
only.type Archived = ArchivedVec<<T as Archive>::Archived>
type Resolver = VecResolver
Source§impl<const A: usize> ArchiveWith<AlignedVec<A>> for AsVec
Available on crate feature alloc
only.
impl<const A: usize> ArchiveWith<AlignedVec<A>> for AsVec
alloc
only.