Skip to content

Commit

Permalink
feat(span): implement CloneIn for the AST-related items. (#4729)
Browse files Browse the repository at this point in the history
Follow-on after #4276, related to #4284.
  • Loading branch information
rzvxa committed Aug 7, 2024
1 parent 23b0040 commit 2e63618
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion crates/oxc_span/src/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions crates/oxc_span/src/source_type/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::Path;

mod types;
use oxc_allocator::{Allocator, CloneIn};
pub use types::*;

#[derive(Debug)]
Expand All @@ -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"];

Expand Down
9 changes: 9 additions & 0 deletions crates/oxc_span/src/span/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 2e63618

Please sign in to comment.