Skip to content

Commit e42b1aa

Browse files
committed
Hide ToString specializations behind a private trait
1 parent 63cc2bb commit e42b1aa

File tree

2 files changed

+55
-38
lines changed

2 files changed

+55
-38
lines changed

library/alloc/src/string.rs

+34-19
Original file line numberDiff line numberDiff line change
@@ -2375,7 +2375,22 @@ impl<T: fmt::Display + ?Sized> ToString for T {
23752375
// See <https://github.com/rust-lang/rust/pull/74852>, the last attempt
23762376
// to try to remove it.
23772377
#[inline]
2378-
default fn to_string(&self) -> String {
2378+
fn to_string(&self) -> String {
2379+
SpecToString::spec_to_string(self)
2380+
}
2381+
}
2382+
2383+
#[unstable(issue = "none", feature = "spec_to_string")]
2384+
#[doc(hidden)]
2385+
pub trait SpecToString {
2386+
fn spec_to_string(&self) -> String;
2387+
}
2388+
2389+
#[cfg(not(no_global_oom_handling))]
2390+
#[unstable(issue = "none", feature = "spec_to_string")]
2391+
impl<T: fmt::Display + ?Sized> SpecToString for T {
2392+
#[inline]
2393+
default fn spec_to_string(&self) -> String {
23792394
let mut buf = String::new();
23802395
let mut formatter = core::fmt::Formatter::new(&mut buf);
23812396
// Bypass format_args!() to avoid write_str with zero-length strs
@@ -2386,19 +2401,19 @@ impl<T: fmt::Display + ?Sized> ToString for T {
23862401
}
23872402

23882403
#[cfg(not(no_global_oom_handling))]
2389-
#[stable(feature = "char_to_string_specialization", since = "1.46.0")]
2390-
impl ToString for char {
2404+
#[unstable(issue = "none", feature = "spec_to_string")]
2405+
impl SpecToString for char {
23912406
#[inline]
2392-
fn to_string(&self) -> String {
2407+
fn spec_to_string(&self) -> String {
23932408
String::from(self.encode_utf8(&mut [0; 4]))
23942409
}
23952410
}
23962411

23972412
#[cfg(not(no_global_oom_handling))]
2398-
#[stable(feature = "u8_to_string_specialization", since = "1.54.0")]
2399-
impl ToString for u8 {
2413+
#[unstable(issue = "none", feature = "spec_to_string")]
2414+
impl SpecToString for u8 {
24002415
#[inline]
2401-
fn to_string(&self) -> String {
2416+
fn spec_to_string(&self) -> String {
24022417
let mut buf = String::with_capacity(3);
24032418
let mut n = *self;
24042419
if n >= 10 {
@@ -2415,10 +2430,10 @@ impl ToString for u8 {
24152430
}
24162431

24172432
#[cfg(not(no_global_oom_handling))]
2418-
#[stable(feature = "i8_to_string_specialization", since = "1.54.0")]
2419-
impl ToString for i8 {
2433+
#[unstable(issue = "none", feature = "spec_to_string")]
2434+
impl SpecToString for i8 {
24202435
#[inline]
2421-
fn to_string(&self) -> String {
2436+
fn spec_to_string(&self) -> String {
24222437
let mut buf = String::with_capacity(4);
24232438
if self.is_negative() {
24242439
buf.push('-');
@@ -2438,28 +2453,28 @@ impl ToString for i8 {
24382453
}
24392454

24402455
#[cfg(not(no_global_oom_handling))]
2441-
#[stable(feature = "str_to_string_specialization", since = "1.9.0")]
2442-
impl ToString for str {
2456+
#[unstable(issue = "none", feature = "spec_to_string")]
2457+
impl SpecToString for str {
24432458
#[inline]
2444-
fn to_string(&self) -> String {
2459+
fn spec_to_string(&self) -> String {
24452460
String::from(self)
24462461
}
24472462
}
24482463

24492464
#[cfg(not(no_global_oom_handling))]
2450-
#[stable(feature = "cow_str_to_string_specialization", since = "1.17.0")]
2451-
impl ToString for Cow<'_, str> {
2465+
#[unstable(issue = "none", feature = "spec_to_string")]
2466+
impl SpecToString for Cow<'_, str> {
24522467
#[inline]
2453-
fn to_string(&self) -> String {
2468+
fn spec_to_string(&self) -> String {
24542469
self[..].to_owned()
24552470
}
24562471
}
24572472

24582473
#[cfg(not(no_global_oom_handling))]
2459-
#[stable(feature = "string_to_string_specialization", since = "1.17.0")]
2460-
impl ToString for String {
2474+
#[unstable(issue = "none", feature = "spec_to_string")]
2475+
impl SpecToString for String {
24612476
#[inline]
2462-
fn to_string(&self) -> String {
2477+
fn spec_to_string(&self) -> String {
24632478
self.to_owned()
24642479
}
24652480
}

library/proc_macro/src/lib.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#![feature(restricted_std)]
3232
#![feature(rustc_attrs)]
3333
#![feature(min_specialization)]
34+
#![feature(spec_to_string)]
3435
#![recursion_limit = "256"]
3536

3637
#[unstable(feature = "proc_macro_internals", issue = "27812")]
@@ -46,6 +47,7 @@ use std::cmp::Ordering;
4647
use std::ops::RangeBounds;
4748
use std::path::PathBuf;
4849
use std::str::FromStr;
50+
use std::string::SpecToString;
4951
use std::{error, fmt, iter, mem};
5052

5153
/// Determines whether proc_macro has been made accessible to the currently
@@ -141,9 +143,9 @@ impl FromStr for TokenStream {
141143

142144
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
143145
// based on it (the reverse of the usual relationship between the two).
144-
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
145-
impl ToString for TokenStream {
146-
fn to_string(&self) -> String {
146+
#[unstable(issue = "none", feature = "spec_to_string")]
147+
impl SpecToString for TokenStream {
148+
fn spec_to_string(&self) -> String {
147149
self.0.to_string()
148150
}
149151
}
@@ -624,9 +626,9 @@ impl From<Literal> for TokenTree {
624626

625627
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
626628
// based on it (the reverse of the usual relationship between the two).
627-
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
628-
impl ToString for TokenTree {
629-
fn to_string(&self) -> String {
629+
#[unstable(issue = "none", feature = "spec_to_string")]
630+
impl SpecToString for TokenTree {
631+
fn spec_to_string(&self) -> String {
630632
match *self {
631633
TokenTree::Group(ref t) => t.to_string(),
632634
TokenTree::Ident(ref t) => t.to_string(),
@@ -754,9 +756,9 @@ impl Group {
754756

755757
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
756758
// based on it (the reverse of the usual relationship between the two).
757-
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
758-
impl ToString for Group {
759-
fn to_string(&self) -> String {
759+
#[unstable(issue = "none", feature = "spec_to_string")]
760+
impl SpecToString for Group {
761+
fn spec_to_string(&self) -> String {
760762
TokenStream::from(TokenTree::from(self.clone())).to_string()
761763
}
762764
}
@@ -854,9 +856,9 @@ impl Punct {
854856

855857
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
856858
// based on it (the reverse of the usual relationship between the two).
857-
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
858-
impl ToString for Punct {
859-
fn to_string(&self) -> String {
859+
#[unstable(issue = "none", feature = "spec_to_string")]
860+
impl SpecToString for Punct {
861+
fn spec_to_string(&self) -> String {
860862
TokenStream::from(TokenTree::from(self.clone())).to_string()
861863
}
862864
}
@@ -935,7 +937,7 @@ impl Ident {
935937
}
936938

937939
/// Returns the span of this `Ident`, encompassing the entire string returned
938-
/// by [`to_string`](Self::to_string).
940+
/// by [`to_string`](ToString::to_string).
939941
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
940942
pub fn span(&self) -> Span {
941943
Span(self.0.span())
@@ -950,9 +952,9 @@ impl Ident {
950952

951953
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
952954
// based on it (the reverse of the usual relationship between the two).
953-
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
954-
impl ToString for Ident {
955-
fn to_string(&self) -> String {
955+
#[unstable(issue = "none", feature = "spec_to_string")]
956+
impl SpecToString for Ident {
957+
fn spec_to_string(&self) -> String {
956958
TokenStream::from(TokenTree::from(self.clone())).to_string()
957959
}
958960
}
@@ -1210,9 +1212,9 @@ impl FromStr for Literal {
12101212

12111213
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
12121214
// based on it (the reverse of the usual relationship between the two).
1213-
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
1214-
impl ToString for Literal {
1215-
fn to_string(&self) -> String {
1215+
#[unstable(issue = "none", feature = "spec_to_string")]
1216+
impl SpecToString for Literal {
1217+
fn spec_to_string(&self) -> String {
12161218
self.0.to_string()
12171219
}
12181220
}

0 commit comments

Comments
 (0)