nalgebra/src/base/rkyv_wrappers.rs
2023-08-14 17:23:59 +07:00

47 lines
1.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Wrapper that allows changing the generic type of a PhantomData<T>
//!
//! Copied from <https://github.com/rkyv/rkyv_contrib> (MIT-Apache2 licences) which isnt published yet.
use rkyv::{
with::{ArchiveWith, DeserializeWith, SerializeWith},
Fallible,
};
use std::marker::PhantomData;
/// 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]
unsafe fn resolve_with(
_: &PhantomData<OT>,
_: usize,
_: Self::Resolver,
_: *mut 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)
}
}