|
| 1 | +use std::cmp::PartialEq; |
| 2 | +use std::ops::Deref; |
| 3 | +// |
| 4 | +// use rkyv::rancor::{Fallible, Source}; |
| 5 | +// use rkyv::ser::{Allocator, Writer}; |
| 6 | +// use rkyv::string::{ArchivedString, StringResolver}; |
| 7 | +// use rkyv::{Archive, Place}; |
| 8 | + |
| 9 | +/// An optimized small string type for short identifiers, like package names. |
| 10 | +/// |
| 11 | +/// Represented as an [`arcstr::ArcStr`] internally. |
| 12 | +#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 13 | +pub(crate) struct SmallString(arcstr::ArcStr); |
| 14 | + |
| 15 | +impl From<&str> for SmallString { |
| 16 | + fn from(s: &str) -> Self { |
| 17 | + Self(s.into()) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +impl From<String> for SmallString { |
| 22 | + fn from(s: String) -> Self { |
| 23 | + Self(s.into()) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl AsRef<str> for SmallString { |
| 28 | + fn as_ref(&self) -> &str { |
| 29 | + &self.0 |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl Deref for SmallString { |
| 34 | + type Target = str; |
| 35 | + |
| 36 | + fn deref(&self) -> &Self::Target { |
| 37 | + &self.0 |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// A [`serde::Serialize`] implementation for [`SmallString`]. |
| 42 | +impl serde::Serialize for SmallString { |
| 43 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 44 | + where |
| 45 | + S: serde::Serializer, |
| 46 | + { |
| 47 | + self.0.serialize(serializer) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/// An [`rkyv`] implementation for [`SmallString`]. |
| 52 | +impl rkyv::Archive for SmallString { |
| 53 | + type Archived = rkyv::string::ArchivedString; |
| 54 | + type Resolver = rkyv::string::StringResolver; |
| 55 | + |
| 56 | + #[inline] |
| 57 | + fn resolve(&self, resolver: Self::Resolver, out: rkyv::Place<Self::Archived>) { |
| 58 | + rkyv::string::ArchivedString::resolve_from_str(&self.0, resolver, out); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl<S> rkyv::Serialize<S> for SmallString |
| 63 | +where |
| 64 | + S: rkyv::rancor::Fallible + rkyv::ser::Allocator + rkyv::ser::Writer + ?Sized, |
| 65 | + S::Error: rkyv::rancor::Source, |
| 66 | +{ |
| 67 | + fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> { |
| 68 | + rkyv::string::ArchivedString::serialize_from_str(&self.0, serializer) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl<D: rkyv::rancor::Fallible + ?Sized> rkyv::Deserialize<SmallString, D> |
| 73 | + for rkyv::string::ArchivedString |
| 74 | +{ |
| 75 | + fn deserialize(&self, _deserializer: &mut D) -> Result<SmallString, D::Error> { |
| 76 | + Ok(SmallString::from(self.as_str())) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl PartialEq<SmallString> for rkyv::string::ArchivedString { |
| 81 | + fn eq(&self, other: &SmallString) -> bool { |
| 82 | + **other == **self |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl PartialOrd<SmallString> for rkyv::string::ArchivedString { |
| 87 | + fn partial_cmp(&self, other: &SmallString) -> Option<::core::cmp::Ordering> { |
| 88 | + Some(self.as_str().cmp(other)) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/// An [`schemars::JsonSchema`] implementation for [`SmallString`]. |
| 93 | +#[cfg(feature = "schemars")] |
| 94 | +impl schemars::JsonSchema for SmallString { |
| 95 | + fn schema_name() -> String { |
| 96 | + String::schema_name() |
| 97 | + } |
| 98 | + |
| 99 | + fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { |
| 100 | + String::json_schema(_gen) |
| 101 | + } |
| 102 | +} |
0 commit comments