mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 20:32:10 +00:00
refactor(span): simplify Atom (#2630)
This commit is contained in:
parent
cb4e054eb9
commit
b2de57a0e3
1 changed files with 7 additions and 25 deletions
|
|
@ -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
|
||||
/// lifetimeless form.
|
||||
#[derive(Clone, Eq)]
|
||||
pub enum Atom<'a> {
|
||||
Arena(&'a str),
|
||||
}
|
||||
pub struct Atom<'a>(&'a str);
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
impl<'a> Serialize for Atom<'a> {
|
||||
|
|
@ -40,36 +38,28 @@ impl<'a> Serialize for Atom<'a> {
|
|||
impl<'a> Atom<'a> {
|
||||
#[inline]
|
||||
pub fn as_str(&self) -> &str {
|
||||
match self {
|
||||
Self::Arena(s) => s,
|
||||
}
|
||||
self.0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn into_string(self) -> String {
|
||||
match self {
|
||||
Self::Arena(s) => String::from(s),
|
||||
}
|
||||
String::from(self.as_str())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn into_compact_str(self) -> CompactStr {
|
||||
match self {
|
||||
Self::Arena(s) => CompactStr::new(s),
|
||||
}
|
||||
CompactStr::new(self.as_str())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn to_compact_str(&self) -> CompactStr {
|
||||
match &self {
|
||||
Self::Arena(s) => CompactStr::new(s),
|
||||
}
|
||||
CompactStr::new(self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for Atom<'a> {
|
||||
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> {
|
||||
fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
|
||||
match self {
|
||||
Self::Arena(s) => s.hash(hasher),
|
||||
}
|
||||
self.as_str().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 {
|
||||
type Target = str;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue