diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index b1f860d6b64a8..8b0f3047a0a8b 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -2224,6 +2224,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]