-
-
Notifications
You must be signed in to change notification settings - Fork 561
Expand file tree
/
Copy pathrkyv_wrappers.rs
More file actions
51 lines (43 loc) · 1.45 KB
/
Copy pathrkyv_wrappers.rs
File metadata and controls
51 lines (43 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! Wrapper that allows changing the generic type of a PhantomData<T>
//!
//! Copied from <https://github.com/rkyv/rkyv_contrib> (MIT-Apache2 licences) which isn’t published yet.
use rkyv::{
rancor::Fallible,
with::{ArchiveWith, DeserializeWith, SerializeWith},
Place,
};
use std::marker::PhantomData;
#[derive(Debug)]
/// A wrapper that allows for changing the generic type of a `PhantomData<T>`.
pub struct CustomPhantom<NT: ?Sized> {
_data: PhantomData<*const NT>,
}
impl<OT: ?Sized, NT: ?Sized> ArchiveWith<PhantomData<OT>> for CustomPhantom<NT> {
type Archived = PhantomData<NT>;
type Resolver = ();
#[inline]
fn resolve_with(_: &PhantomData<OT>, _: Self::Resolver, _: Place<Self::Archived>) {}
}
impl<OT: ?Sized, NT: ?Sized, S: Fallible + ?Sized> SerializeWith<PhantomData<OT>, S>
for CustomPhantom<NT>
{
#[inline]
fn serialize_with(_: &PhantomData<OT>, _: &mut S) -> Result<Self::Resolver, S::Error> {
Ok(())
}
}
impl<OT: ?Sized, NT: ?Sized, D: Fallible + ?Sized>
DeserializeWith<PhantomData<NT>, PhantomData<OT>, D> for CustomPhantom<NT>
{
#[inline]
fn deserialize_with(_: &PhantomData<NT>, _: &mut D) -> Result<PhantomData<OT>, D::Error> {
Ok(PhantomData)
}
}
// TODO: implement PartialEq with generics.
#[cfg(feature = "rkyv-serialize")]
impl<NT: ?Sized> PartialEq<CustomPhantom<NT>> for CustomPhantom<NT> {
fn eq(&self, _other: &CustomPhantom<NT>) -> bool {
true
}
}