Simplify macro generating ToString implementations for &…&str
Use deref coercion to let the compiler remove any amount of references.
This commit is contained in:
parent
8c32e313cc
commit
6a24ae96ea
1 changed files with 21 additions and 34 deletions
|
|
@ -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))]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue