|
| 1 | +use std::any::Any; |
| 2 | + |
| 3 | +use rkyv::{ |
| 4 | + Archive, Deserialize, Place, Serialize, |
| 5 | + de::Pooling, |
| 6 | + rancor::Fallible, |
| 7 | + ser::Sharing, |
| 8 | + with::{ArchiveWith, DeserializeWith, SerializeWith}, |
| 9 | +}; |
| 10 | + |
| 11 | +use crate::{DeserializeError, SerializeError, cacheable, context::ContextGuard}; |
| 12 | + |
| 13 | +/// A trait for writing custom serialization and deserialization. |
| 14 | +/// |
| 15 | +/// `#[cacheable(with=Custom)]` will use this trait. |
| 16 | +pub trait CustomConverter { |
| 17 | + type Target: Archive; |
| 18 | + fn serialize(&self, ctx: &dyn Any) -> Result<Self::Target, SerializeError>; |
| 19 | + fn deserialize(data: Self::Target, ctx: &dyn Any) -> Result<Self, DeserializeError> |
| 20 | + where |
| 21 | + Self: Sized; |
| 22 | +} |
| 23 | + |
| 24 | +/// A wrapper that uses CustomConverter for serialization. |
| 25 | +pub struct Custom; |
| 26 | + |
| 27 | +/// A simple structure to save the generated `CustomConverter::Target`, |
| 28 | +/// which can avoid some deserialization conflicts. |
| 29 | +#[cacheable(crate=crate)] |
| 30 | +pub struct DataBox<T: Archive>(T); |
| 31 | + |
| 32 | +pub struct CustomResolver<A: Archive> { |
| 33 | + resolver: DataBoxResolver<A>, |
| 34 | + value: DataBox<A>, |
| 35 | +} |
| 36 | + |
| 37 | +impl<T> ArchiveWith<T> for Custom |
| 38 | +where |
| 39 | + T: CustomConverter, |
| 40 | + T::Target: Archive, |
| 41 | +{ |
| 42 | + type Archived = ArchivedDataBox<T::Target>; |
| 43 | + type Resolver = CustomResolver<T::Target>; |
| 44 | + |
| 45 | + #[inline] |
| 46 | + fn resolve_with(_field: &T, resolver: Self::Resolver, out: Place<Self::Archived>) { |
| 47 | + let CustomResolver { resolver, value } = resolver; |
| 48 | + value.resolve(resolver, out) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl<T, S> SerializeWith<T, S> for Custom |
| 53 | +where |
| 54 | + T: CustomConverter, |
| 55 | + T::Target: Archive + Serialize<S>, |
| 56 | + S: Fallible<Error = SerializeError> + Sharing + ?Sized, |
| 57 | +{ |
| 58 | + #[inline] |
| 59 | + fn serialize_with(field: &T, serializer: &mut S) -> Result<Self::Resolver, SerializeError> { |
| 60 | + let ctx = ContextGuard::sharing_context(serializer)?; |
| 61 | + let value = DataBox(T::serialize(field, ctx)?); |
| 62 | + Ok(CustomResolver { |
| 63 | + resolver: value.serialize(serializer)?, |
| 64 | + value, |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl<T, D> DeserializeWith<ArchivedDataBox<T::Target>, T, D> for Custom |
| 70 | +where |
| 71 | + T: CustomConverter, |
| 72 | + T::Target: Archive, |
| 73 | + ArchivedDataBox<T::Target>: Deserialize<DataBox<T::Target>, D>, |
| 74 | + D: Fallible<Error = DeserializeError> + Pooling + ?Sized, |
| 75 | +{ |
| 76 | + #[inline] |
| 77 | + fn deserialize_with( |
| 78 | + field: &ArchivedDataBox<T::Target>, |
| 79 | + de: &mut D, |
| 80 | + ) -> Result<T, DeserializeError> { |
| 81 | + let value = field.deserialize(de)?; |
| 82 | + let ctx = ContextGuard::pooling_context(de)?; |
| 83 | + T::deserialize(value.0, ctx) |
| 84 | + } |
| 85 | +} |
0 commit comments