Skip to content
Merged
Changes from 4 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
54 changes: 54 additions & 0 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2224,6 +2224,60 @@ 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 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 = "i8_to_string_specialization", since = "1.999.0")]
impl ToString for i8 {
#[inline]
fn to_string(&self) -> String {
let mut vec = vec![0; 4];
let n = *self;
let mut free = 0;
let mut n: u8 = if n.is_negative() {
vec[free] = b'-';
free += 1;
i8::unsigned_abs(n)
} else {
n as u8
};
if n >= 10 {
if n >= 100 {
n -= 100;
vec[free] = b'1';
free += 1;
}
debug_assert!(n < 100);
vec[free] = b'0' + n / 10;
free += 1;
n %= 10;
}
debug_assert!(n < 10);
vec[free] = b'0' + n;
free += 1;
vec.truncate(free);

// SAFETY: Vec only contains ascii so valid utf8
unsafe { String::from_utf8_unchecked(vec) }
}
}

#[stable(feature = "str_to_string_specialization", since = "1.9.0")]
impl ToString for str {
#[inline]
Expand Down