Skip to content

Commit 3b342fa

Browse files
committed
Auto merge of #32586 - seanmonstar:speialize-to-string, r=alexcrichton
specialize ToString for str If there was some conditional compiling we could do, such that this impl only exists in nightly, and is turned off in beta/stable, I think that'd be an improvement here, as we could test specialization out without affecting stable builds.
2 parents a2f0cc6 + fc8cf9c commit 3b342fa

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/libcollections/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#![feature(placement_new_protocol)]
4949
#![feature(shared)]
5050
#![feature(slice_patterns)]
51+
#![feature(specialization)]
5152
#![feature(staged_api)]
5253
#![feature(step_by)]
5354
#![feature(str_char)]

src/libcollections/string.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,7 @@ pub trait ToString {
17551755
#[stable(feature = "rust1", since = "1.0.0")]
17561756
impl<T: fmt::Display + ?Sized> ToString for T {
17571757
#[inline]
1758-
fn to_string(&self) -> String {
1758+
default fn to_string(&self) -> String {
17591759
use core::fmt::Write;
17601760
let mut buf = String::new();
17611761
let _ = buf.write_fmt(format_args!("{}", self));
@@ -1764,6 +1764,14 @@ impl<T: fmt::Display + ?Sized> ToString for T {
17641764
}
17651765
}
17661766

1767+
#[stable(feature = "str_to_string_specialization", since = "1.9.0")]
1768+
impl ToString for str {
1769+
#[inline]
1770+
fn to_string(&self) -> String {
1771+
String::from(self)
1772+
}
1773+
}
1774+
17671775
#[stable(feature = "rust1", since = "1.0.0")]
17681776
impl AsRef<str> for String {
17691777
#[inline]

0 commit comments

Comments
 (0)