Skip to content

Commit

Permalink
alloc: make to_string_str! a bit less complex
Browse files Browse the repository at this point in the history
  • Loading branch information
notriddle committed Aug 7, 2024
1 parent 20c833c commit ffa62b2
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2647,37 +2647,47 @@ 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)* }
macro_rules! to_string_str_wrap_in_ref {
{x $($x:ident)*} => {
&to_string_str_wrap_in_ref! { $($x)* }
};
{impl ;} => { };
{} => { str };
}
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)*))
}
macro_rules! to_string_str {
{$($($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_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)* }
)+
};
}

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)]
Expand Down

0 comments on commit ffa62b2

Please sign in to comment.