Skip to content

Commit 8a8ed07

Browse files
committed
Auto merge of rust-lang#82576 - gilescope:to_string, r=Amanieu
i8 and u8::to_string() specialisation (far less asm). Take 2. Around 1/6th of the assembly to without specialisation. https://godbolt.org/z/bzz8Mq (partially fixes rust-lang#73533 )
2 parents 6b5de7a + 05330aa commit 8a8ed07

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

library/alloc/src/string.rs

+41
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,47 @@ impl ToString for char {
22882288
}
22892289
}
22902290

2291+
#[stable(feature = "u8_to_string_specialization", since = "1.999.0")]
2292+
impl ToString for u8 {
2293+
#[inline]
2294+
fn to_string(&self) -> String {
2295+
let mut buf = String::with_capacity(3);
2296+
let mut n = *self;
2297+
if n >= 10 {
2298+
if n >= 100 {
2299+
buf.push((b'0' + n / 100) as char);
2300+
n %= 100;
2301+
}
2302+
buf.push((b'0' + n / 10) as char);
2303+
n %= 10;
2304+
}
2305+
buf.push((b'0' + n) as char);
2306+
buf
2307+
}
2308+
}
2309+
2310+
#[stable(feature = "i8_to_string_specialization", since = "1.999.0")]
2311+
impl ToString for i8 {
2312+
#[inline]
2313+
fn to_string(&self) -> String {
2314+
let mut buf = String::with_capacity(4);
2315+
if self.is_negative() {
2316+
buf.push('-');
2317+
}
2318+
let mut n = self.unsigned_abs();
2319+
if n >= 10 {
2320+
if n >= 100 {
2321+
buf.push('1');
2322+
n -= 100;
2323+
}
2324+
buf.push((b'0' + n / 10) as char);
2325+
n %= 10;
2326+
}
2327+
buf.push((b'0' + n) as char);
2328+
buf
2329+
}
2330+
}
2331+
22912332
#[stable(feature = "str_to_string_specialization", since = "1.9.0")]
22922333
impl ToString for str {
22932334
#[inline]

0 commit comments

Comments
 (0)