Skip to content

Commit

Permalink
Rollup merge of rust-lang#128759 - notriddle:notriddle/spec-to-string…
Browse files Browse the repository at this point in the history
…, r=workingjubilee,compiler-errors

alloc: add ToString specialization for `&&str`

Fixes rust-lang#128690
  • Loading branch information
matthiaskrgr authored Aug 14, 2024
2 parents 0a6a74b + b4e5330 commit 0199b00
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2643,14 +2643,54 @@ impl ToString for i8 {
}
}

#[doc(hidden)]
// 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))]
#[stable(feature = "str_to_string_specialization", since = "1.9.0")]
impl ToString for str {
#[inline]
fn to_string(&self) -> String {
String::from(self)
}
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)*),+} => {
$(
#[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)*))
}
}
)+
};
}

#[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,
}

#[doc(hidden)]
Expand Down

0 comments on commit 0199b00

Please sign in to comment.