Skip to content

Commit 44c7fe3

Browse files
committed
feat(span): add various implementations of FromIn for Atom. (#4090)
1 parent 6e6ec74 commit 44c7fe3

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/oxc_span/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ workspace = true
2020
doctest = false
2121

2222
[dependencies]
23+
oxc_allocator = { workspace = true }
24+
2325
miette = { workspace = true }
2426
compact_str = { workspace = true }
2527

crates/oxc_span/src/atom.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{
2-
borrow::Borrow,
2+
borrow::{Borrow, Cow},
33
fmt, hash,
44
ops::{Deref, Index},
55
};
@@ -9,6 +9,7 @@ use compact_str::CompactString;
99
use serde::{Serialize, Serializer};
1010

1111
use crate::Span;
12+
use oxc_allocator::{Allocator, FromIn};
1213

1314
#[cfg(feature = "serialize")]
1415
#[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)]
@@ -58,6 +59,36 @@ impl<'a> Atom<'a> {
5859
}
5960
}
6061

62+
impl<'a, 'b> FromIn<'a, &'b Atom<'a>> for Atom<'a> {
63+
fn from_in(s: &'b Atom<'a>, _: &'a Allocator) -> Self {
64+
Self::from(s.0)
65+
}
66+
}
67+
68+
impl<'a, 'b> FromIn<'a, &'b str> for Atom<'a> {
69+
fn from_in(s: &'b str, alloc: &'a Allocator) -> Self {
70+
Self::from(oxc_allocator::String::from_str_in(s, alloc).into_bump_str())
71+
}
72+
}
73+
74+
impl<'a> FromIn<'a, String> for Atom<'a> {
75+
fn from_in(s: String, alloc: &'a Allocator) -> Self {
76+
Self::from_in(s.as_str(), alloc)
77+
}
78+
}
79+
80+
impl<'a> FromIn<'a, &String> for Atom<'a> {
81+
fn from_in(s: &String, alloc: &'a Allocator) -> Self {
82+
Self::from_in(s.as_str(), alloc)
83+
}
84+
}
85+
86+
impl<'a, 'b> FromIn<'a, Cow<'b, str>> for Atom<'a> {
87+
fn from_in(s: Cow<'b, str>, alloc: &'a Allocator) -> Self {
88+
Self::from_in(&*s, alloc)
89+
}
90+
}
91+
6192
impl<'a> From<&'a str> for Atom<'a> {
6293
fn from(s: &'a str) -> Self {
6394
Self(s)

0 commit comments

Comments
 (0)