about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Howell <michael@notriddle.com>2024-08-07 09:06:49 -0700
committerMichael Howell <michael@notriddle.com>2024-08-07 10:06:54 -0700
commit3312f5d65244b4ccb035be7b4c61541afc211914 (patch)
tree11c66c7d5429b50fc2aaea73703581362696377f
parent20c833c632d76ee78284441226f12b919318bc4b (diff)
downloadrust-3312f5d65244b4ccb035be7b4c61541afc211914.tar.gz
rust-3312f5d65244b4ccb035be7b4c61541afc211914.zip
alloc: make `to_string_str!` a bit less complex
-rw-r--r--library/alloc/src/string.rs57
1 files changed, 35 insertions, 22 deletions
diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs
index d943901e9ec..e628be1546f 100644
--- a/library/alloc/src/string.rs
+++ b/library/alloc/src/string.rs
@@ -2647,37 +2647,50 @@ impl ToString for i8 {
 // for strings, including `&&&str`s that would never be written
 // by hand. This macro generates twelve layers of nested `&`-impl
 // for primitive strings.
-macro_rules! to_string_str {
-    {type ; x $($x:ident)*} => {
-        &to_string_str! { type ; $($x)* }
-    };
-    {type ;} => { str };
-    {impl ; x $($x:ident)*} => {
-        to_string_str! { $($x)* }
+#[cfg(not(no_global_oom_handling))]
+macro_rules! to_string_str_wrap_in_ref {
+    {x $($x:ident)*} => {
+        &to_string_str_wrap_in_ref! { $($x)* }
     };
-    {impl ;} => { };
+    {} => { str };
+}
+#[cfg(not(no_global_oom_handling))]
+macro_rules! to_string_expr_wrap_in_deref {
     {$self:expr ; x $($x:ident)*} => {
-        *(to_string_str! { $self ; $($x)* })
+        *(to_string_expr_wrap_in_deref! { $self ; $($x)* })
     };
     {$self:expr ;} => { $self };
-    {$($x:ident)*} => {
-        #[doc(hidden)]
-        #[cfg(not(no_global_oom_handling))]
-        #[stable(feature = "str_to_string_specialization", since = "1.9.0")]
-        impl ToString for to_string_str!(type ; $($x)*) {
-            #[inline]
-            fn to_string(&self) -> String {
-                String::from(to_string_str!(self ; $($x)*))
+}
+#[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)*))
+                }
             }
-        }
-        to_string_str! { impl ; $($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 x x,
+    x x x x,
+    x x x,
+    x x,
+    x,
 }
 
 #[doc(hidden)]