From 9d550aacafdb7d8f8711555cfa18c26ff9f27b4b Mon Sep 17 00:00:00 2001 From: _Kerman Date: Tue, 14 Jan 2025 16:51:31 +0800 Subject: [PATCH] feat(span): add `Atom::r#static` (#8479) This allows ```rs const TYPEOF_UNDEFINED: Atom<'static> = Atom::r#static("undefined"); #[derive(Clone, Copy)] enum Value<'a> { /// Not `Atom<'a>`, which takes one more byte StringLiteral(&'a Atom<'a>), // ... } fn get_typeof<'a>(value: Value<'a>) -> Value<'a> { if ... { return Value::StringLiteral(&TYPEOF_UNDEFINED); } } ``` --- crates/oxc_span/src/atom.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/oxc_span/src/atom.rs b/crates/oxc_span/src/atom.rs index d8e7a94329039..896af0ca79d70 100644 --- a/crates/oxc_span/src/atom.rs +++ b/crates/oxc_span/src/atom.rs @@ -20,10 +20,16 @@ use crate::{cmp::ContentEq, hash::ContentHash, CompactStr}; pub struct Atom<'a>(&'a str); impl Atom<'static> { + /// Get an [`Atom`] containing a static string. + #[inline] + pub const fn r#static(s: &'static str) -> Self { + Atom(s) + } + /// Get an [`Atom`] containing the empty string (`""`). #[inline] pub const fn empty() -> Self { - Atom("") + Self::r#static("") } }