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

This commit is contained in:
rzvxa 2024-07-08 15:32:57 +00:00
parent 6e6ec748c1
commit 44c7fe39ee
3 changed files with 35 additions and 1 deletions

1
Cargo.lock generated
View file

@ -1726,6 +1726,7 @@ version = "0.17.2"
dependencies = [
"compact_str",
"miette",
"oxc_allocator",
"serde",
"tsify",
"wasm-bindgen",

View file

@ -20,6 +20,8 @@ workspace = true
doctest = false
[dependencies]
oxc_allocator = { workspace = true }
miette = { workspace = true }
compact_str = { workspace = true }

View file

@ -1,5 +1,5 @@
use std::{
borrow::Borrow,
borrow::{Borrow, Cow},
fmt, hash,
ops::{Deref, Index},
};
@ -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)]
@ -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)