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