Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat(span): implement CloneIn for the AST-related items. (#4729)
Follow-on after #4276, related to #4284.
  • Loading branch information
rzvxa committed Aug 7, 2024
commit 2e636184629f394a1afe6ee7f6ba6a169680a033
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