From a69960a4ecf5d452bcebae3f75d056fd6c451a6e Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Fri, 26 Feb 2021 23:04:42 +0000 Subject: [PATCH] u8::to_string() specialisation (far less asm). --- library/alloc/src/string.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index b1f860d6b64..1cdfa39cd79 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -2224,6 +2224,25 @@ 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 result = String::with_capacity(3); + let mut n = *self; + if n >= 100 { + result.push((b'0' + n / 100) as char); + n %= 100; + } + if !result.is_empty() || n >= 10 { + result.push((b'0' + n / 10) as char); + n %= 10; + }; + result.push((b'0' + n) as char); + result + } +} + #[stable(feature = "str_to_string_specialization", since = "1.9.0")] impl ToString for str { #[inline]