Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i8 and u8::to_string() specialisation (far less asm). #82576

Merged
merged 6 commits into from
May 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2224,6 +2224,47 @@ impl ToString for char {
}
}

#[stable(feature = "u8_to_string_specialization", since = "1.999.0")]
Copy link
Contributor

@pickfire pickfire May 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is with the since tag? Why 1.999?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already fixed on master.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, wasn't sure what to put for that. Yes 1.54 makes sense, but at the time I didn't want to presume so put something a little further out.

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]
Expand Down