Skip to content

Commit fc9e5c4

Browse files
committed
Make Default const and add some const Default impls
Full list of `impl const Default` types: - () - bool - char - std::ascii::Char - usize - u8 - u16 - u32 - u64 - u128 - i8 - i16 - i32 - i64 - i128 - f16 - f32 - f64 - f128 - std::marker::PhantomData<T> - Option<T> - std::iter::Empty<T> - std::ptr::Alignment - &[T] - &mut [T] - &str - &mut str - String - Vec<T>
1 parent 2801f9a commit fc9e5c4

26 files changed

+172
-146
lines changed

library/alloc/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@
107107
#![feature(char_max_len)]
108108
#![feature(clone_to_uninit)]
109109
#![feature(coerce_unsized)]
110+
#![feature(const_default)]
110111
#![feature(const_eval_select)]
111112
#![feature(const_heap)]
113+
#![feature(const_trait_impl)]
112114
#![feature(core_intrinsics)]
113115
#![feature(deprecated_suggestion)]
114116
#![feature(deref_pure_trait)]

library/alloc/src/string.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2611,7 +2611,8 @@ impl_eq! { Cow<'a, str>, &'b str }
26112611
impl_eq! { Cow<'a, str>, String }
26122612

26132613
#[stable(feature = "rust1", since = "1.0.0")]
2614-
impl Default for String {
2614+
#[rustc_const_unstable(feature = "const_default", issue = "none")]
2615+
impl const Default for String {
26152616
/// Creates an empty `String`.
26162617
#[inline]
26172618
fn default() -> String {

library/alloc/src/vec/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3840,7 +3840,8 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec<T, A> {
38403840
}
38413841

38423842
#[stable(feature = "rust1", since = "1.0.0")]
3843-
impl<T> Default for Vec<T> {
3843+
#[rustc_const_unstable(feature = "const_default", issue = "none")]
3844+
impl<T> const Default for Vec<T> {
38443845
/// Creates an empty `Vec<T>`.
38453846
///
38463847
/// The vector will not allocate until elements are pushed onto it.

library/core/src/default.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ use crate::ascii::Char as AsciiChar;
103103
/// ```
104104
#[rustc_diagnostic_item = "Default"]
105105
#[stable(feature = "rust1", since = "1.0.0")]
106+
#[const_trait]
107+
#[rustc_trivial_field_reads]
108+
#[rustc_const_unstable(feature = "const_default", issue = "none")]
106109
pub trait Default: Sized {
107110
/// Returns the "default value" for a type.
108111
///
@@ -149,7 +152,8 @@ pub macro Default($item:item) {
149152
macro_rules! default_impl {
150153
($t:ty, $v:expr, $doc:tt) => {
151154
#[stable(feature = "rust1", since = "1.0.0")]
152-
impl Default for $t {
155+
#[rustc_const_unstable(feature = "const_default", issue = "none")]
156+
impl const Default for $t {
153157
#[inline(always)]
154158
#[doc = $doc]
155159
fn default() -> $t {

library/core/src/iter/sources/empty.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ impl<T> Clone for Empty<T> {
8181
// not #[derive] because that adds a Default bound on T,
8282
// which isn't necessary.
8383
#[stable(feature = "iter_empty", since = "1.2.0")]
84-
impl<T> Default for Empty<T> {
84+
#[rustc_const_unstable(feature = "const_default", issue = "none")]
85+
impl<T> const Default for Empty<T> {
8586
fn default() -> Empty<T> {
8687
Empty(marker::PhantomData)
8788
}

library/core/src/marker.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,8 @@ impl<T: PointeeSized> Clone for PhantomData<T> {
855855
}
856856

857857
#[stable(feature = "rust1", since = "1.0.0")]
858-
impl<T: PointeeSized> Default for PhantomData<T> {
858+
#[rustc_const_unstable(feature = "const_default", issue = "none")]
859+
impl<T: PointeeSized> const Default for PhantomData<T> {
859860
fn default() -> Self {
860861
Self
861862
}

library/core/src/option.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2111,7 +2111,8 @@ where
21112111
impl<T> crate::clone::UseCloned for Option<T> where T: crate::clone::UseCloned {}
21122112

21132113
#[stable(feature = "rust1", since = "1.0.0")]
2114-
impl<T> Default for Option<T> {
2114+
#[rustc_const_unstable(feature = "const_default", issue = "none")]
2115+
impl<T> const Default for Option<T> {
21152116
/// Returns [`None`][Option::None].
21162117
///
21172118
/// # Examples

library/core/src/ptr/alignment.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ impl hash::Hash for Alignment {
230230

231231
/// Returns [`Alignment::MIN`], which is valid for any type.
232232
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
233-
impl Default for Alignment {
233+
#[rustc_const_unstable(feature = "const_default", issue = "none")]
234+
impl const Default for Alignment {
234235
fn default() -> Alignment {
235236
Alignment::MIN
236237
}

library/core/src/slice/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5158,15 +5158,17 @@ where
51585158
}
51595159

51605160
#[stable(feature = "rust1", since = "1.0.0")]
5161-
impl<T> Default for &[T] {
5161+
#[rustc_const_unstable(feature = "const_default", issue = "none")]
5162+
impl<T> const Default for &[T] {
51625163
/// Creates an empty slice.
51635164
fn default() -> Self {
51645165
&[]
51655166
}
51665167
}
51675168

51685169
#[stable(feature = "mut_slice_default", since = "1.5.0")]
5169-
impl<T> Default for &mut [T] {
5170+
#[rustc_const_unstable(feature = "const_default", issue = "none")]
5171+
impl<T> const Default for &mut [T] {
51705172
/// Creates a mutable empty slice.
51715173
fn default() -> Self {
51725174
&mut []

library/core/src/str/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3072,7 +3072,8 @@ impl AsRef<[u8]> for str {
30723072
}
30733073

30743074
#[stable(feature = "rust1", since = "1.0.0")]
3075-
impl Default for &str {
3075+
#[rustc_const_unstable(feature = "const_default", issue = "none")]
3076+
impl const Default for &str {
30763077
/// Creates an empty str
30773078
#[inline]
30783079
fn default() -> Self {
@@ -3081,7 +3082,8 @@ impl Default for &str {
30813082
}
30823083

30833084
#[stable(feature = "default_mut_str", since = "1.28.0")]
3084-
impl Default for &mut str {
3085+
#[rustc_const_unstable(feature = "const_default", issue = "none")]
3086+
impl const Default for &mut str {
30853087
/// Creates an empty mutable str
30863088
#[inline]
30873089
fn default() -> Self {

0 commit comments

Comments
 (0)