summary refs log tree commit diff
path: root/library/alloc/src
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-08-14 05:05:51 +0200
committerGitHub <noreply@github.com>2024-08-14 05:05:51 +0200
commit85180cd365f2a518183b24468fb5754f6c842e0f (patch)
tree8dc6a0d572ac4c08c2cd0b63220a895393c66276 /library/alloc/src
parent2cb59087dbbbacbe173414e43c12bf046d84224c (diff)
parentc6fb0f344e0c6edb34966b1d73c3c1d8c5afbe34 (diff)
downloadrust-85180cd365f2a518183b24468fb5754f6c842e0f.tar.gz
rust-85180cd365f2a518183b24468fb5754f6c842e0f.zip
Rollup merge of #128759 - notriddle:notriddle/spec-to-string, r=workingjubilee,compiler-errors
alloc: add ToString specialization for `&&str`

Fixes #128690
Diffstat (limited to 'library/alloc/src')
-rw-r--r--library/alloc/src/string.rs54
1 files changed, 47 insertions, 7 deletions
diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs
index 124230812df..e628be1546f 100644
--- a/library/alloc/src/string.rs
+++ b/library/alloc/src/string.rs
@@ -2643,14 +2643,54 @@ impl ToString for i8 {
     }
 }
 
-#[doc(hidden)]
+// 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))]
-#[stable(feature = "str_to_string_specialization", since = "1.9.0")]
-impl ToString for str {
-    #[inline]
-    fn to_string(&self) -> String {
-        String::from(self)
-    }
+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)*),+} => {
+        $(
+            #[doc(hidden)]
+            #[stable(feature = "str_to_string_specialization", since = "1.9.0")]
+            impl ToString for to_string_str_wrap_in_ref!($($x)*) {
+                #[inline]
+                fn to_string(&self) -> String {
+                    String::from(to_string_expr_wrap_in_deref!(self ; $($x)*))
+                }
+            }
+        )+
+    };
+}
+
+#[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,
 }
 
 #[doc(hidden)]