Skip to content

feat(span): add various implementations of FromIn for Atom. #4090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
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
Loading