Skip to content

Commit 2bd970d

Browse files
authored
Implement From<FixedString> for Box<str> (#12)
1 parent a238a72 commit 2bd970d

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/string.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use alloc::{
22
borrow::{Cow, ToOwned},
33
boxed::Box,
4-
string::{String, ToString},
4+
string::{String},
55
sync::Arc,
66
};
77
use core::{borrow::Borrow, fmt::Write as _, hash::Hash, str::FromStr};
@@ -315,12 +315,18 @@ impl<LenT: ValidLength> From<char> for FixedString<LenT> {
315315
}
316316

317317
impl<LenT: ValidLength> From<FixedString<LenT>> for String {
318+
fn from(value: FixedString<LenT>) -> Self {
319+
Box::<str>::from(value).into()
320+
}
321+
}
322+
323+
impl<LenT: ValidLength> From<FixedString<LenT>> for Box<str> {
318324
fn from(value: FixedString<LenT>) -> Self {
319325
match value.0 {
326+
FixedStringRepr::Inline(a) => a.as_str().into(),
327+
FixedStringRepr::Static(a) => a.as_str().into(),
320328
// SAFETY: Self holds the type invariant that the array is UTF-8.
321-
FixedStringRepr::Heap(a) => unsafe { String::from_utf8_unchecked(a.into()) },
322-
FixedStringRepr::Inline(a) => a.as_str().to_string(),
323-
FixedStringRepr::Static(a) => a.as_str().to_string(),
329+
FixedStringRepr::Heap(a) => unsafe { alloc::str::from_boxed_utf8_unchecked(a.into()) },
324330
}
325331
}
326332
}

0 commit comments

Comments
 (0)