Skip to content

Commit

Permalink
feat: add rkyv serialize, deserialize, archive
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Griffon <[email protected]>
  • Loading branch information
Miaxos committed Dec 17, 2021
1 parent 5ed7bda commit 150c637
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ once_cell = { version = "1.4", optional = true }
tinyset = { version = "0.4.2", optional = true }
hashbrown = { version = "0.9" }
serde = { version = "1.0", optional = true }
rkyv = { version = "0.7", optional = true }

arc-interner = { version = "0.5", optional = true }

Expand Down
34 changes: 33 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ use std::sync::atomic::Ordering;
mod container;
use container::{TypeHolder, TypeHolderSend};

use std::any::TypeId;
use std::any::Any;
use std::any::TypeId;
use std::borrow::Borrow;
use std::convert::AsRef;
use std::fmt::{Debug, Display, Pointer};
use std::hash::{Hash, Hasher};
use std::ops::Deref;

#[cfg(feature = "rkyv")]
use rkyv::{Archive as RkyvArchive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "tinyset")]
Expand Down Expand Up @@ -778,6 +780,36 @@ macro_rules! create_impls {
}
}

#[cfg(feature = "rkyv")]
impl<T: $( $traits +)* rkyv::Archive> rkyv::Archive for $Intern<T> {
type Archived = rkyv::Archived<T>;
type Resolver = rkyv::Resolver<T>;

#[allow(clippy::unit_arg)]
#[inline]
unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) {
self.as_ref().resolve(pos, resolver, out)
}
}

#[cfg(feature = "rkyv")]
impl<T: $( $traits +)* RkyvSerialize<S>, S: rkyv::Fallible + ?Sized> RkyvSerialize<S> for $Intern<T>
{
#[inline]
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
RkyvSerialize::serialize(self.as_ref(), serializer)
}
}

#[cfg(feature = "rkyv")]
impl<T: Eq + Hash + Sync + Send + $( $traits +)* rkyv::Archive<Archived = impl RkyvDeserialize<T, D>> + 'static, D: rkyv::Fallible + ?Sized> RkyvDeserialize<$Intern<T>, D> for rkyv::Archived<$Intern<T>>
{
#[inline]
fn deserialize(&self, deserializer: &mut D) -> Result<$Intern<T>, D::Error> {
RkyvDeserialize::<T, D>::deserialize(self, deserializer).map($Intern::new)
}
}

#[cfg(test)]
mod $testname {
use super::$Intern;
Expand Down

0 comments on commit 150c637

Please sign in to comment.