From 44c7fe39ee9dad8fe701e9f3e8c937cc7f8733ca Mon Sep 17 00:00:00 2001 From: rzvxa <3788964+rzvxa@users.noreply.github.com> Date: Mon, 8 Jul 2024 15:32:57 +0000 Subject: [PATCH] feat(span): add various implementations of `FromIn` for `Atom`. (#4090) --- Cargo.lock | 1 + crates/oxc_span/Cargo.toml | 2 ++ crates/oxc_span/src/atom.rs | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index a27855cfebfb2..3c31cc71c0e37 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1726,6 +1726,7 @@ version = "0.17.2" dependencies = [ "compact_str", "miette", + "oxc_allocator", "serde", "tsify", "wasm-bindgen", diff --git a/crates/oxc_span/Cargo.toml b/crates/oxc_span/Cargo.toml index 15d215674bf49..903ded3571d9f 100644 --- a/crates/oxc_span/Cargo.toml +++ b/crates/oxc_span/Cargo.toml @@ -20,6 +20,8 @@ workspace = true doctest = false [dependencies] +oxc_allocator = { workspace = true } + miette = { workspace = true } compact_str = { workspace = true } diff --git a/crates/oxc_span/src/atom.rs b/crates/oxc_span/src/atom.rs index f1cf1f239a510..c4f340f0776e9 100644 --- a/crates/oxc_span/src/atom.rs +++ b/crates/oxc_span/src/atom.rs @@ -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)