From 6a24ae96eaf175b048e70dfb46dfb860ee7147d9 Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Mon, 18 Aug 2025 11:50:27 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Simplify=20macro=20generating=20`ToString`?= =?UTF-8?q?=20implementations=20for=20`&=E2=80=A6&str`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use deref coercion to let the compiler remove any amount of references. --- library/alloc/src/string.rs | 55 ++++++++++++++----------------------- 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 9eacbf00e4233..b1f2cb598c879 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -2949,52 +2949,39 @@ impl SpecToString for i8 { } } -// Generic/generated code can sometimes have multiple, nested references -// for strings, including `&&&str`s that would never be written -// by hand. This macro generates twelve layers of nested `&`-impl -// for primitive strings. -#[cfg(not(no_global_oom_handling))] -macro_rules! to_string_str_wrap_in_ref { - {x $($x:ident)*} => { - &to_string_str_wrap_in_ref! { $($x)* } - }; - {} => { str }; -} -#[cfg(not(no_global_oom_handling))] -macro_rules! to_string_expr_wrap_in_deref { - {$self:expr ; x $($x:ident)*} => { - *(to_string_expr_wrap_in_deref! { $self ; $($x)* }) - }; - {$self:expr ;} => { $self }; -} #[cfg(not(no_global_oom_handling))] macro_rules! to_string_str { - {$($($x:ident)*),+} => { + {$($type:ty,)*} => { $( - impl SpecToString for to_string_str_wrap_in_ref!($($x)*) { + impl SpecToString for $type { #[inline] fn spec_to_string(&self) -> String { - String::from(to_string_expr_wrap_in_deref!(self ; $($x)*)) + let s: &str = self; + String::from(s) } } - )+ + )* }; } #[cfg(not(no_global_oom_handling))] to_string_str! { - x x x x x x x x x x x x, - x x x x x x x x x x x, - x x x x x x x x x x, - x x x x x x x x x, - x x x x x x x x, - x x x x x x x, - x x x x x x, - x x x x x, - x x x x, - x x x, - x x, - x, + // Generic/generated code can sometimes have multiple, nested references + // for strings, including `&&&str`s that would never be written + // by hand. + &&&&&&&&&&&&str, + &&&&&&&&&&&str, + &&&&&&&&&&str, + &&&&&&&&&str, + &&&&&&&&str, + &&&&&&&str, + &&&&&&str, + &&&&&str, + &&&&str, + &&&str, + &&str, + &str, + str, } #[cfg(not(no_global_oom_handling))] From cb5a4d138a298853225d7561c243ce6e23064775 Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Mon, 18 Aug 2025 11:51:31 +0200 Subject: [PATCH 2/2] Use `ToString` specialization macro also for `Cow` and `String` --- library/alloc/src/string.rs | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index b1f2cb598c879..ec4d934a5a3f6 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -2966,6 +2966,8 @@ macro_rules! to_string_str { #[cfg(not(no_global_oom_handling))] to_string_str! { + Cow<'_, str>, + String, // Generic/generated code can sometimes have multiple, nested references // for strings, including `&&&str`s that would never be written // by hand. @@ -2984,22 +2986,6 @@ to_string_str! { str, } -#[cfg(not(no_global_oom_handling))] -impl SpecToString for Cow<'_, str> { - #[inline] - fn spec_to_string(&self) -> String { - self[..].to_owned() - } -} - -#[cfg(not(no_global_oom_handling))] -impl SpecToString for String { - #[inline] - fn spec_to_string(&self) -> String { - self.to_owned() - } -} - #[cfg(not(no_global_oom_handling))] impl SpecToString for fmt::Arguments<'_> { #[inline]