Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rkyv #18

Merged
merged 1 commit into from
Sep 14, 2024
Merged
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
113 changes: 113 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ simdutf8 = { version = "0.1", default-features = false, features = [
redis = { version = "0.26", optional = true, default-features = false }
itoa = { version = "1", optional = true }
ryu = { version = "1", optional = true }
rkyv = { version = "0.8", optional = true, default-features = false }

[features]
default = ["std"]
std = ["bytes/std", "simdutf8/std", "serde?/std"]
std = ["bytes/std", "simdutf8/std", "serde?/std", "rkyv?/std"]
serde = ["serde/alloc"]
serde-unsafe = ["serde"]
redis = ["std", "dep:redis", "itoa", "ryu"]
redis-unsafe = ["redis"]
rkyv = ["rkyv/alloc"]


[dev-dependencies]
static_assertions = { version = "1" }
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,3 +798,6 @@ mod redis;

#[cfg(feature = "serde")]
mod serde;

#[cfg(feature = "rkyv")]
mod rkyv;
48 changes: 48 additions & 0 deletions src/rkyv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#[cfg(not(feature = "std"))]
use alloc::string::String;

use rancor::{Fallible, Source};
use rkyv::{
ser::{Allocator, Writer},
string::{ArchivedString, StringResolver},
*,
};

use super::FastStr;

impl Archive for FastStr {
type Archived = ArchivedString;
type Resolver = StringResolver;

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

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

impl<D: Fallible + ?Sized> Deserialize<FastStr, D> for ArchivedString {
fn deserialize(&self, _deserializer: &mut D) -> Result<FastStr, D::Error> {
Ok(FastStr::new(self.as_str()))
}
}

impl PartialEq<FastStr> for ArchivedString {
fn eq(&self, other: &FastStr) -> bool {
other.as_str() == self.as_str()
}
}

impl PartialOrd<FastStr> for ArchivedString {
fn partial_cmp(&self, other: &FastStr) -> Option<::core::cmp::Ordering> {
Some(self.as_str().cmp(other.as_str()))
}
}
Loading