From 2e636184629f394a1afe6ee7f6ba6a169680a033 Mon Sep 17 00:00:00 2001 From: rzvxa <3788964+rzvxa@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:28:54 +0000 Subject: [PATCH] feat(span): implement `CloneIn` for the AST-related items. (#4729) Follow-on after #4276, related to #4284. --- crates/oxc_span/src/atom.rs | 10 +++++++++- crates/oxc_span/src/source_type/mod.rs | 9 +++++++++ crates/oxc_span/src/span/mod.rs | 9 +++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/oxc_span/src/atom.rs b/crates/oxc_span/src/atom.rs index f81bd59a00f2c..0d3ff890b4c75 100644 --- a/crates/oxc_span/src/atom.rs +++ b/crates/oxc_span/src/atom.rs @@ -9,7 +9,7 @@ use compact_str::CompactString; use serde::{Serialize, Serializer}; use crate::Span; -use oxc_allocator::{Allocator, FromIn}; +use oxc_allocator::{Allocator, CloneIn, FromIn}; #[cfg(feature = "serialize")] #[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)] @@ -59,6 +59,14 @@ impl<'a> Atom<'a> { } } +impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for Atom<'old_alloc> { + type Cloned = Atom<'new_alloc>; + + fn clone_in(&self, alloc: &'new_alloc Allocator) -> Self::Cloned { + Atom::from_in(self.as_str(), alloc) + } +} + impl<'a, 'b> FromIn<'a, &'b Atom<'a>> for Atom<'a> { fn from_in(s: &'b Atom<'a>, _: &'a Allocator) -> Self { Self::from(s.0) diff --git a/crates/oxc_span/src/source_type/mod.rs b/crates/oxc_span/src/source_type/mod.rs index 87b04e1eff843..1229dcee5d06e 100644 --- a/crates/oxc_span/src/source_type/mod.rs +++ b/crates/oxc_span/src/source_type/mod.rs @@ -1,6 +1,7 @@ use std::path::Path; mod types; +use oxc_allocator::{Allocator, CloneIn}; pub use types::*; #[derive(Debug)] @@ -17,6 +18,14 @@ impl Default for SourceType { } } +impl<'a> CloneIn<'a> for SourceType { + type Cloned = Self; + #[inline] + fn clone_in(&self, _: &'a Allocator) -> Self { + *self + } +} + /// Valid file extensions pub const VALID_EXTENSIONS: [&str; 8] = ["js", "mjs", "cjs", "jsx", "ts", "mts", "cts", "tsx"]; diff --git a/crates/oxc_span/src/span/mod.rs b/crates/oxc_span/src/span/mod.rs index 89b98b8aaa5d8..504dd916f0188 100644 --- a/crates/oxc_span/src/span/mod.rs +++ b/crates/oxc_span/src/span/mod.rs @@ -6,6 +6,7 @@ use std::{ use miette::{LabeledSpan, SourceOffset, SourceSpan}; mod types; +use oxc_allocator::{Allocator, CloneIn}; pub use types::Span; /// An Empty span useful for creating AST nodes. @@ -338,6 +339,14 @@ impl GetSpanMut for Span { } } +impl<'a> CloneIn<'a> for Span { + type Cloned = Self; + #[inline] + fn clone_in(&self, _: &'a Allocator) -> Self { + *self + } +} + #[cfg(test)] mod test { use super::Span;