refactor(span): simplify Atom (#2630)

This commit is contained in:
overlookmotel 2024-03-06 13:10:45 +00:00 committed by GitHub
parent cb4e054eb9
commit b2de57a0e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -23,9 +23,7 @@ pub const MAX_INLINE_LEN: usize = 16;
/// Use [CompactStr] with [Atom::to_compact_str] or [Atom::into_compact_str] for the /// Use [CompactStr] with [Atom::to_compact_str] or [Atom::into_compact_str] for the
/// lifetimeless form. /// lifetimeless form.
#[derive(Clone, Eq)] #[derive(Clone, Eq)]
pub enum Atom<'a> { pub struct Atom<'a>(&'a str);
Arena(&'a str),
}
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
impl<'a> Serialize for Atom<'a> { impl<'a> Serialize for Atom<'a> {
@ -40,36 +38,28 @@ impl<'a> Serialize for Atom<'a> {
impl<'a> Atom<'a> { impl<'a> Atom<'a> {
#[inline] #[inline]
pub fn as_str(&self) -> &str { pub fn as_str(&self) -> &str {
match self { self.0
Self::Arena(s) => s,
}
} }
#[inline] #[inline]
pub fn into_string(self) -> String { pub fn into_string(self) -> String {
match self { String::from(self.as_str())
Self::Arena(s) => String::from(s),
}
} }
#[inline] #[inline]
pub fn into_compact_str(self) -> CompactStr { pub fn into_compact_str(self) -> CompactStr {
match self { CompactStr::new(self.as_str())
Self::Arena(s) => CompactStr::new(s),
}
} }
#[inline] #[inline]
pub fn to_compact_str(&self) -> CompactStr { pub fn to_compact_str(&self) -> CompactStr {
match &self { CompactStr::new(self.as_str())
Self::Arena(s) => CompactStr::new(s),
}
} }
} }
impl<'a> From<&'a str> for Atom<'a> { impl<'a> From<&'a str> for Atom<'a> {
fn from(s: &'a str) -> Self { fn from(s: &'a str) -> Self {
Self::Arena(s) Self(s)
} }
} }
@ -107,9 +97,7 @@ impl<'a> PartialEq<Atom<'a>> for &str {
impl<'a> hash::Hash for Atom<'a> { impl<'a> hash::Hash for Atom<'a> {
fn hash<H: hash::Hasher>(&self, hasher: &mut H) { fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
match self { self.as_str().hash(hasher);
Self::Arena(s) => s.hash(hasher),
}
} }
} }
@ -206,12 +194,6 @@ impl From<String> for CompactStr {
} }
} }
impl From<CompactString> for CompactStr {
fn from(s: CompactString) -> Self {
Self(s)
}
}
impl Deref for CompactStr { impl Deref for CompactStr {
type Target = str; type Target = str;