refactor(span): remove AtomImpl (#2525)

This commit is contained in:
Boshen 2024-02-27 13:45:58 +08:00 committed by GitHub
parent 903f17c0df
commit 27052ebfed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -22,12 +22,8 @@ export type CompactString = string;
/// An inlinable string for oxc_allocator. /// An inlinable string for oxc_allocator.
/// ///
/// Use [CompactString] with [Atom::to_compact_string()] for the lifetimeless form. /// Use [CompactString] with [Atom::to_compact_string()] for the lifetimeless form.
#[derive(Clone, Eq, Hash)] #[derive(Clone, Eq)]
pub struct Atom<'a>(AtomImpl<'a>); pub enum Atom<'a> {
/// Immutable Inlinable String
#[derive(Clone, Eq, PartialEq)]
enum AtomImpl<'a> {
Arena(&'a str), Arena(&'a str),
Compact(CompactString), Compact(CompactString),
} }
@ -45,52 +41,52 @@ 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.0 { match self {
AtomImpl::Arena(s) => s, Self::Arena(s) => s,
AtomImpl::Compact(s) => s.as_ref(), Self::Compact(s) => s.as_ref(),
} }
} }
#[inline] #[inline]
pub fn into_string(self) -> String { pub fn into_string(self) -> String {
match self.0 { match self {
AtomImpl::Arena(s) => String::from(s), Self::Arena(s) => String::from(s),
AtomImpl::Compact(s) => s.to_string(), Self::Compact(s) => s.to_string(),
} }
} }
#[inline] #[inline]
pub fn into_compact_string(self) -> CompactString { pub fn into_compact_string(self) -> CompactString {
match self.0 { match self {
AtomImpl::Arena(s) => CompactString::new(s), Self::Arena(s) => CompactString::new(s),
AtomImpl::Compact(s) => s, Self::Compact(s) => s,
} }
} }
#[inline] #[inline]
pub fn to_compact_string(&self) -> CompactString { pub fn to_compact_string(&self) -> CompactString {
match &self.0 { match &self {
AtomImpl::Arena(s) => CompactString::new(s), Self::Arena(s) => CompactString::new(s),
AtomImpl::Compact(s) => s.clone(), Self::Compact(s) => s.clone(),
} }
} }
} }
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(AtomImpl::Arena(s)) Self::Arena(s)
} }
} }
impl<'a> From<String> for Atom<'a> { impl<'a> From<String> for Atom<'a> {
fn from(s: String) -> Self { fn from(s: String) -> Self {
Self(AtomImpl::Compact(CompactString::from(s))) Self::Compact(CompactString::from(s))
} }
} }
impl<'a> From<Cow<'_, str>> for Atom<'a> { impl<'a> From<Cow<'_, str>> for Atom<'a> {
fn from(s: Cow<'_, str>) -> Self { fn from(s: Cow<'_, str>) -> Self {
Self(AtomImpl::Compact(CompactString::from(s))) Self::Compact(CompactString::from(s))
} }
} }
@ -126,7 +122,7 @@ impl<'a> PartialEq<Atom<'a>> for &str {
} }
} }
impl<'a> hash::Hash for AtomImpl<'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 { match self {
Self::Arena(s) => s.hash(hasher), Self::Arena(s) => s.hash(hasher),