From c93efddcde4157dc7252b1a8d945563200b273f2 Mon Sep 17 00:00:00 2001 From: Joshix Date: Tue, 9 Sep 2025 06:00:00 +0000 Subject: [PATCH 1/4] improve cow from string --- src/string.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/string.rs b/src/string.rs index 907dee1..47ec16f 100644 --- a/src/string.rs +++ b/src/string.rs @@ -319,8 +319,8 @@ impl From> 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(), } } } @@ -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()), + } } } From 154128bfdf3cd552963d671345078f56a9a9f91d Mon Sep 17 00:00:00 2001 From: Joshix Date: Tue, 9 Sep 2025 06:00:00 +0000 Subject: [PATCH 2/4] remove unused import --- src/string.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/string.rs b/src/string.rs index 47ec16f..0b0916d 100644 --- a/src/string.rs +++ b/src/string.rs @@ -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}; From b1b97dd3ef34ecad19946822feaabe639a258d09 Mon Sep 17 00:00:00 2001 From: Joshix Date: Tue, 9 Sep 2025 06:00:00 +0000 Subject: [PATCH 3/4] add test_from_static_to_cow --- src/string.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/string.rs b/src/string.rs index 0b0916d..90f2bed 100644 --- a/src/string.rs +++ b/src/string.rs @@ -446,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| { From 55720f59564f9a65404e7e636e1bd8ea6243d5a5 Mon Sep 17 00:00:00 2001 From: Joshix Date: Fri, 12 Sep 2025 17:27:31 +0200 Subject: [PATCH 4/4] Revert "improve cow from string" This reverts commit c93efddcde4157dc7252b1a8d945563200b273f2. --- src/string.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/string.rs b/src/string.rs index 90f2bed..b83b532 100644 --- a/src/string.rs +++ b/src/string.rs @@ -1,7 +1,7 @@ use alloc::{ borrow::{Cow, ToOwned}, boxed::Box, - string::{String}, + string::{String, ToString}, sync::Arc, }; use core::{borrow::Borrow, fmt::Write as _, hash::Hash, str::FromStr}; @@ -319,8 +319,8 @@ impl From> 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().into(), - FixedStringRepr::Static(a) => a.as_str().into(), + FixedStringRepr::Inline(a) => a.as_str().to_string(), + FixedStringRepr::Static(a) => a.as_str().to_string(), } } }