about summary refs log tree commit diff
path: root/library/core
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2024-12-21 20:25:43 +0000
committerEsteban Küber <esteban@kuber.com.ar>2025-07-07 22:09:37 +0000
commitc3301503b97cd6b768a944661b81a92994e9db00 (patch)
tree6beacc787162110dba8aeab9c73034d261d6b128 /library/core
parent2f8eeb2bba86b8f457ec602c578473c711f85628 (diff)
downloadrust-c3301503b97cd6b768a944661b81a92994e9db00.tar.gz
rust-c3301503b97cd6b768a944661b81a92994e9db00.zip
Make `Default` const and add some `const Default` impls
Full list of `impl const Default` types:

- ()
- bool
- char
- Cell
- 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>
Diffstat (limited to 'library/core')
-rw-r--r--library/core/src/cell.rs12
-rw-r--r--library/core/src/default.rs5
-rw-r--r--library/core/src/iter/sources/empty.rs3
-rw-r--r--library/core/src/marker.rs3
-rw-r--r--library/core/src/option.rs3
-rw-r--r--library/core/src/ptr/alignment.rs3
-rw-r--r--library/core/src/slice/mod.rs6
-rw-r--r--library/core/src/str/mod.rs6
8 files changed, 28 insertions, 13 deletions
diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs
index dfed8a00b7d..f7ea1f37a39 100644
--- a/library/core/src/cell.rs
+++ b/library/core/src/cell.rs
@@ -333,7 +333,8 @@ impl<T: Copy> Clone for Cell<T> {
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<T: Default> Default for Cell<T> {
+#[rustc_const_unstable(feature = "const_default", issue = "67792")]
+impl<T: ~const Default> const Default for Cell<T> {
     /// Creates a `Cell<T>`, with the `Default` value for T.
     #[inline]
     fn default() -> Cell<T> {
@@ -1323,7 +1324,8 @@ impl<T: Clone> Clone for RefCell<T> {
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<T: Default> Default for RefCell<T> {
+#[rustc_const_unstable(feature = "const_default", issue = "67792")]
+impl<T: ~const Default> const Default for RefCell<T> {
     /// Creates a `RefCell<T>`, with the `Default` value for T.
     #[inline]
     fn default() -> RefCell<T> {
@@ -2330,7 +2332,8 @@ impl<T: ?Sized> UnsafeCell<T> {
 }
 
 #[stable(feature = "unsafe_cell_default", since = "1.10.0")]
-impl<T: Default> Default for UnsafeCell<T> {
+#[rustc_const_unstable(feature = "const_default", issue = "67792")]
+impl<T: ~const Default> const Default for UnsafeCell<T> {
     /// Creates an `UnsafeCell`, with the `Default` value for T.
     fn default() -> UnsafeCell<T> {
         UnsafeCell::new(Default::default())
@@ -2434,7 +2437,8 @@ impl<T: ?Sized> SyncUnsafeCell<T> {
 }
 
 #[unstable(feature = "sync_unsafe_cell", issue = "95439")]
-impl<T: Default> Default for SyncUnsafeCell<T> {
+#[rustc_const_unstable(feature = "const_default", issue = "67792")]
+impl<T: ~const Default> const Default for SyncUnsafeCell<T> {
     /// Creates an `SyncUnsafeCell`, with the `Default` value for T.
     fn default() -> SyncUnsafeCell<T> {
         SyncUnsafeCell::new(Default::default())
diff --git a/library/core/src/default.rs b/library/core/src/default.rs
index 0a15cedfb55..4d108b0c18e 100644
--- a/library/core/src/default.rs
+++ b/library/core/src/default.rs
@@ -103,6 +103,8 @@ use crate::ascii::Char as AsciiChar;
 /// ```
 #[rustc_diagnostic_item = "Default"]
 #[stable(feature = "rust1", since = "1.0.0")]
+#[const_trait]
+#[rustc_const_unstable(feature = "const_default", issue = "67792")]
 pub trait Default: Sized {
     /// Returns the "default value" for a type.
     ///
@@ -149,7 +151,8 @@ pub macro Default($item:item) {
 macro_rules! default_impl {
     ($t:ty, $v:expr, $doc:tt) => {
         #[stable(feature = "rust1", since = "1.0.0")]
-        impl Default for $t {
+        #[rustc_const_unstable(feature = "const_default", issue = "67792")]
+        impl const Default for $t {
             #[inline(always)]
             #[doc = $doc]
             fn default() -> $t {
diff --git a/library/core/src/iter/sources/empty.rs b/library/core/src/iter/sources/empty.rs
index 3c3acceded8..309962ab70c 100644
--- a/library/core/src/iter/sources/empty.rs
+++ b/library/core/src/iter/sources/empty.rs
@@ -81,7 +81,8 @@ impl<T> Clone for Empty<T> {
 // not #[derive] because that adds a Default bound on T,
 // which isn't necessary.
 #[stable(feature = "iter_empty", since = "1.2.0")]
-impl<T> Default for Empty<T> {
+#[rustc_const_unstable(feature = "const_default", issue = "67792")]
+impl<T> const Default for Empty<T> {
     fn default() -> Empty<T> {
         Empty(marker::PhantomData)
     }
diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs
index ccbd2b00cfd..2aeb0b0c31e 100644
--- a/library/core/src/marker.rs
+++ b/library/core/src/marker.rs
@@ -858,7 +858,8 @@ impl<T: PointeeSized> Clone for PhantomData<T> {
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<T: PointeeSized> Default for PhantomData<T> {
+#[rustc_const_unstable(feature = "const_default", issue = "67792")]
+impl<T: PointeeSized> const Default for PhantomData<T> {
     fn default() -> Self {
         Self
     }
diff --git a/library/core/src/option.rs b/library/core/src/option.rs
index f2a1e901188..38eb2662b34 100644
--- a/library/core/src/option.rs
+++ b/library/core/src/option.rs
@@ -2111,7 +2111,8 @@ where
 impl<T> crate::clone::UseCloned for Option<T> where T: crate::clone::UseCloned {}
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<T> Default for Option<T> {
+#[rustc_const_unstable(feature = "const_default", issue = "67792")]
+impl<T> const Default for Option<T> {
     /// Returns [`None`][Option::None].
     ///
     /// # Examples
diff --git a/library/core/src/ptr/alignment.rs b/library/core/src/ptr/alignment.rs
index 3e66e271f03..304cde05af9 100644
--- a/library/core/src/ptr/alignment.rs
+++ b/library/core/src/ptr/alignment.rs
@@ -230,7 +230,8 @@ impl hash::Hash for Alignment {
 
 /// Returns [`Alignment::MIN`], which is valid for any type.
 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
-impl Default for Alignment {
+#[rustc_const_unstable(feature = "const_default", issue = "67792")]
+impl const Default for Alignment {
     fn default() -> Alignment {
         Alignment::MIN
     }
diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs
index dc09ba8d788..479fe0f1554 100644
--- a/library/core/src/slice/mod.rs
+++ b/library/core/src/slice/mod.rs
@@ -5158,7 +5158,8 @@ where
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<T> Default for &[T] {
+#[rustc_const_unstable(feature = "const_default", issue = "67792")]
+impl<T> const Default for &[T] {
     /// Creates an empty slice.
     fn default() -> Self {
         &[]
@@ -5166,7 +5167,8 @@ impl<T> Default for &[T] {
 }
 
 #[stable(feature = "mut_slice_default", since = "1.5.0")]
-impl<T> Default for &mut [T] {
+#[rustc_const_unstable(feature = "const_default", issue = "67792")]
+impl<T> const Default for &mut [T] {
     /// Creates a mutable empty slice.
     fn default() -> Self {
         &mut []
diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs
index fe64132ff22..32a22988175 100644
--- a/library/core/src/str/mod.rs
+++ b/library/core/src/str/mod.rs
@@ -3072,7 +3072,8 @@ impl AsRef<[u8]> for str {
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl Default for &str {
+#[rustc_const_unstable(feature = "const_default", issue = "67792")]
+impl const Default for &str {
     /// Creates an empty str
     #[inline]
     fn default() -> Self {
@@ -3081,7 +3082,8 @@ impl Default for &str {
 }
 
 #[stable(feature = "default_mut_str", since = "1.28.0")]
-impl Default for &mut str {
+#[rustc_const_unstable(feature = "const_default", issue = "67792")]
+impl const Default for &mut str {
     /// Creates an empty mutable str
     #[inline]
     fn default() -> Self {