@@ -2643,14 +2643,54 @@ impl ToString for i8 {
2643
2643
}
2644
2644
}
2645
2645
2646
- #[ doc( hidden) ]
2646
+ // Generic/generated code can sometimes have multiple, nested references
2647
+ // for strings, including `&&&str`s that would never be written
2648
+ // by hand. This macro generates twelve layers of nested `&`-impl
2649
+ // for primitive strings.
2647
2650
#[ cfg( not( no_global_oom_handling) ) ]
2648
- #[ stable( feature = "str_to_string_specialization" , since = "1.9.0" ) ]
2649
- impl ToString for str {
2650
- #[ inline]
2651
- fn to_string ( & self ) -> String {
2652
- String :: from ( self )
2653
- }
2651
+ macro_rules! to_string_str_wrap_in_ref {
2652
+ { x $( $x: ident) * } => {
2653
+ & to_string_str_wrap_in_ref! { $( $x) * }
2654
+ } ;
2655
+ { } => { str } ;
2656
+ }
2657
+ #[ cfg( not( no_global_oom_handling) ) ]
2658
+ macro_rules! to_string_expr_wrap_in_deref {
2659
+ { $self: expr ; x $( $x: ident) * } => {
2660
+ * ( to_string_expr_wrap_in_deref! { $self ; $( $x) * } )
2661
+ } ;
2662
+ { $self: expr ; } => { $self } ;
2663
+ }
2664
+ #[ cfg( not( no_global_oom_handling) ) ]
2665
+ macro_rules! to_string_str {
2666
+ { $( $( $x: ident) * ) ,+} => {
2667
+ $(
2668
+ #[ doc( hidden) ]
2669
+ #[ stable( feature = "str_to_string_specialization" , since = "1.9.0" ) ]
2670
+ impl ToString for to_string_str_wrap_in_ref!( $( $x) * ) {
2671
+ #[ inline]
2672
+ fn to_string( & self ) -> String {
2673
+ String :: from( to_string_expr_wrap_in_deref!( self ; $( $x) * ) )
2674
+ }
2675
+ }
2676
+ ) +
2677
+ } ;
2678
+ }
2679
+
2680
+ #[ cfg( not( no_global_oom_handling) ) ]
2681
+ to_string_str ! {
2682
+ x x x x x x x x x x x x,
2683
+ x x x x x x x x x x x,
2684
+ x x x x x x x x x x,
2685
+ x x x x x x x x x,
2686
+ x x x x x x x x,
2687
+ x x x x x x x,
2688
+ x x x x x x,
2689
+ x x x x x,
2690
+ x x x x,
2691
+ x x x,
2692
+ x x,
2693
+ x,
2654
2694
}
2655
2695
2656
2696
#[ doc( hidden) ]
0 commit comments