Skip to content

Commit

Permalink
feat(span): add various implementations of FromIn for Atom. (#4090)
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Jul 8, 2024
1 parent 6e6ec74 commit 44c7fe3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/oxc_span/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ workspace = true
doctest = false

[dependencies]
oxc_allocator = { workspace = true }

miette = { workspace = true }
compact_str = { workspace = true }

Expand Down
33 changes: 32 additions & 1 deletion crates/oxc_span/src/atom.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
borrow::Borrow,
borrow::{Borrow, Cow},
fmt, hash,
ops::{Deref, Index},
};
Expand All @@ -9,6 +9,7 @@ use compact_str::CompactString;
use serde::{Serialize, Serializer};

use crate::Span;
use oxc_allocator::{Allocator, FromIn};

#[cfg(feature = "serialize")]
#[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)]
Expand Down Expand Up @@ -58,6 +59,36 @@ impl<'a> Atom<'a> {
}
}

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)
}
}

impl<'a, 'b> FromIn<'a, &'b str> for Atom<'a> {
fn from_in(s: &'b str, alloc: &'a Allocator) -> Self {
Self::from(oxc_allocator::String::from_str_in(s, alloc).into_bump_str())
}
}

impl<'a> FromIn<'a, String> for Atom<'a> {
fn from_in(s: String, alloc: &'a Allocator) -> Self {
Self::from_in(s.as_str(), alloc)
}
}

impl<'a> FromIn<'a, &String> for Atom<'a> {
fn from_in(s: &String, alloc: &'a Allocator) -> Self {
Self::from_in(s.as_str(), alloc)
}
}

impl<'a, 'b> FromIn<'a, Cow<'b, str>> for Atom<'a> {
fn from_in(s: Cow<'b, str>, alloc: &'a Allocator) -> Self {
Self::from_in(&*s, alloc)
}
}

impl<'a> From<&'a str> for Atom<'a> {
fn from(s: &'a str) -> Self {
Self(s)
Expand Down

0 comments on commit 44c7fe3

Please sign in to comment.