Skip to content

Commit

Permalink
refactor(atoms): Rename FastAtom to UnsafeAtom
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Feb 21, 2025
1 parent 6a90b1f commit 3e92d93
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
16 changes: 8 additions & 8 deletions crates/swc_atoms/src/fast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@ use std::{

use crate::Atom;

/// FastAtom is a wrapper around [Atom] that does not allocate, but extremely
/// UnsafeAtom is a wrapper around [Atom] that does not allocate, but extremely
/// unsafe.
///
/// Do not use this unless you know what you are doing.
///
/// **Currently, it's considered as a unstable API and may be changed in the
/// future without a semver bump.**
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FastAtom(ManuallyDrop<Atom>);
pub struct UnsafeAtom(ManuallyDrop<Atom>);

impl FastAtom {
impl UnsafeAtom {
/// # Safety
///
/// - You should ensure that the passed `atom` is not freed.
///
/// Some simple solutions to ensure this are
///
/// - Collect all [Atom] and store them somewhere while you are using
/// [FastAtom]
/// - Use [FastAtom] only for short-lived operations where all [Atom] is
/// [UnsafeAtom]
/// - Use [UnsafeAtom] only for short-lived operations where all [Atom] is
/// stored in AST and ensure that the AST is not dropped.
#[inline]
pub unsafe fn new(atom: &Atom) -> Self {
Self(ManuallyDrop::new(transmute_copy(atom)))
}
}

impl Deref for FastAtom {
impl Deref for UnsafeAtom {
type Target = Atom;

#[inline]
Expand All @@ -41,14 +41,14 @@ impl Deref for FastAtom {
}
}

impl Clone for FastAtom {
impl Clone for UnsafeAtom {
#[inline]
fn clone(&self) -> Self {
unsafe { Self::new(&self.0) }
}
}

impl PartialEq<Atom> for FastAtom {
impl PartialEq<Atom> for UnsafeAtom {
#[inline]
fn eq(&self, other: &Atom) -> bool {
*self.0 == *other
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_atoms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ use once_cell::sync::Lazy;
use serde::Serializer;

pub use self::{atom as js_word, Atom as JsWord};
pub use crate::fast::UnsafeAtom;

pub mod fast;
mod fast;

/// Clone-on-write string.
///
Expand Down
12 changes: 6 additions & 6 deletions crates/swc_ecma_ast/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
};

use phf::phf_set;
use swc_atoms::{fast::FastAtom, js_word, Atom};
use swc_atoms::{js_word, Atom, UnsafeAtom};
use swc_common::{
ast_node, util::take::Take, BytePos, EqIgnoreSpan, Mark, Span, Spanned, SyntaxContext, DUMMY_SP,
};
Expand Down Expand Up @@ -475,30 +475,30 @@ impl From<IdentName> for BindingIdent {
///
/// **Currently, it's considered as a unstable API and may be changed in the
/// future without a semver bump.**
pub type UnsafeId = (FastAtom, SyntaxContext);
pub type UnsafeId = (UnsafeAtom, SyntaxContext);

/// This is extremely unsafe so don't use it unless you know what you are doing.
///
/// # Safety
///
/// See [`FastAtom::new`] for constraints.
/// See [`UnsafeAtom::new`] for constraints.
///
/// **Currently, it's considered as a unstable API and may be changed in the
/// future without a semver bump.**
pub unsafe fn unsafe_id(id: &Id) -> UnsafeId {
(FastAtom::new(&id.0), id.1)
(UnsafeAtom::new(&id.0), id.1)
}

/// This is extremely unsafe so don't use it unless you know what you are doing.
///
/// # Safety
///
/// See [`FastAtom::new`] for constraints.
/// See [`UnsafeAtom::new`] for constraints.
///
/// **Currently, it's considered as a unstable API and may be changed in the
/// future without a semver bump.**
pub unsafe fn unsafe_id_from_ident(id: &Ident) -> UnsafeId {
(FastAtom::new(&id.sym), id.ctxt)
(UnsafeAtom::new(&id.sym), id.ctxt)
}

/// See [Ident] for documentation.
Expand Down

0 comments on commit 3e92d93

Please sign in to comment.