diff --git a/src/string.rs b/src/string.rs index 907dee1..b83b532 100644 --- a/src/string.rs +++ b/src/string.rs @@ -333,7 +333,10 @@ impl<'a, LenT: ValidLength> From<&'a FixedString> for Cow<'a, str> { impl From> for Cow<'_, str> { fn from(value: FixedString) -> Self { - Cow::Owned(value.into_string()) + match value.0 { + FixedStringRepr::Static(static_str) => Cow::Borrowed(static_str.as_str()), + _ => Cow::Owned(value.into()), + } } } @@ -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::::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"); + }; + + assert_eq!(string, STR); + } + #[test] fn check_u8_roundtrip() { check_u8_roundtrip_generic(|original| {