Auto merge of #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 #73533 )
This commit is contained in:
bors 2021-05-02 22:01:57 +00:00
commit 8a8ed07883

View File

@ -2288,6 +2288,47 @@ impl ToString for char {
}
}
#[stable(feature = "u8_to_string_specialization", since = "1.999.0")]
impl ToString for u8 {
#[inline]
fn to_string(&self) -> String {
let mut buf = String::with_capacity(3);
let mut n = *self;
if n >= 10 {
if n >= 100 {
buf.push((b'0' + n / 100) as char);
n %= 100;
}
buf.push((b'0' + n / 10) as char);
n %= 10;
}
buf.push((b'0' + n) as char);
buf
}
}
#[stable(feature = "i8_to_string_specialization", since = "1.999.0")]
impl ToString for i8 {
#[inline]
fn to_string(&self) -> String {
let mut buf = String::with_capacity(4);
if self.is_negative() {
buf.push('-');
}
let mut n = self.unsigned_abs();
if n >= 10 {
if n >= 100 {
buf.push('1');
n -= 100;
}
buf.push((b'0' + n / 10) as char);
n %= 10;
}
buf.push((b'0' + n) as char);
buf
}
}
#[stable(feature = "str_to_string_specialization", since = "1.9.0")]
impl ToString for str {
#[inline]