mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 12:51:57 +00:00
refactor(span): remove AtomImpl (#2525)
This commit is contained in:
parent
903f17c0df
commit
27052ebfed
1 changed files with 18 additions and 22 deletions
|
|
@ -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),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue