Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 2 additions & 33 deletions crates/hstr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use crate::tagged_value::TaggedValue;

mod dynamic;
mod global_store;
#[cfg(feature = "rkyv")]
mod rkyv;
mod tagged_value;
#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -412,39 +414,6 @@ impl PartialEq<Atom> for str {
}
}

/// NOT A PUBLIC API
#[cfg(feature = "rkyv")]
impl rkyv::Archive for Atom {
type Archived = rkyv::string::ArchivedString;
type Resolver = rkyv::string::StringResolver;

#[allow(clippy::unit_arg)]
unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) {
rkyv::string::ArchivedString::resolve_from_str(self, pos, resolver, out)
}
}

/// NOT A PUBLIC API
#[cfg(feature = "rkyv")]
impl<S: rkyv::ser::Serializer + ?Sized> rkyv::Serialize<S> for Atom {
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
String::serialize(&self.to_string(), serializer)
}
}

/// NOT A PUBLIC API
#[cfg(feature = "rkyv")]
impl<D> rkyv::Deserialize<Atom, D> for rkyv::string::ArchivedString
where
D: ?Sized + rkyv::Fallible,
{
fn deserialize(&self, deserializer: &mut D) -> Result<Atom, <D as rkyv::Fallible>::Error> {
let s: String = self.deserialize(deserializer)?;

Ok(Atom::new(s))
}
}

#[cfg(test)]
mod macro_tests {

Expand Down
37 changes: 37 additions & 0 deletions crates/hstr/src/rkyv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use rkyv::{
de::Pooling,
rancor::{Fallible, Source},
ser::{Sharing, Writer},
Deserialize, Portable,
};

use crate::Atom;

#[derive(Debug, Portable)]
#[repr(transparent)]
struct ArchivedAtom {}

struct AtomResolver {}

impl rkyv::Archive for Atom {
type Archived = ArchivedAtom;
type Resolver = AtomResolver;

fn resolve(&self, resolver: Self::Resolver, out: rkyv::Place<Self::Archived>) {}
}

impl<S> rkyv::Serialize<S> for Atom
where
S: Fallible + Writer + Sharing + ?Sized,
S::Error: Source,
{
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {}
}

impl<D> Deserialize<Atom, D> for ArchivedAtom
where
D: Fallible + Pooling + ?Sized,
D::Error: Source,
{
fn deserialize(&self, deserializer: &mut D) -> Result<Atom, D::Error> {}
}
2 changes: 1 addition & 1 deletion crates/swc_atoms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ bench = false

[features]
__rkyv = []
rkyv-impl = ["__rkyv", "rkyv", "bytecheck", "rancor"]
rkyv-impl = ["__rkyv", "rkyv", "bytecheck", "rancor", "hstr/rkyv"]
shrink-to-fit = ["dep:shrink-to-fit"]

[dependencies]
Expand Down
46 changes: 9 additions & 37 deletions crates/swc_atoms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ mod fast;
///
/// See [tendril] for more details.
#[derive(Clone, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))]
#[cfg_attr(
feature = "rkyv-impl",
derive(
rkyv::Archive,
rkyv::Serialize,
rkyv::Deserialize,
bytecheck::CheckBytes
)
)]
#[cfg_attr(feature = "rkyv-impl", repr(C))]
pub struct Atom(hstr::Atom);

Expand Down Expand Up @@ -201,42 +209,6 @@ impl PartialEq<Atom> for str {
}
}

/// NOT A PUBLIC API
#[cfg(feature = "rkyv-impl")]
impl rkyv::Archive for Atom {
type Archived = rkyv::string::ArchivedString;
type Resolver = rkyv::string::StringResolver;

#[allow(clippy::unit_arg)]
fn resolve(&self, resolver: Self::Resolver, out: rkyv::Place<Self::Archived>) {
rkyv::string::ArchivedString::resolve_from_str(self, resolver, out)
}
}

/// NOT A PUBLIC API
#[cfg(feature = "rkyv-impl")]
impl<S: rancor::Fallible + rkyv::ser::Writer + ?Sized> rkyv::Serialize<S> for Atom
where
<S as rancor::Fallible>::Error: rancor::Source,
{
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
String::serialize(&self.to_string(), serializer)
}
}

/// NOT A PUBLIC API
#[cfg(feature = "rkyv-impl")]
impl<D> rkyv::Deserialize<Atom, D> for rkyv::string::ArchivedString
where
D: ?Sized + rancor::Fallible,
{
fn deserialize(&self, deserializer: &mut D) -> Result<Atom, <D as rancor::Fallible>::Error> {
let s: String = self.deserialize(deserializer)?;

Ok(Atom::new(s))
}
}

#[doc(hidden)]
pub type CahcedAtom = Lazy<Atom>;

Expand Down
Loading