about summary refs log tree commit diff
path: root/library/core/src/array
diff options
context:
space:
mode:
authorKevin Reid <kpreid@switchb.org>2024-05-12 21:37:53 -0700
committerKevin Reid <kpreid@switchb.org>2024-06-24 10:23:50 -0700
commit13fca73f4937fe4bb318321525a0fd666e9da16e (patch)
tree20871f0b5da54ed5dc5b62f1e447defe33cda445 /library/core/src/array
parentd371d17496f2ce3a56da76aa083f4ef157572c20 (diff)
downloadrust-13fca73f4937fe4bb318321525a0fd666e9da16e.tar.gz
rust-13fca73f4937fe4bb318321525a0fd666e9da16e.zip
Replace `MaybeUninit::uninit_array()` with array repeat expression.
This is possible now that inline const blocks are stable; the idea was
even mentioned as an alternative when `uninit_array()` was added:
<https://github.com/rust-lang/rust/pull/65580#issuecomment-544200681>

> if it’s stabilized soon enough maybe it’s not worth having a
> standard library method that will be replaceable with
> `let buffer = [MaybeUninit::<T>::uninit(); $N];`

Const array repetition and inline const blocks are now stable (in the
next release), so that circumstance has come to pass, and we no longer
have reason to want `uninit_array()` other than convenience. Therefore,
let’s evaluate the inconvenience by not using `uninit_array()` in
the standard library, before potentially deleting it entirely.
Diffstat (limited to 'library/core/src/array')
-rw-r--r--library/core/src/array/iter.rs8
-rw-r--r--library/core/src/array/mod.rs4
2 files changed, 6 insertions, 6 deletions
diff --git a/library/core/src/array/iter.rs b/library/core/src/array/iter.rs
index b314d0536a3..3585bf07b59 100644
--- a/library/core/src/array/iter.rs
+++ b/library/core/src/array/iter.rs
@@ -101,7 +101,6 @@ impl<T, const N: usize> IntoIter<T, N> {
     /// ```
     /// #![feature(array_into_iter_constructors)]
     /// #![feature(maybe_uninit_uninit_array_transpose)]
-    /// #![feature(maybe_uninit_uninit_array)]
     /// use std::array::IntoIter;
     /// use std::mem::MaybeUninit;
     ///
@@ -111,7 +110,7 @@ impl<T, const N: usize> IntoIter<T, N> {
     /// fn next_chunk<T: Copy, const N: usize>(
     ///     it: &mut impl Iterator<Item = T>,
     /// ) -> Result<[T; N], IntoIter<T, N>> {
-    ///     let mut buffer = MaybeUninit::uninit_array();
+    ///     let mut buffer = [const { MaybeUninit::uninit() }; N];
     ///     let mut i = 0;
     ///     while i < N {
     ///         match it.next() {
@@ -203,7 +202,7 @@ impl<T, const N: usize> IntoIter<T, N> {
     #[unstable(feature = "array_into_iter_constructors", issue = "91583")]
     #[rustc_const_unstable(feature = "const_array_into_iter_constructors", issue = "91583")]
     pub const fn empty() -> Self {
-        let buffer = MaybeUninit::uninit_array();
+        let buffer = [const { MaybeUninit::uninit() }; N];
         let initialized = 0..0;
 
         // SAFETY: We're telling it that none of the elements are initialized,
@@ -405,7 +404,8 @@ impl<T: Clone, const N: usize> Clone for IntoIter<T, N> {
     fn clone(&self) -> Self {
         // Note, we don't really need to match the exact same alive range, so
         // we can just clone into offset 0 regardless of where `self` is.
-        let mut new = Self { data: MaybeUninit::uninit_array(), alive: IndexRange::zero_to(0) };
+        let mut new =
+            Self { data: [const { MaybeUninit::uninit() }; N], alive: IndexRange::zero_to(0) };
 
         // Clone all alive elements.
         for (src, dst) in iter::zip(self.as_slice(), &mut new.data) {
diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs
index 3e4eadbb7c9..8285c64ed29 100644
--- a/library/core/src/array/mod.rs
+++ b/library/core/src/array/mod.rs
@@ -127,7 +127,7 @@ where
     R: Try,
     R::Residual: Residual<[R::Output; N]>,
 {
-    let mut array = MaybeUninit::uninit_array::<N>();
+    let mut array = [const { MaybeUninit::uninit() }; N];
     match try_from_fn_erased(&mut array, cb) {
         ControlFlow::Break(r) => FromResidual::from_residual(r),
         ControlFlow::Continue(()) => {
@@ -918,7 +918,7 @@ impl<T> Drop for Guard<'_, T> {
 pub(crate) fn iter_next_chunk<T, const N: usize>(
     iter: &mut impl Iterator<Item = T>,
 ) -> Result<[T; N], IntoIter<T, N>> {
-    let mut array = MaybeUninit::uninit_array::<N>();
+    let mut array = [const { MaybeUninit::uninit() }; N];
     let r = iter_next_chunk_erased(&mut array, iter);
     match r {
         Ok(()) => {