-
-
Save lcnr/75d99bd2900f9dd2eaa558b3c68c42e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::ptr::addr_of_mut; | |
use ::rkyv::{Archive, Archived, Deserialize, Fallible, Resolver}; | |
struct Foo<T>(T); | |
impl<D: Fallible + ?Sized, T> Deserialize<Foo<T>, D> for Archived<Foo<T>> | |
where | |
T: Archive, | |
Archived<T>: Deserialize<T, D>, | |
{ | |
#[inline] | |
fn deserialize(&self, deserializer: &mut D) -> Result<Foo<T>, D::Error> { | |
Ok(Foo(Deserialize::<T, D>::deserialize( | |
&self.0, | |
deserializer, | |
)?)) | |
} | |
} | |
///An archived [`Foo`] | |
struct ArchivedFoo<T>( | |
///The archived counterpart of [`Foo::0`] | |
Archived<T>, | |
) | |
where | |
T: Archive; | |
///The resolver for an archived [`Foo`] | |
struct FooResolver<T>(Resolver<T>) | |
where | |
T: Archive; | |
impl<T> Archive for Foo<T> | |
where | |
T: Archive, | |
{ | |
type Archived = ArchivedFoo<T>; | |
type Resolver = FooResolver<T>; | |
#[inline] | |
unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { | |
let (fp, fo) = { | |
#[allow(unused_unsafe)] | |
unsafe { | |
let fo = addr_of_mut!((*out).0); | |
(fo.cast::<u8>().offset_from(out.cast::<u8>()) as usize, fo) | |
} | |
}; | |
Archive::resolve(&self.0, pos + fp, resolver.0, fo); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment