Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/string.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::{
borrow::{Cow, ToOwned},
boxed::Box,
string::{String, ToString},
string::{String},
sync::Arc,
};
use core::{borrow::Borrow, fmt::Write as _, hash::Hash, str::FromStr};
Expand Down Expand Up @@ -319,8 +319,8 @@ impl<LenT: ValidLength> From<FixedString<LenT>> for String {
match value.0 {
// SAFETY: Self holds the type invariant that the array is UTF-8.
FixedStringRepr::Heap(a) => unsafe { String::from_utf8_unchecked(a.into()) },
FixedStringRepr::Inline(a) => a.as_str().to_string(),
FixedStringRepr::Static(a) => a.as_str().to_string(),
FixedStringRepr::Inline(a) => a.as_str().into(),
FixedStringRepr::Static(a) => a.as_str().into(),
Comment thread
Joshix-1 marked this conversation as resolved.
Outdated
}
}
}
Expand All @@ -333,7 +333,10 @@ impl<'a, LenT: ValidLength> From<&'a FixedString<LenT>> for Cow<'a, str> {

impl<LenT: ValidLength> From<FixedString<LenT>> for Cow<'_, str> {
fn from(value: FixedString<LenT>) -> Self {
Cow::Owned(value.into_string())
match value.0 {
FixedStringRepr::Static(static_str) => Cow::Borrowed(static_str.as_str()),
_ => Cow::Owned(value.into()),
}
}
}

Expand Down Expand Up @@ -443,6 +446,23 @@ mod test {
assert_ne!(STR, str);
}

#[test]
fn test_from_static_to_cow() {
const STR: &str = "static string";

let string = FixedString::<u8>::from_static_trunc(STR);

let cow: std::borrow::Cow<'static, _> = string.into();

assert_eq!(cow, STR);

let std::borrow::Cow::Borrowed(string) = cow else {
panic!("Expected borrowed string");
};
Comment thread
Joshix-1 marked this conversation as resolved.

assert_eq!(string, STR);
}

#[test]
fn check_u8_roundtrip() {
check_u8_roundtrip_generic(|original| {
Expand Down