Skip to content

Commit 2089cb6

Browse files
Added ToString::RESULT - an associated type with a default.
1 parent 9e7e06a commit 2089cb6

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
// - cannot specialize on predicate `[(); _] well-formed`
192192
// - cannot specialize on predicate `the constant `core::alloc::co_alloc_metadata_num_slots::<A>()` can be evaluated`
193193
//#![feature(min_specialization)]
194+
#![feature(associated_type_defaults)]
194195
#![feature(specialization)]
195196
#![feature(negative_impls)]
196197
#![feature(never_type)]

library/alloc/src/string.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -2682,6 +2682,10 @@ pub trait ToString<const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()}>
26822682
where
26832683
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
26842684
{
2685+
/// NOT for public use. Do not override. This exists to woraround (current) limitations of const generic defaults.
2686+
#[unstable(feature = "global_co_alloc", issue = "none")]
2687+
type RESULT = String<COOP_PREFERRED>;
2688+
26852689
/// Converts the given value to a `String`.
26862690
///
26872691
/// # Examples
@@ -2696,7 +2700,7 @@ where
26962700
/// ```
26972701
#[rustc_conversion_suggestion]
26982702
#[stable(feature = "rust1", since = "1.0.0")]
2699-
fn to_string(&self) -> String<COOP_PREFERRED>;
2703+
fn to_string(&self) -> Self::RESULT;
27002704
}
27012705

27022706
/// # Panics
@@ -2728,18 +2732,24 @@ where
27282732

27292733
#[cfg(not(no_global_oom_handling))]
27302734
#[stable(feature = "char_to_string_specialization", since = "1.46.0")]
2731-
impl ToString for char {
2735+
impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for char
2736+
where
2737+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
2738+
{
27322739
#[inline]
2733-
fn to_string(&self) -> String {
2740+
fn to_string(&self) -> String<COOP_PREFERRED> {
27342741
String::from(self.encode_utf8(&mut [0; 4]))
27352742
}
27362743
}
27372744

27382745
#[cfg(not(no_global_oom_handling))]
27392746
#[stable(feature = "bool_to_string_specialization", since = "CURRENT_RUSTC_VERSION")]
2740-
impl ToString for bool {
2747+
impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for bool
2748+
where
2749+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
2750+
{
27412751
#[inline]
2742-
fn to_string(&self) -> String {
2752+
fn to_string(&self) -> String<COOP_PREFERRED> {
27432753
String::from(if *self { "true" } else { "false" })
27442754
}
27452755
}
@@ -3291,7 +3301,7 @@ where
32913301
/// assert_eq!("a", &s[..]);
32923302
/// ```
32933303
#[inline]
3294-
fn from(c: char) -> Self {
3304+
fn from(c: char) -> String<COOP_PREFERRED> {
32953305
c.to_string()
32963306
}
32973307
}

0 commit comments

Comments
 (0)