Skip to content

Commit

Permalink
Rollup merge of #106662 - Ezrashaw:specialize-bool-tostring, r=cuviper
Browse files Browse the repository at this point in the history
specialize impl of `ToString` on `bool`

Fixes #106611

Specialize `bool`s `ToString` impl by copying it from `Display`. This is a significant optimization as we avoid lots of dynamic dispatch. AFAIK, this doesn't require a API Change Proposal as this doesn't regress existing code and can be undone without regressing code.
  • Loading branch information
Yuki Okushi authored Jan 11, 2023
2 parents 80c3ec8 + 203bbfa commit feca61e
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2548,6 +2548,15 @@ impl ToString for char {
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "bool_to_string_specialization", since = "CURRENT_RUSTC_VERSION")]
impl ToString for bool {
#[inline]
fn to_string(&self) -> String {
String::from(if *self { "true" } else { "false" })
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "u8_to_string_specialization", since = "1.54.0")]
impl ToString for u8 {
Expand Down

0 comments on commit feca61e

Please sign in to comment.