core/array/
mod.rs

1//! Utilities for the array primitive type.
2//!
3//! *[See also the array primitive type](array).*
4
5#![stable(feature = "core_array", since = "1.35.0")]
6
7use crate::borrow::{Borrow, BorrowMut};
8use crate::cmp::Ordering;
9use crate::convert::Infallible;
10use crate::error::Error;
11use crate::fmt;
12use crate::hash::{self, Hash};
13use crate::intrinsics::transmute_unchecked;
14use crate::iter::{UncheckedIterator, repeat_n};
15use crate::mem::{self, MaybeUninit};
16use crate::ops::{
17    ChangeOutputType, ControlFlow, FromResidual, Index, IndexMut, NeverShortCircuit, Residual, Try,
18};
19use crate::ptr::{null, null_mut};
20use crate::slice::{Iter, IterMut};
21
22mod ascii;
23mod drain;
24mod equality;
25mod iter;
26
27pub(crate) use drain::drain_array_with;
28#[stable(feature = "array_value_iter", since = "1.51.0")]
29pub use iter::IntoIter;
30
31/// Creates an array of type `[T; N]` by repeatedly cloning a value.
32///
33/// This is the same as `[val; N]`, but it also works for types that do not
34/// implement [`Copy`].
35///
36/// The provided value will be used as an element of the resulting array and
37/// will be cloned N - 1 times to fill up the rest. If N is zero, the value
38/// will be dropped.
39///
40/// # Example
41///
42/// Creating multiple copies of a `String`:
43/// ```rust
44/// #![feature(array_repeat)]
45///
46/// use std::array;
47///
48/// let string = "Hello there!".to_string();
49/// let strings = array::repeat(string);
50/// assert_eq!(strings, ["Hello there!", "Hello there!"]);
51/// ```
52#[inline]
53#[unstable(feature = "array_repeat", issue = "126695")]
54pub fn repeat<T: Clone, const N: usize>(val: T) -> [T; N] {
55    from_trusted_iterator(repeat_n(val, N))
56}
57
58/// Creates an array where each element is produced by calling `f` with
59/// that element's index while walking forward through the array.
60///
61/// This is essentially the same as writing
62/// ```text
63/// [f(0), f(1), f(2), …, f(N - 2), f(N - 1)]
64/// ```
65/// and is similar to `(0..i).map(f)`, just for arrays not iterators.
66///
67/// If `N == 0`, this produces an empty array without ever calling `f`.
68///
69/// # Example
70///
71/// ```rust
72/// // type inference is helping us here, the way `from_fn` knows how many
73/// // elements to produce is the length of array down there: only arrays of
74/// // equal lengths can be compared, so the const generic parameter `N` is
75/// // inferred to be 5, thus creating array of 5 elements.
76///
77/// let array = core::array::from_fn(|i| i);
78/// // indexes are:    0  1  2  3  4
79/// assert_eq!(array, [0, 1, 2, 3, 4]);
80///
81/// let array2: [usize; 8] = core::array::from_fn(|i| i * 2);
82/// // indexes are:     0  1  2  3  4  5   6   7
83/// assert_eq!(array2, [0, 2, 4, 6, 8, 10, 12, 14]);
84///
85/// let bool_arr = core::array::from_fn::<_, 5, _>(|i| i % 2 == 0);
86/// // indexes are:       0     1      2     3      4
87/// assert_eq!(bool_arr, [true, false, true, false, true]);
88/// ```
89///
90/// You can also capture things, for example to create an array full of clones
91/// where you can't just use `[item; N]` because it's not `Copy`:
92/// ```
93/// # // TBH `array::repeat` would be better for this, but it's not stable yet.
94/// let my_string = String::from("Hello");
95/// let clones: [String; 42] = std::array::from_fn(|_| my_string.clone());
96/// assert!(clones.iter().all(|x| *x == my_string));
97/// ```
98///
99/// The array is generated in ascending index order, starting from the front
100/// and going towards the back, so you can use closures with mutable state:
101/// ```
102/// let mut state = 1;
103/// let a = std::array::from_fn(|_| { let x = state; state *= 2; x });
104/// assert_eq!(a, [1, 2, 4, 8, 16, 32]);
105/// ```
106#[inline]
107#[stable(feature = "array_from_fn", since = "1.63.0")]
108pub fn from_fn<T, const N: usize, F>(f: F) -> [T; N]
109where
110    F: FnMut(usize) -> T,
111{
112    try_from_fn(NeverShortCircuit::wrap_mut_1(f)).0
113}
114
115/// Creates an array `[T; N]` where each fallible array element `T` is returned by the `cb` call.
116/// Unlike [`from_fn`], where the element creation can't fail, this version will return an error
117/// if any element creation was unsuccessful.
118///
119/// The return type of this function depends on the return type of the closure.
120/// If you return `Result<T, E>` from the closure, you'll get a `Result<[T; N], E>`.
121/// If you return `Option<T>` from the closure, you'll get an `Option<[T; N]>`.
122///
123/// # Arguments
124///
125/// * `cb`: Callback where the passed argument is the current array index.
126///
127/// # Example
128///
129/// ```rust
130/// #![feature(array_try_from_fn)]
131///
132/// let array: Result<[u8; 5], _> = std::array::try_from_fn(|i| i.try_into());
133/// assert_eq!(array, Ok([0, 1, 2, 3, 4]));
134///
135/// let array: Result<[i8; 200], _> = std::array::try_from_fn(|i| i.try_into());
136/// assert!(array.is_err());
137///
138/// let array: Option<[_; 4]> = std::array::try_from_fn(|i| i.checked_add(100));
139/// assert_eq!(array, Some([100, 101, 102, 103]));
140///
141/// let array: Option<[_; 4]> = std::array::try_from_fn(|i| i.checked_sub(100));
142/// assert_eq!(array, None);
143/// ```
144#[inline]
145#[unstable(feature = "array_try_from_fn", issue = "89379")]
146pub fn try_from_fn<R, const N: usize, F>(cb: F) -> ChangeOutputType<R, [R::Output; N]>
147where
148    F: FnMut(usize) -> R,
149    R: Try,
150    R::Residual: Residual<[R::Output; N]>,
151{
152    let mut array = [const { MaybeUninit::uninit() }; N];
153    match try_from_fn_erased(&mut array, cb) {
154        ControlFlow::Break(r) => FromResidual::from_residual(r),
155        ControlFlow::Continue(()) => {
156            // SAFETY: All elements of the array were populated.
157            try { unsafe { MaybeUninit::array_assume_init(array) } }
158        }
159    }
160}
161
162/// Converts a reference to `T` into a reference to an array of length 1 (without copying).
163#[stable(feature = "array_from_ref", since = "1.53.0")]
164#[rustc_const_stable(feature = "const_array_from_ref_shared", since = "1.63.0")]
165pub const fn from_ref<T>(s: &T) -> &[T; 1] {
166    // SAFETY: Converting `&T` to `&[T; 1]` is sound.
167    unsafe { &*(s as *const T).cast::<[T; 1]>() }
168}
169
170/// Converts a mutable reference to `T` into a mutable reference to an array of length 1 (without copying).
171#[stable(feature = "array_from_ref", since = "1.53.0")]
172#[rustc_const_stable(feature = "const_array_from_ref", since = "1.83.0")]
173pub const fn from_mut<T>(s: &mut T) -> &mut [T; 1] {
174    // SAFETY: Converting `&mut T` to `&mut [T; 1]` is sound.
175    unsafe { &mut *(s as *mut T).cast::<[T; 1]>() }
176}
177
178/// The error type returned when a conversion from a slice to an array fails.
179#[stable(feature = "try_from", since = "1.34.0")]
180#[derive(Debug, Copy, Clone)]
181pub struct TryFromSliceError(());
182
183#[stable(feature = "core_array", since = "1.35.0")]
184impl fmt::Display for TryFromSliceError {
185    #[inline]
186    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
187        "could not convert slice to array".fmt(f)
188    }
189}
190
191#[stable(feature = "try_from", since = "1.34.0")]
192impl Error for TryFromSliceError {}
193
194#[stable(feature = "try_from_slice_error", since = "1.36.0")]
195#[rustc_const_unstable(feature = "const_try", issue = "74935")]
196impl const From<Infallible> for TryFromSliceError {
197    fn from(x: Infallible) -> TryFromSliceError {
198        match x {}
199    }
200}
201
202#[stable(feature = "rust1", since = "1.0.0")]
203impl<T, const N: usize> AsRef<[T]> for [T; N] {
204    #[inline]
205    fn as_ref(&self) -> &[T] {
206        &self[..]
207    }
208}
209
210#[stable(feature = "rust1", since = "1.0.0")]
211impl<T, const N: usize> AsMut<[T]> for [T; N] {
212    #[inline]
213    fn as_mut(&mut self) -> &mut [T] {
214        &mut self[..]
215    }
216}
217
218#[stable(feature = "array_borrow", since = "1.4.0")]
219impl<T, const N: usize> Borrow<[T]> for [T; N] {
220    fn borrow(&self) -> &[T] {
221        self
222    }
223}
224
225#[stable(feature = "array_borrow", since = "1.4.0")]
226impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
227    fn borrow_mut(&mut self) -> &mut [T] {
228        self
229    }
230}
231
232/// Tries to create an array `[T; N]` by copying from a slice `&[T]`.
233/// Succeeds if `slice.len() == N`.
234///
235/// ```
236/// let bytes: [u8; 3] = [1, 0, 2];
237///
238/// let bytes_head: [u8; 2] = <[u8; 2]>::try_from(&bytes[0..2]).unwrap();
239/// assert_eq!(1, u16::from_le_bytes(bytes_head));
240///
241/// let bytes_tail: [u8; 2] = bytes[1..3].try_into().unwrap();
242/// assert_eq!(512, u16::from_le_bytes(bytes_tail));
243/// ```
244#[stable(feature = "try_from", since = "1.34.0")]
245impl<T, const N: usize> TryFrom<&[T]> for [T; N]
246where
247    T: Copy,
248{
249    type Error = TryFromSliceError;
250
251    #[inline]
252    fn try_from(slice: &[T]) -> Result<[T; N], TryFromSliceError> {
253        <&Self>::try_from(slice).copied()
254    }
255}
256
257/// Tries to create an array `[T; N]` by copying from a mutable slice `&mut [T]`.
258/// Succeeds if `slice.len() == N`.
259///
260/// ```
261/// let mut bytes: [u8; 3] = [1, 0, 2];
262///
263/// let bytes_head: [u8; 2] = <[u8; 2]>::try_from(&mut bytes[0..2]).unwrap();
264/// assert_eq!(1, u16::from_le_bytes(bytes_head));
265///
266/// let bytes_tail: [u8; 2] = (&mut bytes[1..3]).try_into().unwrap();
267/// assert_eq!(512, u16::from_le_bytes(bytes_tail));
268/// ```
269#[stable(feature = "try_from_mut_slice_to_array", since = "1.59.0")]
270impl<T, const N: usize> TryFrom<&mut [T]> for [T; N]
271where
272    T: Copy,
273{
274    type Error = TryFromSliceError;
275
276    #[inline]
277    fn try_from(slice: &mut [T]) -> Result<[T; N], TryFromSliceError> {
278        <Self>::try_from(&*slice)
279    }
280}
281
282/// Tries to create an array ref `&[T; N]` from a slice ref `&[T]`. Succeeds if
283/// `slice.len() == N`.
284///
285/// ```
286/// let bytes: [u8; 3] = [1, 0, 2];
287///
288/// let bytes_head: &[u8; 2] = <&[u8; 2]>::try_from(&bytes[0..2]).unwrap();
289/// assert_eq!(1, u16::from_le_bytes(*bytes_head));
290///
291/// let bytes_tail: &[u8; 2] = bytes[1..3].try_into().unwrap();
292/// assert_eq!(512, u16::from_le_bytes(*bytes_tail));
293/// ```
294#[stable(feature = "try_from", since = "1.34.0")]
295impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] {
296    type Error = TryFromSliceError;
297
298    #[inline]
299    fn try_from(slice: &'a [T]) -> Result<&'a [T; N], TryFromSliceError> {
300        slice.as_array().ok_or(TryFromSliceError(()))
301    }
302}
303
304/// Tries to create a mutable array ref `&mut [T; N]` from a mutable slice ref
305/// `&mut [T]`. Succeeds if `slice.len() == N`.
306///
307/// ```
308/// let mut bytes: [u8; 3] = [1, 0, 2];
309///
310/// let bytes_head: &mut [u8; 2] = <&mut [u8; 2]>::try_from(&mut bytes[0..2]).unwrap();
311/// assert_eq!(1, u16::from_le_bytes(*bytes_head));
312///
313/// let bytes_tail: &mut [u8; 2] = (&mut bytes[1..3]).try_into().unwrap();
314/// assert_eq!(512, u16::from_le_bytes(*bytes_tail));
315/// ```
316#[stable(feature = "try_from", since = "1.34.0")]
317impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N] {
318    type Error = TryFromSliceError;
319
320    #[inline]
321    fn try_from(slice: &'a mut [T]) -> Result<&'a mut [T; N], TryFromSliceError> {
322        slice.as_mut_array().ok_or(TryFromSliceError(()))
323    }
324}
325
326/// The hash of an array is the same as that of the corresponding slice,
327/// as required by the `Borrow` implementation.
328///
329/// ```
330/// use std::hash::BuildHasher;
331///
332/// let b = std::hash::RandomState::new();
333/// let a: [u8; 3] = [0xa8, 0x3c, 0x09];
334/// let s: &[u8] = &[0xa8, 0x3c, 0x09];
335/// assert_eq!(b.hash_one(a), b.hash_one(s));
336/// ```
337#[stable(feature = "rust1", since = "1.0.0")]
338impl<T: Hash, const N: usize> Hash for [T; N] {
339    fn hash<H: hash::Hasher>(&self, state: &mut H) {
340        Hash::hash(&self[..], state)
341    }
342}
343
344#[stable(feature = "rust1", since = "1.0.0")]
345impl<T: fmt::Debug, const N: usize> fmt::Debug for [T; N] {
346    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
347        fmt::Debug::fmt(&&self[..], f)
348    }
349}
350
351#[stable(feature = "rust1", since = "1.0.0")]
352impl<'a, T, const N: usize> IntoIterator for &'a [T; N] {
353    type Item = &'a T;
354    type IntoIter = Iter<'a, T>;
355
356    fn into_iter(self) -> Iter<'a, T> {
357        self.iter()
358    }
359}
360
361#[stable(feature = "rust1", since = "1.0.0")]
362impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N] {
363    type Item = &'a mut T;
364    type IntoIter = IterMut<'a, T>;
365
366    fn into_iter(self) -> IterMut<'a, T> {
367        self.iter_mut()
368    }
369}
370
371#[stable(feature = "index_trait_on_arrays", since = "1.50.0")]
372#[rustc_const_unstable(feature = "const_index", issue = "143775")]
373impl<T, I, const N: usize> const Index<I> for [T; N]
374where
375    [T]: [const] Index<I>,
376{
377    type Output = <[T] as Index<I>>::Output;
378
379    #[inline]
380    fn index(&self, index: I) -> &Self::Output {
381        Index::index(self as &[T], index)
382    }
383}
384
385#[stable(feature = "index_trait_on_arrays", since = "1.50.0")]
386#[rustc_const_unstable(feature = "const_index", issue = "143775")]
387impl<T, I, const N: usize> const IndexMut<I> for [T; N]
388where
389    [T]: [const] IndexMut<I>,
390{
391    #[inline]
392    fn index_mut(&mut self, index: I) -> &mut Self::Output {
393        IndexMut::index_mut(self as &mut [T], index)
394    }
395}
396
397/// Implements comparison of arrays [lexicographically](Ord#lexicographical-comparison).
398#[stable(feature = "rust1", since = "1.0.0")]
399impl<T: PartialOrd, const N: usize> PartialOrd for [T; N] {
400    #[inline]
401    fn partial_cmp(&self, other: &[T; N]) -> Option<Ordering> {
402        PartialOrd::partial_cmp(&&self[..], &&other[..])
403    }
404    #[inline]
405    fn lt(&self, other: &[T; N]) -> bool {
406        PartialOrd::lt(&&self[..], &&other[..])
407    }
408    #[inline]
409    fn le(&self, other: &[T; N]) -> bool {
410        PartialOrd::le(&&self[..], &&other[..])
411    }
412    #[inline]
413    fn ge(&self, other: &[T; N]) -> bool {
414        PartialOrd::ge(&&self[..], &&other[..])
415    }
416    #[inline]
417    fn gt(&self, other: &[T; N]) -> bool {
418        PartialOrd::gt(&&self[..], &&other[..])
419    }
420}
421
422/// Implements comparison of arrays [lexicographically](Ord#lexicographical-comparison).
423#[stable(feature = "rust1", since = "1.0.0")]
424impl<T: Ord, const N: usize> Ord for [T; N] {
425    #[inline]
426    fn cmp(&self, other: &[T; N]) -> Ordering {
427        Ord::cmp(&&self[..], &&other[..])
428    }
429}
430
431#[stable(feature = "copy_clone_array_lib", since = "1.58.0")]
432impl<T: Copy, const N: usize> Copy for [T; N] {}
433
434#[stable(feature = "copy_clone_array_lib", since = "1.58.0")]
435impl<T: Clone, const N: usize> Clone for [T; N] {
436    #[inline]
437    fn clone(&self) -> Self {
438        SpecArrayClone::clone(self)
439    }
440
441    #[inline]
442    fn clone_from(&mut self, other: &Self) {
443        self.clone_from_slice(other);
444    }
445}
446
447trait SpecArrayClone: Clone {
448    fn clone<const N: usize>(array: &[Self; N]) -> [Self; N];
449}
450
451impl<T: Clone> SpecArrayClone for T {
452    #[inline]
453    default fn clone<const N: usize>(array: &[T; N]) -> [T; N] {
454        from_trusted_iterator(array.iter().cloned())
455    }
456}
457
458impl<T: Copy> SpecArrayClone for T {
459    #[inline]
460    fn clone<const N: usize>(array: &[T; N]) -> [T; N] {
461        *array
462    }
463}
464
465// The Default impls cannot be done with const generics because `[T; 0]` doesn't
466// require Default to be implemented, and having different impl blocks for
467// different numbers isn't supported yet.
468
469macro_rules! array_impl_default {
470    {$n:expr, $t:ident $($ts:ident)*} => {
471        #[stable(since = "1.4.0", feature = "array_default")]
472        impl<T> Default for [T; $n] where T: Default {
473            fn default() -> [T; $n] {
474                [$t::default(), $($ts::default()),*]
475            }
476        }
477        array_impl_default!{($n - 1), $($ts)*}
478    };
479    {$n:expr,} => {
480        #[stable(since = "1.4.0", feature = "array_default")]
481        impl<T> Default for [T; $n] {
482            fn default() -> [T; $n] { [] }
483        }
484    };
485}
486
487array_impl_default! {32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T}
488
489impl<T, const N: usize> [T; N] {
490    /// Returns an array of the same size as `self`, with function `f` applied to each element
491    /// in order.
492    ///
493    /// If you don't necessarily need a new fixed-size array, consider using
494    /// [`Iterator::map`] instead.
495    ///
496    ///
497    /// # Note on performance and stack usage
498    ///
499    /// Unfortunately, usages of this method are currently not always optimized
500    /// as well as they could be. This mainly concerns large arrays, as mapping
501    /// over small arrays seem to be optimized just fine. Also note that in
502    /// debug mode (i.e. without any optimizations), this method can use a lot
503    /// of stack space (a few times the size of the array or more).
504    ///
505    /// Therefore, in performance-critical code, try to avoid using this method
506    /// on large arrays or check the emitted code. Also try to avoid chained
507    /// maps (e.g. `arr.map(...).map(...)`).
508    ///
509    /// In many cases, you can instead use [`Iterator::map`] by calling `.iter()`
510    /// or `.into_iter()` on your array. `[T; N]::map` is only necessary if you
511    /// really need a new array of the same size as the result. Rust's lazy
512    /// iterators tend to get optimized very well.
513    ///
514    ///
515    /// # Examples
516    ///
517    /// ```
518    /// let x = [1, 2, 3];
519    /// let y = x.map(|v| v + 1);
520    /// assert_eq!(y, [2, 3, 4]);
521    ///
522    /// let x = [1, 2, 3];
523    /// let mut temp = 0;
524    /// let y = x.map(|v| { temp += 1; v * temp });
525    /// assert_eq!(y, [1, 4, 9]);
526    ///
527    /// let x = ["Ferris", "Bueller's", "Day", "Off"];
528    /// let y = x.map(|v| v.len());
529    /// assert_eq!(y, [6, 9, 3, 3]);
530    /// ```
531    #[must_use]
532    #[stable(feature = "array_map", since = "1.55.0")]
533    pub fn map<F, U>(self, f: F) -> [U; N]
534    where
535        F: FnMut(T) -> U,
536    {
537        self.try_map(NeverShortCircuit::wrap_mut_1(f)).0
538    }
539
540    /// A fallible function `f` applied to each element on array `self` in order to
541    /// return an array the same size as `self` or the first error encountered.
542    ///
543    /// The return type of this function depends on the return type of the closure.
544    /// If you return `Result<T, E>` from the closure, you'll get a `Result<[T; N], E>`.
545    /// If you return `Option<T>` from the closure, you'll get an `Option<[T; N]>`.
546    ///
547    /// # Examples
548    ///
549    /// ```
550    /// #![feature(array_try_map)]
551    ///
552    /// let a = ["1", "2", "3"];
553    /// let b = a.try_map(|v| v.parse::<u32>()).unwrap().map(|v| v + 1);
554    /// assert_eq!(b, [2, 3, 4]);
555    ///
556    /// let a = ["1", "2a", "3"];
557    /// let b = a.try_map(|v| v.parse::<u32>());
558    /// assert!(b.is_err());
559    ///
560    /// use std::num::NonZero;
561    ///
562    /// let z = [1, 2, 0, 3, 4];
563    /// assert_eq!(z.try_map(NonZero::new), None);
564    ///
565    /// let a = [1, 2, 3];
566    /// let b = a.try_map(NonZero::new);
567    /// let c = b.map(|x| x.map(NonZero::get));
568    /// assert_eq!(c, Some(a));
569    /// ```
570    #[unstable(feature = "array_try_map", issue = "79711")]
571    pub fn try_map<R>(self, f: impl FnMut(T) -> R) -> ChangeOutputType<R, [R::Output; N]>
572    where
573        R: Try<Residual: Residual<[R::Output; N]>>,
574    {
575        drain_array_with(self, |iter| try_from_trusted_iterator(iter.map(f)))
576    }
577
578    /// Returns a slice containing the entire array. Equivalent to `&s[..]`.
579    #[stable(feature = "array_as_slice", since = "1.57.0")]
580    #[rustc_const_stable(feature = "array_as_slice", since = "1.57.0")]
581    pub const fn as_slice(&self) -> &[T] {
582        self
583    }
584
585    /// Returns a mutable slice containing the entire array. Equivalent to
586    /// `&mut s[..]`.
587    #[stable(feature = "array_as_slice", since = "1.57.0")]
588    #[rustc_const_stable(feature = "const_array_as_mut_slice", since = "1.89.0")]
589    pub const fn as_mut_slice(&mut self) -> &mut [T] {
590        self
591    }
592
593    /// Borrows each element and returns an array of references with the same
594    /// size as `self`.
595    ///
596    ///
597    /// # Example
598    ///
599    /// ```
600    /// let floats = [3.1, 2.7, -1.0];
601    /// let float_refs: [&f64; 3] = floats.each_ref();
602    /// assert_eq!(float_refs, [&3.1, &2.7, &-1.0]);
603    /// ```
604    ///
605    /// This method is particularly useful if combined with other methods, like
606    /// [`map`](#method.map). This way, you can avoid moving the original
607    /// array if its elements are not [`Copy`].
608    ///
609    /// ```
610    /// let strings = ["Ferris".to_string(), "♥".to_string(), "Rust".to_string()];
611    /// let is_ascii = strings.each_ref().map(|s| s.is_ascii());
612    /// assert_eq!(is_ascii, [true, false, true]);
613    ///
614    /// // We can still access the original array: it has not been moved.
615    /// assert_eq!(strings.len(), 3);
616    /// ```
617    #[stable(feature = "array_methods", since = "1.77.0")]
618    #[rustc_const_stable(feature = "const_array_each_ref", since = "CURRENT_RUSTC_VERSION")]
619    pub const fn each_ref(&self) -> [&T; N] {
620        let mut buf = [null::<T>(); N];
621
622        // FIXME(const_trait_impl): We would like to simply use iterators for this (as in the original implementation), but this is not allowed in constant expressions.
623        let mut i = 0;
624        while i < N {
625            buf[i] = &raw const self[i];
626
627            i += 1;
628        }
629
630        // SAFETY: `*const T` has the same layout as `&T`, and we've also initialised each pointer as a valid reference.
631        unsafe { transmute_unchecked(buf) }
632    }
633
634    /// Borrows each element mutably and returns an array of mutable references
635    /// with the same size as `self`.
636    ///
637    ///
638    /// # Example
639    ///
640    /// ```
641    ///
642    /// let mut floats = [3.1, 2.7, -1.0];
643    /// let float_refs: [&mut f64; 3] = floats.each_mut();
644    /// *float_refs[0] = 0.0;
645    /// assert_eq!(float_refs, [&mut 0.0, &mut 2.7, &mut -1.0]);
646    /// assert_eq!(floats, [0.0, 2.7, -1.0]);
647    /// ```
648    #[stable(feature = "array_methods", since = "1.77.0")]
649    #[rustc_const_stable(feature = "const_array_each_ref", since = "CURRENT_RUSTC_VERSION")]
650    pub const fn each_mut(&mut self) -> [&mut T; N] {
651        let mut buf = [null_mut::<T>(); N];
652
653        // FIXME(const_trait_impl): We would like to simply use iterators for this (as in the original implementation), but this is not allowed in constant expressions.
654        let mut i = 0;
655        while i < N {
656            buf[i] = &raw mut self[i];
657
658            i += 1;
659        }
660
661        // SAFETY: `*mut T` has the same layout as `&mut T`, and we've also initialised each pointer as a valid reference.
662        unsafe { transmute_unchecked(buf) }
663    }
664
665    /// Divides one array reference into two at an index.
666    ///
667    /// The first will contain all indices from `[0, M)` (excluding
668    /// the index `M` itself) and the second will contain all
669    /// indices from `[M, N)` (excluding the index `N` itself).
670    ///
671    /// # Panics
672    ///
673    /// Panics if `M > N`.
674    ///
675    /// # Examples
676    ///
677    /// ```
678    /// #![feature(split_array)]
679    ///
680    /// let v = [1, 2, 3, 4, 5, 6];
681    ///
682    /// {
683    ///    let (left, right) = v.split_array_ref::<0>();
684    ///    assert_eq!(left, &[]);
685    ///    assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
686    /// }
687    ///
688    /// {
689    ///     let (left, right) = v.split_array_ref::<2>();
690    ///     assert_eq!(left, &[1, 2]);
691    ///     assert_eq!(right, &[3, 4, 5, 6]);
692    /// }
693    ///
694    /// {
695    ///     let (left, right) = v.split_array_ref::<6>();
696    ///     assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
697    ///     assert_eq!(right, &[]);
698    /// }
699    /// ```
700    #[unstable(
701        feature = "split_array",
702        reason = "return type should have array as 2nd element",
703        issue = "90091"
704    )]
705    #[inline]
706    pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T]) {
707        self.split_first_chunk::<M>().unwrap()
708    }
709
710    /// Divides one mutable array reference into two at an index.
711    ///
712    /// The first will contain all indices from `[0, M)` (excluding
713    /// the index `M` itself) and the second will contain all
714    /// indices from `[M, N)` (excluding the index `N` itself).
715    ///
716    /// # Panics
717    ///
718    /// Panics if `M > N`.
719    ///
720    /// # Examples
721    ///
722    /// ```
723    /// #![feature(split_array)]
724    ///
725    /// let mut v = [1, 0, 3, 0, 5, 6];
726    /// let (left, right) = v.split_array_mut::<2>();
727    /// assert_eq!(left, &mut [1, 0][..]);
728    /// assert_eq!(right, &mut [3, 0, 5, 6]);
729    /// left[1] = 2;
730    /// right[1] = 4;
731    /// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
732    /// ```
733    #[unstable(
734        feature = "split_array",
735        reason = "return type should have array as 2nd element",
736        issue = "90091"
737    )]
738    #[inline]
739    pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T]) {
740        self.split_first_chunk_mut::<M>().unwrap()
741    }
742
743    /// Divides one array reference into two at an index from the end.
744    ///
745    /// The first will contain all indices from `[0, N - M)` (excluding
746    /// the index `N - M` itself) and the second will contain all
747    /// indices from `[N - M, N)` (excluding the index `N` itself).
748    ///
749    /// # Panics
750    ///
751    /// Panics if `M > N`.
752    ///
753    /// # Examples
754    ///
755    /// ```
756    /// #![feature(split_array)]
757    ///
758    /// let v = [1, 2, 3, 4, 5, 6];
759    ///
760    /// {
761    ///    let (left, right) = v.rsplit_array_ref::<0>();
762    ///    assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
763    ///    assert_eq!(right, &[]);
764    /// }
765    ///
766    /// {
767    ///     let (left, right) = v.rsplit_array_ref::<2>();
768    ///     assert_eq!(left, &[1, 2, 3, 4]);
769    ///     assert_eq!(right, &[5, 6]);
770    /// }
771    ///
772    /// {
773    ///     let (left, right) = v.rsplit_array_ref::<6>();
774    ///     assert_eq!(left, &[]);
775    ///     assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
776    /// }
777    /// ```
778    #[unstable(
779        feature = "split_array",
780        reason = "return type should have array as 2nd element",
781        issue = "90091"
782    )]
783    #[inline]
784    pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M]) {
785        self.split_last_chunk::<M>().unwrap()
786    }
787
788    /// Divides one mutable array reference into two at an index from the end.
789    ///
790    /// The first will contain all indices from `[0, N - M)` (excluding
791    /// the index `N - M` itself) and the second will contain all
792    /// indices from `[N - M, N)` (excluding the index `N` itself).
793    ///
794    /// # Panics
795    ///
796    /// Panics if `M > N`.
797    ///
798    /// # Examples
799    ///
800    /// ```
801    /// #![feature(split_array)]
802    ///
803    /// let mut v = [1, 0, 3, 0, 5, 6];
804    /// let (left, right) = v.rsplit_array_mut::<4>();
805    /// assert_eq!(left, &mut [1, 0]);
806    /// assert_eq!(right, &mut [3, 0, 5, 6][..]);
807    /// left[1] = 2;
808    /// right[1] = 4;
809    /// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
810    /// ```
811    #[unstable(
812        feature = "split_array",
813        reason = "return type should have array as 2nd element",
814        issue = "90091"
815    )]
816    #[inline]
817    pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M]) {
818        self.split_last_chunk_mut::<M>().unwrap()
819    }
820}
821
822/// Populate an array from the first `N` elements of `iter`
823///
824/// # Panics
825///
826/// If the iterator doesn't actually have enough items.
827///
828/// By depending on `TrustedLen`, however, we can do that check up-front (where
829/// it easily optimizes away) so it doesn't impact the loop that fills the array.
830#[inline]
831fn from_trusted_iterator<T, const N: usize>(iter: impl UncheckedIterator<Item = T>) -> [T; N] {
832    try_from_trusted_iterator(iter.map(NeverShortCircuit)).0
833}
834
835#[inline]
836fn try_from_trusted_iterator<T, R, const N: usize>(
837    iter: impl UncheckedIterator<Item = R>,
838) -> ChangeOutputType<R, [T; N]>
839where
840    R: Try<Output = T>,
841    R::Residual: Residual<[T; N]>,
842{
843    assert!(iter.size_hint().0 >= N);
844    fn next<T>(mut iter: impl UncheckedIterator<Item = T>) -> impl FnMut(usize) -> T {
845        move |_| {
846            // SAFETY: We know that `from_fn` will call this at most N times,
847            // and we checked to ensure that we have at least that many items.
848            unsafe { iter.next_unchecked() }
849        }
850    }
851
852    try_from_fn(next(iter))
853}
854
855/// Version of [`try_from_fn`] using a passed-in slice in order to avoid
856/// needing to monomorphize for every array length.
857///
858/// This takes a generator rather than an iterator so that *at the type level*
859/// it never needs to worry about running out of items.  When combined with
860/// an infallible `Try` type, that means the loop canonicalizes easily, allowing
861/// it to optimize well.
862///
863/// It would be *possible* to unify this and [`iter_next_chunk_erased`] into one
864/// function that does the union of both things, but last time it was that way
865/// it resulted in poor codegen from the "are there enough source items?" checks
866/// not optimizing away.  So if you give it a shot, make sure to watch what
867/// happens in the codegen tests.
868#[inline]
869fn try_from_fn_erased<T, R>(
870    buffer: &mut [MaybeUninit<T>],
871    mut generator: impl FnMut(usize) -> R,
872) -> ControlFlow<R::Residual>
873where
874    R: Try<Output = T>,
875{
876    let mut guard = Guard { array_mut: buffer, initialized: 0 };
877
878    while guard.initialized < guard.array_mut.len() {
879        let item = generator(guard.initialized).branch()?;
880
881        // SAFETY: The loop condition ensures we have space to push the item
882        unsafe { guard.push_unchecked(item) };
883    }
884
885    mem::forget(guard);
886    ControlFlow::Continue(())
887}
888
889/// Panic guard for incremental initialization of arrays.
890///
891/// Disarm the guard with `mem::forget` once the array has been initialized.
892///
893/// # Safety
894///
895/// All write accesses to this structure are unsafe and must maintain a correct
896/// count of `initialized` elements.
897///
898/// To minimize indirection fields are still pub but callers should at least use
899/// `push_unchecked` to signal that something unsafe is going on.
900struct Guard<'a, T> {
901    /// The array to be initialized.
902    pub array_mut: &'a mut [MaybeUninit<T>],
903    /// The number of items that have been initialized so far.
904    pub initialized: usize,
905}
906
907impl<T> Guard<'_, T> {
908    /// Adds an item to the array and updates the initialized item counter.
909    ///
910    /// # Safety
911    ///
912    /// No more than N elements must be initialized.
913    #[inline]
914    pub(crate) unsafe fn push_unchecked(&mut self, item: T) {
915        // SAFETY: If `initialized` was correct before and the caller does not
916        // invoke this method more than N times then writes will be in-bounds
917        // and slots will not be initialized more than once.
918        unsafe {
919            self.array_mut.get_unchecked_mut(self.initialized).write(item);
920            self.initialized = self.initialized.unchecked_add(1);
921        }
922    }
923}
924
925impl<T> Drop for Guard<'_, T> {
926    #[inline]
927    fn drop(&mut self) {
928        debug_assert!(self.initialized <= self.array_mut.len());
929
930        // SAFETY: this slice will contain only initialized objects.
931        unsafe {
932            self.array_mut.get_unchecked_mut(..self.initialized).assume_init_drop();
933        }
934    }
935}
936
937/// Pulls `N` items from `iter` and returns them as an array. If the iterator
938/// yields fewer than `N` items, `Err` is returned containing an iterator over
939/// the already yielded items.
940///
941/// Since the iterator is passed as a mutable reference and this function calls
942/// `next` at most `N` times, the iterator can still be used afterwards to
943/// retrieve the remaining items.
944///
945/// If `iter.next()` panicks, all items already yielded by the iterator are
946/// dropped.
947///
948/// Used for [`Iterator::next_chunk`].
949#[inline]
950pub(crate) fn iter_next_chunk<T, const N: usize>(
951    iter: &mut impl Iterator<Item = T>,
952) -> Result<[T; N], IntoIter<T, N>> {
953    let mut array = [const { MaybeUninit::uninit() }; N];
954    let r = iter_next_chunk_erased(&mut array, iter);
955    match r {
956        Ok(()) => {
957            // SAFETY: All elements of `array` were populated.
958            Ok(unsafe { MaybeUninit::array_assume_init(array) })
959        }
960        Err(initialized) => {
961            // SAFETY: Only the first `initialized` elements were populated
962            Err(unsafe { IntoIter::new_unchecked(array, 0..initialized) })
963        }
964    }
965}
966
967/// Version of [`iter_next_chunk`] using a passed-in slice in order to avoid
968/// needing to monomorphize for every array length.
969///
970/// Unfortunately this loop has two exit conditions, the buffer filling up
971/// or the iterator running out of items, making it tend to optimize poorly.
972#[inline]
973fn iter_next_chunk_erased<T>(
974    buffer: &mut [MaybeUninit<T>],
975    iter: &mut impl Iterator<Item = T>,
976) -> Result<(), usize> {
977    let mut guard = Guard { array_mut: buffer, initialized: 0 };
978    while guard.initialized < guard.array_mut.len() {
979        let Some(item) = iter.next() else {
980            // Unlike `try_from_fn_erased`, we want to keep the partial results,
981            // so we need to defuse the guard instead of using `?`.
982            let initialized = guard.initialized;
983            mem::forget(guard);
984            return Err(initialized);
985        };
986
987        // SAFETY: The loop condition ensures we have space to push the item
988        unsafe { guard.push_unchecked(item) };
989    }
990
991    mem::forget(guard);
992    Ok(())
993}