Skip to content

Commit feca61e

Browse files
author
Yuki Okushi
authored
Rollup merge of #106662 - Ezrashaw:specialize-bool-tostring, r=cuviper
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.
2 parents 80c3ec8 + 203bbfa commit feca61e

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

library/alloc/src/string.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2548,6 +2548,15 @@ impl ToString for char {
25482548
}
25492549
}
25502550

2551+
#[cfg(not(no_global_oom_handling))]
2552+
#[stable(feature = "bool_to_string_specialization", since = "CURRENT_RUSTC_VERSION")]
2553+
impl ToString for bool {
2554+
#[inline]
2555+
fn to_string(&self) -> String {
2556+
String::from(if *self { "true" } else { "false" })
2557+
}
2558+
}
2559+
25512560
#[cfg(not(no_global_oom_handling))]
25522561
#[stable(feature = "u8_to_string_specialization", since = "1.54.0")]
25532562
impl ToString for u8 {

0 commit comments

Comments
 (0)