about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGiles Cope <gilescope@gmail.com>2021-02-26 23:04:42 +0000
committerGiles Cope <gilescope@gmail.com>2021-02-27 01:15:37 +0000
commita69960a4ecf5d452bcebae3f75d056fd6c451a6e (patch)
tree89d421c665e0274d3d8c9b7a6ccba270b9305315
parent9a9477fada5baf69d693e717d6df902e411a73d6 (diff)
downloadrust-a69960a4ecf5d452bcebae3f75d056fd6c451a6e.tar.gz
rust-a69960a4ecf5d452bcebae3f75d056fd6c451a6e.zip
u8::to_string() specialisation (far less asm).
-rw-r--r--library/alloc/src/string.rs19
1 files changed, 19 insertions, 0 deletions
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]