about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-05-02 22:01:57 +0000
committerbors <bors@rust-lang.org>2021-05-02 22:01:57 +0000
commit8a8ed078832da1910dc9473bafb1bc2635a6c758 (patch)
tree58786e62f30e76ffe183ec054f2e68d80cf0f877
parent6b5de7aaec2955f2c7c7c82521a9186f2ba4f037 (diff)
parent05330aaf42c794d441a91dc261e3202f965e0ce2 (diff)
downloadrust-8a8ed078832da1910dc9473bafb1bc2635a6c758.tar.gz
rust-8a8ed078832da1910dc9473bafb1bc2635a6c758.zip
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 )
-rw-r--r--library/alloc/src/string.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs
index f4ec4a36ffd..509ff9ce4d1 100644
--- a/library/alloc/src/string.rs
+++ b/library/alloc/src/string.rs
@@ -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]