Skip to content

Commit 6a24ae9

Browse files
committed
Simplify macro generating ToString implementations for &…&str
Use deref coercion to let the compiler remove any amount of references.
1 parent 8c32e31 commit 6a24ae9

File tree

1 file changed

+21
-34
lines changed

1 file changed

+21
-34
lines changed

library/alloc/src/string.rs

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2949,52 +2949,39 @@ impl SpecToString for i8 {
29492949
}
29502950
}
29512951

2952-
// Generic/generated code can sometimes have multiple, nested references
2953-
// for strings, including `&&&str`s that would never be written
2954-
// by hand. This macro generates twelve layers of nested `&`-impl
2955-
// for primitive strings.
2956-
#[cfg(not(no_global_oom_handling))]
2957-
macro_rules! to_string_str_wrap_in_ref {
2958-
{x $($x:ident)*} => {
2959-
&to_string_str_wrap_in_ref! { $($x)* }
2960-
};
2961-
{} => { str };
2962-
}
2963-
#[cfg(not(no_global_oom_handling))]
2964-
macro_rules! to_string_expr_wrap_in_deref {
2965-
{$self:expr ; x $($x:ident)*} => {
2966-
*(to_string_expr_wrap_in_deref! { $self ; $($x)* })
2967-
};
2968-
{$self:expr ;} => { $self };
2969-
}
29702952
#[cfg(not(no_global_oom_handling))]
29712953
macro_rules! to_string_str {
2972-
{$($($x:ident)*),+} => {
2954+
{$($type:ty,)*} => {
29732955
$(
2974-
impl SpecToString for to_string_str_wrap_in_ref!($($x)*) {
2956+
impl SpecToString for $type {
29752957
#[inline]
29762958
fn spec_to_string(&self) -> String {
2977-
String::from(to_string_expr_wrap_in_deref!(self ; $($x)*))
2959+
let s: &str = self;
2960+
String::from(s)
29782961
}
29792962
}
2980-
)+
2963+
)*
29812964
};
29822965
}
29832966

29842967
#[cfg(not(no_global_oom_handling))]
29852968
to_string_str! {
2986-
x x x x x x x x x x x x,
2987-
x x x x x x x x x x x,
2988-
x x x x x x x x x x,
2989-
x x x x x x x x x,
2990-
x x x x x x x x,
2991-
x x x x x x x,
2992-
x x x x x x,
2993-
x x x x x,
2994-
x x x x,
2995-
x x x,
2996-
x x,
2997-
x,
2969+
// Generic/generated code can sometimes have multiple, nested references
2970+
// for strings, including `&&&str`s that would never be written
2971+
// by hand.
2972+
&&&&&&&&&&&&str,
2973+
&&&&&&&&&&&str,
2974+
&&&&&&&&&&str,
2975+
&&&&&&&&&str,
2976+
&&&&&&&&str,
2977+
&&&&&&&str,
2978+
&&&&&&str,
2979+
&&&&&str,
2980+
&&&&str,
2981+
&&&str,
2982+
&&str,
2983+
&str,
2984+
str,
29982985
}
29992986

30002987
#[cfg(not(no_global_oom_handling))]

0 commit comments

Comments
 (0)