| 1 |
//! Wrapper that allows changing the generic type of a `PhantomData<NT>` |
| 2 |
//! |
| 3 |
//! Copied from <https://github.com/rkyv/rkyv_contrib> (MIT-Apache2 licences) which isn’t published yet. |
| 4 |
|
| 5 |
use rkyv::{ |
| 6 |
Fallible, |
| 7 |
with::{ArchiveWith, DeserializeWith, SerializeWith}, |
| 8 |
}; |
| 9 |
use std::marker::PhantomData; |
| 10 |
|
| 11 |
/// A wrapper that allows for changing the generic type of a `PhantomData<NT>`. |
| 12 |
pub struct CustomPhantom<NT: ?Sized> { |
| 13 |
_data: PhantomData<*const NT>, |
| 14 |
} |
| 15 |
|
| 16 |
impl<OT: ?Sized, NT: ?Sized> ArchiveWith<PhantomData<OT>> for CustomPhantom<NT> { |
| 17 |
type Archived = PhantomData<NT>; |
| 18 |
type Resolver = (); |
| 19 |
|
| 20 |
#[inline] |
| 21 |
unsafe fn resolve_with( |
| 22 |
_: &PhantomData<OT>, |
| 23 |
_: usize, |
| 24 |
_: Self::Resolver, |
| 25 |
_: *mut Self::Archived, |
| 26 |
) { |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
impl<OT: ?Sized, NT: ?Sized, S: Fallible + ?Sized> SerializeWith<PhantomData<OT>, S> |
| 31 |
for CustomPhantom<NT> |
| 32 |
{ |
| 33 |
#[inline] |
| 34 |
fn serialize_with(_: &PhantomData<OT>, _: &mut S) -> Result<Self::Resolver, S::Error> { |
| 35 |
Ok(()) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
impl<OT: ?Sized, NT: ?Sized, D: Fallible + ?Sized> |
| 40 |
DeserializeWith<PhantomData<NT>, PhantomData<OT>, D> for CustomPhantom<NT> |
| 41 |
{ |
| 42 |
#[inline] |
| 43 |
fn deserialize_with(_: &PhantomData<NT>, _: &mut D) -> Result<PhantomData<OT>, D::Error> { |
| 44 |
Ok(PhantomData) |
| 45 |
} |
| 46 |
} |