feat(span): add Atom::from_cow_in method (#11480)

Move the functionality of `AstBuilder::atom_from_cow` into new method `Atom::from_cow_in`. This allows efficiently converting a `Cow<'a, str>` to `Atom<'a>` in places where you don't have access to an `AstBuilder`.
This commit is contained in:
overlookmotel committed 2025-06-04 13:17:20 +00:00
1 parent 97aa9ccfc1
commit 590b80963f
2 files changed
+15 -4

No files matched your search

+1 -4
View File
@@ -118,10 +118,7 @@ impl<'a> AstBuilder<'a> {
/// If the `Cow` is owned, allocates the string into arena to generate a new `Atom`.
#[inline]
pub fn atom_from_cow(self, value: &Cow<'a, str>) -> Atom<'a> {
match value {
Cow::Borrowed(s) => Atom::from(*s),
Cow::Owned(s) => self.atom(s),
}
Atom::from_cow_in(value, self.allocator)
}
/// `0`
+14
View File
@@ -88,6 +88,20 @@ impl<'a> Atom<'a> {
) -> Atom<'a> {
Self::from(allocator.alloc_concat_strs_array(strings))
}
/// Convert a [`Cow<'a, str>`] to an [`Atom<'a>`].
///
/// If the `Cow` borrows a string from arena, returns an `Atom` which references that same string,
/// without allocating a new one.
///
/// If the `Cow` is owned, allocates the string into arena to generate a new `Atom`.
#[inline]
pub fn from_cow_in(value: &Cow<'a, str>, allocator: &'a Allocator) -> Atom<'a> {
match value {
Cow::Borrowed(s) => Atom::from(*s),
Cow::Owned(s) => Atom::from_in(s, allocator),
}
}
}
impl<'new_alloc> CloneIn<'new_alloc> for Atom<'_> {