Skip to content

Commit a69960a

Browse files
committed
u8::to_string() specialisation (far less asm).
1 parent 9a9477f commit a69960a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

library/alloc/src/string.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,6 +2224,25 @@ impl ToString for char {
22242224
}
22252225
}
22262226

2227+
#[stable(feature = "u8_to_string_specialization", since="1.999.0")]
2228+
impl ToString for u8 {
2229+
#[inline]
2230+
fn to_string(&self) -> String {
2231+
let mut result = String::with_capacity(3);
2232+
let mut n = *self;
2233+
if n >= 100 {
2234+
result.push((b'0' + n / 100) as char);
2235+
n %= 100;
2236+
}
2237+
if !result.is_empty() || n >= 10 {
2238+
result.push((b'0' + n / 10) as char);
2239+
n %= 10;
2240+
};
2241+
result.push((b'0' + n) as char);
2242+
result
2243+
}
2244+
}
2245+
22272246
#[stable(feature = "str_to_string_specialization", since = "1.9.0")]
22282247
impl ToString for str {
22292248
#[inline]

0 commit comments

Comments
 (0)