2021-05-23 15:55:27 -04:00
|
|
|
//! Helper functions and types for fixed-length arrays.
|
2015-07-20 11:21:02 -07:00
|
|
|
//!
|
2020-12-19 08:23:59 -05:00
|
|
|
//! *[See also the array primitive type](array).*
|
2014-10-31 05:41:25 -04:00
|
|
|
|
2019-05-09 11:58:39 +09:00
|
|
|
#![stable(feature = "core_array", since = "1.36.0")]
|
2015-03-23 14:01:28 -07:00
|
|
|
|
2019-04-15 11:23:21 +09:00
|
|
|
use crate::borrow::{Borrow, BorrowMut};
|
|
|
|
|
use crate::cmp::Ordering;
|
2019-04-26 12:45:26 -07:00
|
|
|
use crate::convert::{Infallible, TryFrom};
|
2019-04-15 11:23:21 +09:00
|
|
|
use crate::fmt;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::hash::{self, Hash};
|
2021-02-14 14:42:47 +01:00
|
|
|
use crate::iter::TrustedLen;
|
|
|
|
|
use crate::mem::{self, MaybeUninit};
|
2020-07-31 23:19:10 +03:00
|
|
|
use crate::ops::{Index, IndexMut};
|
2019-04-15 11:23:21 +09:00
|
|
|
use crate::slice::{Iter, IterMut};
|
2014-10-31 05:41:25 -04:00
|
|
|
|
2021-05-30 10:23:50 -07:00
|
|
|
mod equality;
|
2019-07-25 00:39:39 +02:00
|
|
|
mod iter;
|
|
|
|
|
|
2020-12-29 09:16:46 +01:00
|
|
|
#[stable(feature = "array_value_iter", since = "1.51.0")]
|
2019-07-25 00:39:39 +02:00
|
|
|
pub use iter::IntoIter;
|
|
|
|
|
|
2021-09-30 07:49:32 -03:00
|
|
|
/// Creates an array `[T; N]` where each array element `T` is returned by the `cb` call.
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `cb`: Callback where the passed argument is the current array index.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// #![feature(array_from_fn)]
|
|
|
|
|
///
|
|
|
|
|
/// let array = core::array::from_fn(|i| i);
|
|
|
|
|
/// assert_eq!(array, [0, 1, 2, 3, 4]);
|
|
|
|
|
/// ```
|
|
|
|
|
#[inline]
|
|
|
|
|
#[unstable(feature = "array_from_fn", issue = "89379")]
|
|
|
|
|
pub fn from_fn<F, T, const N: usize>(mut cb: F) -> [T; N]
|
|
|
|
|
where
|
|
|
|
|
F: FnMut(usize) -> T,
|
|
|
|
|
{
|
|
|
|
|
let mut idx = 0;
|
|
|
|
|
[(); N].map(|_| {
|
|
|
|
|
let res = cb(idx);
|
|
|
|
|
idx += 1;
|
|
|
|
|
res
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates an array `[T; N]` where each fallible array element `T` is returned by the `cb` call.
|
|
|
|
|
/// Unlike `core::array::from_fn`, where the element creation can't fail, this version will return an error
|
|
|
|
|
/// if any element creation was unsuccessful.
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `cb`: Callback where the passed argument is the current array index.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// #![feature(array_from_fn)]
|
|
|
|
|
///
|
|
|
|
|
/// #[derive(Debug, PartialEq)]
|
|
|
|
|
/// enum SomeError {
|
|
|
|
|
/// Foo,
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// let array = core::array::try_from_fn(|i| Ok::<_, SomeError>(i));
|
|
|
|
|
/// assert_eq!(array, Ok([0, 1, 2, 3, 4]));
|
|
|
|
|
///
|
|
|
|
|
/// let another_array = core::array::try_from_fn::<SomeError, _, (), 2>(|_| Err(SomeError::Foo));
|
|
|
|
|
/// assert_eq!(another_array, Err(SomeError::Foo));
|
|
|
|
|
/// ```
|
|
|
|
|
#[inline]
|
|
|
|
|
#[unstable(feature = "array_from_fn", issue = "89379")]
|
|
|
|
|
pub fn try_from_fn<E, F, T, const N: usize>(cb: F) -> Result<[T; N], E>
|
|
|
|
|
where
|
|
|
|
|
F: FnMut(usize) -> Result<T, E>,
|
|
|
|
|
{
|
|
|
|
|
// SAFETY: we know for certain that this iterator will yield exactly `N`
|
|
|
|
|
// items.
|
|
|
|
|
unsafe { collect_into_array_rslt_unchecked(&mut (0..N).map(cb)) }
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-22 21:35:43 +02:00
|
|
|
/// Converts a reference to `T` into a reference to an array of length 1 (without copying).
|
2021-04-11 22:06:32 +03:00
|
|
|
#[stable(feature = "array_from_ref", since = "1.53.0")]
|
2021-10-23 20:59:15 +03:00
|
|
|
#[rustc_const_unstable(feature = "const_array_from_ref", issue = "90206")]
|
2021-10-22 11:15:56 +03:00
|
|
|
pub const fn from_ref<T>(s: &T) -> &[T; 1] {
|
2020-09-22 21:35:43 +02:00
|
|
|
// SAFETY: Converting `&T` to `&[T; 1]` is sound.
|
|
|
|
|
unsafe { &*(s as *const T).cast::<[T; 1]>() }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Converts a mutable reference to `T` into a mutable reference to an array of length 1 (without copying).
|
2021-04-11 22:06:32 +03:00
|
|
|
#[stable(feature = "array_from_ref", since = "1.53.0")]
|
2021-10-23 20:59:15 +03:00
|
|
|
#[rustc_const_unstable(feature = "const_array_from_ref", issue = "90206")]
|
2021-10-22 11:15:56 +03:00
|
|
|
pub const fn from_mut<T>(s: &mut T) -> &mut [T; 1] {
|
2020-09-22 21:35:43 +02:00
|
|
|
// SAFETY: Converting `&mut T` to `&mut [T; 1]` is sound.
|
|
|
|
|
unsafe { &mut *(s as *mut T).cast::<[T; 1]>() }
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 16:35:23 -04:00
|
|
|
/// The error type returned when a conversion from a slice to an array fails.
|
2019-02-08 15:00:47 +01:00
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2017-09-21 16:35:23 -04:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
|
pub struct TryFromSliceError(());
|
|
|
|
|
|
2019-05-09 11:58:39 +09:00
|
|
|
#[stable(feature = "core_array", since = "1.36.0")]
|
2017-09-29 11:15:05 -04:00
|
|
|
impl fmt::Display for TryFromSliceError {
|
|
|
|
|
#[inline]
|
2019-04-19 01:37:12 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2017-09-29 11:26:19 -04:00
|
|
|
fmt::Display::fmt(self.__description(), f)
|
2017-09-29 11:15:05 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFromSliceError {
|
2019-12-22 17:42:04 -05:00
|
|
|
#[unstable(
|
|
|
|
|
feature = "array_error_internals",
|
|
|
|
|
reason = "available through Error trait and this method should not \
|
2017-09-29 11:20:21 -04:00
|
|
|
be exposed publicly",
|
2019-12-22 17:42:04 -05:00
|
|
|
issue = "none"
|
|
|
|
|
)]
|
2017-09-29 11:15:05 -04:00
|
|
|
#[inline]
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
|
pub fn __description(&self) -> &str {
|
|
|
|
|
"could not convert slice to array"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-26 12:45:26 -07:00
|
|
|
#[stable(feature = "try_from_slice_error", since = "1.36.0")]
|
2021-10-18 19:19:28 +09:00
|
|
|
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
|
|
|
|
|
impl const From<Infallible> for TryFromSliceError {
|
2019-04-26 12:45:26 -07:00
|
|
|
fn from(x: Infallible) -> TryFromSliceError {
|
|
|
|
|
match x {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 18:06:26 +02:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2020-07-04 13:30:09 +03:00
|
|
|
impl<T, const N: usize> AsRef<[T]> for [T; N] {
|
2019-07-25 18:06:26 +02:00
|
|
|
#[inline]
|
|
|
|
|
fn as_ref(&self) -> &[T] {
|
|
|
|
|
&self[..]
|
2015-12-02 17:31:49 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 18:06:26 +02:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2020-07-04 13:30:09 +03:00
|
|
|
impl<T, const N: usize> AsMut<[T]> for [T; N] {
|
2019-07-25 18:06:26 +02:00
|
|
|
#[inline]
|
|
|
|
|
fn as_mut(&mut self) -> &mut [T] {
|
|
|
|
|
&mut self[..]
|
2015-12-02 17:31:49 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 18:06:26 +02:00
|
|
|
#[stable(feature = "array_borrow", since = "1.4.0")]
|
2020-07-04 13:30:09 +03:00
|
|
|
impl<T, const N: usize> Borrow<[T]> for [T; N] {
|
2019-07-25 18:06:26 +02:00
|
|
|
fn borrow(&self) -> &[T] {
|
|
|
|
|
self
|
2014-10-31 05:41:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 18:06:26 +02:00
|
|
|
#[stable(feature = "array_borrow", since = "1.4.0")]
|
2020-07-04 13:30:09 +03:00
|
|
|
impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
|
2019-07-25 18:06:26 +02:00
|
|
|
fn borrow_mut(&mut self) -> &mut [T] {
|
|
|
|
|
self
|
2019-07-05 23:59:59 -07:00
|
|
|
}
|
2019-07-25 18:06:26 +02:00
|
|
|
}
|
2019-07-05 23:59:59 -07:00
|
|
|
|
2019-07-25 18:06:26 +02:00
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
|
|
|
|
impl<T, const N: usize> TryFrom<&[T]> for [T; N]
|
|
|
|
|
where
|
|
|
|
|
T: Copy,
|
|
|
|
|
{
|
|
|
|
|
type Error = TryFromSliceError;
|
|
|
|
|
|
|
|
|
|
fn try_from(slice: &[T]) -> Result<[T; N], TryFromSliceError> {
|
|
|
|
|
<&Self>::try_from(slice).map(|r| *r)
|
2019-07-05 23:59:59 -07:00
|
|
|
}
|
2019-07-25 18:06:26 +02:00
|
|
|
}
|
2019-07-05 23:59:59 -07:00
|
|
|
|
2021-11-20 21:40:41 +09:00
|
|
|
#[stable(feature = "try_from_mut_slice_to_array", since = "1.57.0")]
|
|
|
|
|
impl<T, const N: usize> TryFrom<&mut [T]> for [T; N]
|
|
|
|
|
where
|
|
|
|
|
T: Copy,
|
|
|
|
|
{
|
|
|
|
|
type Error = TryFromSliceError;
|
|
|
|
|
|
|
|
|
|
fn try_from(slice: &mut [T]) -> Result<[T; N], TryFromSliceError> {
|
|
|
|
|
<Self>::try_from(slice.as_ref())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 18:06:26 +02:00
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] {
|
2019-07-25 18:06:26 +02:00
|
|
|
type Error = TryFromSliceError;
|
|
|
|
|
|
|
|
|
|
fn try_from(slice: &[T]) -> Result<&[T; N], TryFromSliceError> {
|
|
|
|
|
if slice.len() == N {
|
|
|
|
|
let ptr = slice.as_ptr() as *const [T; N];
|
2019-08-21 19:56:46 +02:00
|
|
|
// SAFETY: ok because we just checked that the length fits
|
2019-07-25 18:06:26 +02:00
|
|
|
unsafe { Ok(&*ptr) }
|
|
|
|
|
} else {
|
|
|
|
|
Err(TryFromSliceError(()))
|
2019-07-05 23:59:59 -07:00
|
|
|
}
|
|
|
|
|
}
|
2019-07-25 18:06:26 +02:00
|
|
|
}
|
2019-07-05 23:59:59 -07:00
|
|
|
|
2019-07-25 18:06:26 +02:00
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N] {
|
2019-07-25 18:06:26 +02:00
|
|
|
type Error = TryFromSliceError;
|
|
|
|
|
|
|
|
|
|
fn try_from(slice: &mut [T]) -> Result<&mut [T; N], TryFromSliceError> {
|
|
|
|
|
if slice.len() == N {
|
|
|
|
|
let ptr = slice.as_mut_ptr() as *mut [T; N];
|
2019-08-21 19:56:46 +02:00
|
|
|
// SAFETY: ok because we just checked that the length fits
|
2019-07-25 18:06:26 +02:00
|
|
|
unsafe { Ok(&mut *ptr) }
|
|
|
|
|
} else {
|
|
|
|
|
Err(TryFromSliceError(()))
|
2019-07-05 23:59:59 -07:00
|
|
|
}
|
|
|
|
|
}
|
2019-07-25 18:06:26 +02:00
|
|
|
}
|
2019-07-05 23:59:59 -07:00
|
|
|
|
2021-06-08 08:51:44 -07:00
|
|
|
/// The hash of an array is the same as that of the corresponding slice,
|
|
|
|
|
/// as required by the `Borrow` implementation.
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2021-06-24 01:30:08 -07:00
|
|
|
/// #![feature(build_hasher_simple_hash_one)]
|
|
|
|
|
/// use std::hash::BuildHasher;
|
2021-06-08 08:51:44 -07:00
|
|
|
///
|
|
|
|
|
/// let b = std::collections::hash_map::RandomState::new();
|
|
|
|
|
/// let a: [u8; 3] = [0xa8, 0x3c, 0x09];
|
|
|
|
|
/// let s: &[u8] = &[0xa8, 0x3c, 0x09];
|
2021-06-24 01:30:08 -07:00
|
|
|
/// assert_eq!(b.hash_one(a), b.hash_one(s));
|
2021-06-08 08:51:44 -07:00
|
|
|
/// ```
|
2019-07-25 18:06:26 +02:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
impl<T: Hash, const N: usize> Hash for [T; N] {
|
2019-07-25 18:06:26 +02:00
|
|
|
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
|
|
|
|
Hash::hash(&self[..], state)
|
2019-07-05 23:59:59 -07:00
|
|
|
}
|
2019-07-25 18:06:26 +02:00
|
|
|
}
|
2019-07-05 23:59:59 -07:00
|
|
|
|
2019-07-25 18:06:26 +02:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
impl<T: fmt::Debug, const N: usize> fmt::Debug for [T; N] {
|
2019-07-25 18:06:26 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
fmt::Debug::fmt(&&self[..], f)
|
2019-07-05 23:59:59 -07:00
|
|
|
}
|
2019-07-25 18:06:26 +02:00
|
|
|
}
|
2019-07-05 23:59:59 -07:00
|
|
|
|
2021-04-12 16:07:28 -07:00
|
|
|
// Note: the `#[rustc_skip_array_during_method_dispatch]` on `trait IntoIterator`
|
|
|
|
|
// hides this implementation from explicit `.into_iter()` calls on editions < 2021,
|
|
|
|
|
// so those calls will still resolve to the slice implementation, by reference.
|
2019-10-25 17:25:58 +02:00
|
|
|
#[stable(feature = "array_into_iter_impl", since = "1.53.0")]
|
|
|
|
|
impl<T, const N: usize> IntoIterator for [T; N] {
|
|
|
|
|
type Item = T;
|
|
|
|
|
type IntoIter = IntoIter<T, N>;
|
|
|
|
|
|
2021-04-14 12:05:56 -07:00
|
|
|
/// Creates a consuming iterator, that is, one that moves each value out of
|
|
|
|
|
/// the array (from start to end). The array cannot be used after calling
|
|
|
|
|
/// this unless `T` implements `Copy`, so the whole array is copied.
|
|
|
|
|
///
|
|
|
|
|
/// Arrays have special behavior when calling `.into_iter()` prior to the
|
|
|
|
|
/// 2021 edition -- see the [array] Editions section for more information.
|
|
|
|
|
///
|
|
|
|
|
/// [array]: prim@array
|
2019-10-25 17:25:58 +02:00
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
|
IntoIter::new(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 18:06:26 +02:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
impl<'a, T, const N: usize> IntoIterator for &'a [T; N] {
|
2019-07-25 18:06:26 +02:00
|
|
|
type Item = &'a T;
|
|
|
|
|
type IntoIter = Iter<'a, T>;
|
2019-07-05 23:59:59 -07:00
|
|
|
|
2019-07-25 18:06:26 +02:00
|
|
|
fn into_iter(self) -> Iter<'a, T> {
|
|
|
|
|
self.iter()
|
2019-07-05 23:59:59 -07:00
|
|
|
}
|
2019-07-25 18:06:26 +02:00
|
|
|
}
|
2019-07-05 23:59:59 -07:00
|
|
|
|
2019-07-25 18:06:26 +02:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N] {
|
2019-07-25 18:06:26 +02:00
|
|
|
type Item = &'a mut T;
|
|
|
|
|
type IntoIter = IterMut<'a, T>;
|
2019-07-05 23:59:59 -07:00
|
|
|
|
2019-07-25 18:06:26 +02:00
|
|
|
fn into_iter(self) -> IterMut<'a, T> {
|
|
|
|
|
self.iter_mut()
|
2019-07-05 23:59:59 -07:00
|
|
|
}
|
2019-07-25 18:06:26 +02:00
|
|
|
}
|
2019-07-05 23:59:59 -07:00
|
|
|
|
2020-07-31 23:19:10 +03:00
|
|
|
#[stable(feature = "index_trait_on_arrays", since = "1.50.0")]
|
|
|
|
|
impl<T, I, const N: usize> Index<I> for [T; N]
|
|
|
|
|
where
|
|
|
|
|
[T]: Index<I>,
|
|
|
|
|
{
|
|
|
|
|
type Output = <[T] as Index<I>>::Output;
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn index(&self, index: I) -> &Self::Output {
|
|
|
|
|
Index::index(self as &[T], index)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "index_trait_on_arrays", since = "1.50.0")]
|
|
|
|
|
impl<T, I, const N: usize> IndexMut<I> for [T; N]
|
|
|
|
|
where
|
|
|
|
|
[T]: IndexMut<I>,
|
|
|
|
|
{
|
|
|
|
|
#[inline]
|
|
|
|
|
fn index_mut(&mut self, index: I) -> &mut Self::Output {
|
|
|
|
|
IndexMut::index_mut(self as &mut [T], index)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 18:06:26 +02:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
impl<T: PartialOrd, const N: usize> PartialOrd for [T; N] {
|
2019-07-25 18:06:26 +02:00
|
|
|
#[inline]
|
|
|
|
|
fn partial_cmp(&self, other: &[T; N]) -> Option<Ordering> {
|
|
|
|
|
PartialOrd::partial_cmp(&&self[..], &&other[..])
|
2019-07-05 23:59:59 -07:00
|
|
|
}
|
2019-07-25 18:06:26 +02:00
|
|
|
#[inline]
|
|
|
|
|
fn lt(&self, other: &[T; N]) -> bool {
|
|
|
|
|
PartialOrd::lt(&&self[..], &&other[..])
|
|
|
|
|
}
|
|
|
|
|
#[inline]
|
|
|
|
|
fn le(&self, other: &[T; N]) -> bool {
|
|
|
|
|
PartialOrd::le(&&self[..], &&other[..])
|
|
|
|
|
}
|
|
|
|
|
#[inline]
|
|
|
|
|
fn ge(&self, other: &[T; N]) -> bool {
|
|
|
|
|
PartialOrd::ge(&&self[..], &&other[..])
|
|
|
|
|
}
|
|
|
|
|
#[inline]
|
|
|
|
|
fn gt(&self, other: &[T; N]) -> bool {
|
|
|
|
|
PartialOrd::gt(&&self[..], &&other[..])
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-05 23:59:59 -07:00
|
|
|
|
2020-10-25 17:46:45 +08:00
|
|
|
/// Implements comparison of arrays [lexicographically](Ord#lexicographical-comparison).
|
2019-07-25 18:06:26 +02:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
impl<T: Ord, const N: usize> Ord for [T; N] {
|
2019-07-25 18:06:26 +02:00
|
|
|
#[inline]
|
|
|
|
|
fn cmp(&self, other: &[T; N]) -> Ordering {
|
|
|
|
|
Ord::cmp(&&self[..], &&other[..])
|
2019-07-05 23:59:59 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-05 17:17:35 -04:00
|
|
|
#[cfg(not(bootstrap))]
|
2021-11-08 15:51:56 -05:00
|
|
|
#[stable(feature = "copy_clone_array_lib", since = "1.58.0")]
|
2021-06-05 17:17:35 -04:00
|
|
|
impl<T: Copy, const N: usize> Copy for [T; N] {}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(bootstrap))]
|
2021-11-08 15:51:56 -05:00
|
|
|
#[stable(feature = "copy_clone_array_lib", since = "1.58.0")]
|
2021-06-05 17:17:35 -04:00
|
|
|
impl<T: Clone, const N: usize> Clone for [T; N] {
|
2021-11-08 15:51:56 -05:00
|
|
|
#[inline]
|
2021-06-05 17:17:35 -04:00
|
|
|
fn clone(&self) -> Self {
|
2021-11-09 21:43:20 -08:00
|
|
|
SpecArrayClone::clone(self)
|
2021-06-05 17:17:35 -04:00
|
|
|
}
|
|
|
|
|
|
2021-11-08 15:51:56 -05:00
|
|
|
#[inline]
|
2021-06-05 17:17:35 -04:00
|
|
|
fn clone_from(&mut self, other: &Self) {
|
|
|
|
|
self.clone_from_slice(other);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-09 21:43:20 -08:00
|
|
|
#[cfg(not(bootstrap))]
|
|
|
|
|
trait SpecArrayClone: Clone {
|
|
|
|
|
fn clone<const N: usize>(array: &[Self; N]) -> [Self; N];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(bootstrap))]
|
|
|
|
|
impl<T: Clone> SpecArrayClone for T {
|
2021-11-10 11:57:14 -08:00
|
|
|
#[inline]
|
2021-11-09 21:43:20 -08:00
|
|
|
default fn clone<const N: usize>(array: &[T; N]) -> [T; N] {
|
|
|
|
|
// SAFETY: we know for certain that this iterator will yield exactly `N`
|
|
|
|
|
// items.
|
|
|
|
|
unsafe { collect_into_array_unchecked(&mut array.iter().cloned()) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(bootstrap))]
|
|
|
|
|
impl<T: Copy> SpecArrayClone for T {
|
2021-11-10 11:57:14 -08:00
|
|
|
#[inline]
|
2021-11-09 21:43:20 -08:00
|
|
|
fn clone<const N: usize>(array: &[T; N]) -> [T; N] {
|
|
|
|
|
*array
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-04 01:48:28 +01:00
|
|
|
// The Default impls cannot be done with const generics because `[T; 0]` doesn't
|
|
|
|
|
// require Default to be implemented, and having different impl blocks for
|
2021-03-08 20:28:30 +05:30
|
|
|
// different numbers isn't supported yet.
|
2015-08-14 10:11:19 -07:00
|
|
|
|
|
|
|
|
macro_rules! array_impl_default {
|
|
|
|
|
{$n:expr, $t:ident $($ts:ident)*} => {
|
|
|
|
|
#[stable(since = "1.4.0", feature = "array_default")]
|
|
|
|
|
impl<T> Default for [T; $n] where T: Default {
|
|
|
|
|
fn default() -> [T; $n] {
|
|
|
|
|
[$t::default(), $($ts::default()),*]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
array_impl_default!{($n - 1), $($ts)*}
|
|
|
|
|
};
|
|
|
|
|
{$n:expr,} => {
|
|
|
|
|
#[stable(since = "1.4.0", feature = "array_default")]
|
2021-08-14 16:35:12 +00:00
|
|
|
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
|
|
|
|
|
impl<T> const Default for [T; $n] {
|
2015-08-14 10:11:19 -07:00
|
|
|
fn default() -> [T; $n] { [] }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
array_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}
|
2020-08-06 07:40:06 +00:00
|
|
|
|
|
|
|
|
#[lang = "array"]
|
|
|
|
|
impl<T, const N: usize> [T; N] {
|
2020-08-08 23:20:28 +00:00
|
|
|
/// Returns an array of the same size as `self`, with function `f` applied to each element
|
|
|
|
|
/// in order.
|
2020-08-06 07:40:06 +00:00
|
|
|
///
|
2021-07-30 00:08:48 +02:00
|
|
|
/// If you don't necessarily need a new fixed-size array, consider using
|
|
|
|
|
/// [`Iterator::map`] instead.
|
|
|
|
|
///
|
|
|
|
|
///
|
|
|
|
|
/// # Note on performance and stack usage
|
|
|
|
|
///
|
|
|
|
|
/// Unfortunately, usages of this method are currently not always optimized
|
|
|
|
|
/// as well as they could be. This mainly concerns large arrays, as mapping
|
|
|
|
|
/// over small arrays seem to be optimized just fine. Also note that in
|
|
|
|
|
/// debug mode (i.e. without any optimizations), this method can use a lot
|
|
|
|
|
/// of stack space (a few times the size of the array or more).
|
|
|
|
|
///
|
|
|
|
|
/// Therefore, in performance-critical code, try to avoid using this method
|
|
|
|
|
/// on large arrays or check the emitted code. Also try to avoid chained
|
|
|
|
|
/// maps (e.g. `arr.map(...).map(...)`).
|
|
|
|
|
///
|
|
|
|
|
/// In many cases, you can instead use [`Iterator::map`] by calling `.iter()`
|
|
|
|
|
/// or `.into_iter()` on your array. `[T; N]::map` is only necessary if you
|
|
|
|
|
/// really need a new array of the same size as the result. Rust's lazy
|
|
|
|
|
/// iterators tend to get optimized very well.
|
|
|
|
|
///
|
|
|
|
|
///
|
2020-08-06 07:40:06 +00:00
|
|
|
/// # Examples
|
2020-08-07 06:00:52 +00:00
|
|
|
///
|
2020-08-06 07:40:06 +00:00
|
|
|
/// ```
|
2020-08-06 23:36:50 +00:00
|
|
|
/// let x = [1, 2, 3];
|
2020-08-06 07:40:06 +00:00
|
|
|
/// let y = x.map(|v| v + 1);
|
2020-08-06 23:36:50 +00:00
|
|
|
/// assert_eq!(y, [2, 3, 4]);
|
2020-08-07 06:00:52 +00:00
|
|
|
///
|
|
|
|
|
/// let x = [1, 2, 3];
|
|
|
|
|
/// let mut temp = 0;
|
|
|
|
|
/// let y = x.map(|v| { temp += 1; v * temp });
|
|
|
|
|
/// assert_eq!(y, [1, 4, 9]);
|
2020-08-08 23:20:28 +00:00
|
|
|
///
|
|
|
|
|
/// let x = ["Ferris", "Bueller's", "Day", "Off"];
|
|
|
|
|
/// let y = x.map(|v| v.len());
|
|
|
|
|
/// assert_eq!(y, [6, 9, 3, 3]);
|
2020-08-06 07:40:06 +00:00
|
|
|
/// ```
|
2021-07-15 16:27:08 -07:00
|
|
|
#[stable(feature = "array_map", since = "1.55.0")]
|
2021-02-14 14:42:47 +01:00
|
|
|
pub fn map<F, U>(self, f: F) -> [U; N]
|
2020-08-06 07:40:06 +00:00
|
|
|
where
|
2020-08-06 23:36:50 +00:00
|
|
|
F: FnMut(T) -> U,
|
2020-08-06 07:40:06 +00:00
|
|
|
{
|
2021-02-14 14:42:47 +01:00
|
|
|
// SAFETY: we know for certain that this iterator will yield exactly `N`
|
|
|
|
|
// items.
|
2021-05-30 23:16:45 +06:00
|
|
|
unsafe { collect_into_array_unchecked(&mut IntoIterator::into_iter(self).map(f)) }
|
2020-08-06 07:40:06 +00:00
|
|
|
}
|
2020-08-30 20:22:09 +02:00
|
|
|
|
2020-11-26 23:22:36 +01:00
|
|
|
/// 'Zips up' two arrays into a single array of pairs.
|
2020-12-16 21:12:10 +01:00
|
|
|
///
|
|
|
|
|
/// `zip()` returns a new array where every element is a tuple where the
|
|
|
|
|
/// first element comes from the first array, and the second element comes
|
|
|
|
|
/// from the second array. In other words, it zips two arrays together,
|
|
|
|
|
/// into a single one.
|
2020-11-26 23:22:36 +01:00
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(array_zip)]
|
|
|
|
|
/// let x = [1, 2, 3];
|
|
|
|
|
/// let y = [4, 5, 6];
|
|
|
|
|
/// let z = x.zip(y);
|
|
|
|
|
/// assert_eq!(z, [(1, 4), (2, 5), (3, 6)]);
|
|
|
|
|
/// ```
|
2020-12-17 00:27:21 +01:00
|
|
|
#[unstable(feature = "array_zip", issue = "80094")]
|
2020-11-26 23:22:36 +01:00
|
|
|
pub fn zip<U>(self, rhs: [U; N]) -> [(T, U); N] {
|
2021-05-30 23:16:45 +06:00
|
|
|
let mut iter = IntoIterator::into_iter(self).zip(rhs);
|
2021-02-14 14:42:47 +01:00
|
|
|
|
|
|
|
|
// SAFETY: we know for certain that this iterator will yield exactly `N`
|
|
|
|
|
// items.
|
|
|
|
|
unsafe { collect_into_array_unchecked(&mut iter) }
|
2020-11-26 23:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
2020-08-30 20:22:09 +02:00
|
|
|
/// Returns a slice containing the entire array. Equivalent to `&s[..]`.
|
2021-08-26 05:27:39 -04:00
|
|
|
#[stable(feature = "array_as_slice", since = "1.57.0")]
|
|
|
|
|
pub const fn as_slice(&self) -> &[T] {
|
2020-08-30 20:22:09 +02:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns a mutable slice containing the entire array. Equivalent to
|
|
|
|
|
/// `&mut s[..]`.
|
2021-08-26 05:27:39 -04:00
|
|
|
#[stable(feature = "array_as_slice", since = "1.57.0")]
|
2020-08-30 20:22:09 +02:00
|
|
|
pub fn as_mut_slice(&mut self) -> &mut [T] {
|
|
|
|
|
self
|
|
|
|
|
}
|
2020-08-13 00:19:53 +02:00
|
|
|
|
|
|
|
|
/// Borrows each element and returns an array of references with the same
|
|
|
|
|
/// size as `self`.
|
|
|
|
|
///
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(array_methods)]
|
|
|
|
|
///
|
|
|
|
|
/// let floats = [3.1, 2.7, -1.0];
|
|
|
|
|
/// let float_refs: [&f64; 3] = floats.each_ref();
|
|
|
|
|
/// assert_eq!(float_refs, [&3.1, &2.7, &-1.0]);
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// This method is particularly useful if combined with other methods, like
|
2021-03-08 11:49:26 +05:30
|
|
|
/// [`map`](#method.map). This way, you can avoid moving the original
|
2021-08-29 12:29:43 -07:00
|
|
|
/// array if its elements are not [`Copy`].
|
2020-08-13 00:19:53 +02:00
|
|
|
///
|
|
|
|
|
/// ```
|
2021-07-15 16:27:08 -07:00
|
|
|
/// #![feature(array_methods)]
|
2020-08-13 00:19:53 +02:00
|
|
|
///
|
|
|
|
|
/// let strings = ["Ferris".to_string(), "♥".to_string(), "Rust".to_string()];
|
|
|
|
|
/// let is_ascii = strings.each_ref().map(|s| s.is_ascii());
|
|
|
|
|
/// assert_eq!(is_ascii, [true, false, true]);
|
|
|
|
|
///
|
|
|
|
|
/// // We can still access the original array: it has not been moved.
|
|
|
|
|
/// assert_eq!(strings.len(), 3);
|
|
|
|
|
/// ```
|
|
|
|
|
#[unstable(feature = "array_methods", issue = "76118")]
|
|
|
|
|
pub fn each_ref(&self) -> [&T; N] {
|
2021-02-14 14:42:47 +01:00
|
|
|
// SAFETY: we know for certain that this iterator will yield exactly `N`
|
|
|
|
|
// items.
|
|
|
|
|
unsafe { collect_into_array_unchecked(&mut self.iter()) }
|
2020-08-13 00:19:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Borrows each element mutably and returns an array of mutable references
|
|
|
|
|
/// with the same size as `self`.
|
|
|
|
|
///
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(array_methods)]
|
|
|
|
|
///
|
|
|
|
|
/// let mut floats = [3.1, 2.7, -1.0];
|
|
|
|
|
/// let float_refs: [&mut f64; 3] = floats.each_mut();
|
|
|
|
|
/// *float_refs[0] = 0.0;
|
|
|
|
|
/// assert_eq!(float_refs, [&mut 0.0, &mut 2.7, &mut -1.0]);
|
|
|
|
|
/// assert_eq!(floats, [0.0, 2.7, -1.0]);
|
|
|
|
|
/// ```
|
|
|
|
|
#[unstable(feature = "array_methods", issue = "76118")]
|
|
|
|
|
pub fn each_mut(&mut self) -> [&mut T; N] {
|
2021-02-14 14:42:47 +01:00
|
|
|
// SAFETY: we know for certain that this iterator will yield exactly `N`
|
|
|
|
|
// items.
|
|
|
|
|
unsafe { collect_into_array_unchecked(&mut self.iter_mut()) }
|
|
|
|
|
}
|
2021-03-17 17:23:09 +01:00
|
|
|
|
|
|
|
|
/// Divides one array reference into two at an index.
|
|
|
|
|
///
|
|
|
|
|
/// The first will contain all indices from `[0, M)` (excluding
|
|
|
|
|
/// the index `M` itself) and the second will contain all
|
|
|
|
|
/// indices from `[M, N)` (excluding the index `N` itself).
|
|
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
///
|
|
|
|
|
/// Panics if `M > N`.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(split_array)]
|
|
|
|
|
///
|
|
|
|
|
/// let v = [1, 2, 3, 4, 5, 6];
|
|
|
|
|
///
|
|
|
|
|
/// {
|
|
|
|
|
/// let (left, right) = v.split_array_ref::<0>();
|
|
|
|
|
/// assert_eq!(left, &[]);
|
|
|
|
|
/// assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// {
|
|
|
|
|
/// let (left, right) = v.split_array_ref::<2>();
|
|
|
|
|
/// assert_eq!(left, &[1, 2]);
|
|
|
|
|
/// assert_eq!(right, &[3, 4, 5, 6]);
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// {
|
|
|
|
|
/// let (left, right) = v.split_array_ref::<6>();
|
|
|
|
|
/// assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
|
|
|
|
|
/// assert_eq!(right, &[]);
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
#[unstable(
|
|
|
|
|
feature = "split_array",
|
|
|
|
|
reason = "return type should have array as 2nd element",
|
|
|
|
|
issue = "90091"
|
|
|
|
|
)]
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T]) {
|
|
|
|
|
(&self[..]).split_array_ref::<M>()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Divides one mutable array reference into two at an index.
|
|
|
|
|
///
|
|
|
|
|
/// The first will contain all indices from `[0, M)` (excluding
|
|
|
|
|
/// the index `M` itself) and the second will contain all
|
|
|
|
|
/// indices from `[M, N)` (excluding the index `N` itself).
|
|
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
///
|
|
|
|
|
/// Panics if `M > N`.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(split_array)]
|
|
|
|
|
///
|
|
|
|
|
/// let mut v = [1, 0, 3, 0, 5, 6];
|
|
|
|
|
/// let (left, right) = v.split_array_mut::<2>();
|
|
|
|
|
/// assert_eq!(left, &mut [1, 0][..]);
|
|
|
|
|
/// assert_eq!(right, &mut [3, 0, 5, 6]);
|
|
|
|
|
/// left[1] = 2;
|
|
|
|
|
/// right[1] = 4;
|
|
|
|
|
/// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
|
|
|
|
|
/// ```
|
|
|
|
|
#[unstable(
|
|
|
|
|
feature = "split_array",
|
|
|
|
|
reason = "return type should have array as 2nd element",
|
|
|
|
|
issue = "90091"
|
|
|
|
|
)]
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T]) {
|
|
|
|
|
(&mut self[..]).split_array_mut::<M>()
|
|
|
|
|
}
|
2021-02-14 14:42:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Pulls `N` items from `iter` and returns them as an array. If the iterator
|
|
|
|
|
/// yields fewer than `N` items, this function exhibits undefined behavior.
|
|
|
|
|
///
|
|
|
|
|
/// See [`collect_into_array`] for more information.
|
|
|
|
|
///
|
|
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// It is up to the caller to guarantee that `iter` yields at least `N` items.
|
|
|
|
|
/// Violating this condition causes undefined behavior.
|
2021-09-30 07:49:32 -03:00
|
|
|
unsafe fn collect_into_array_rslt_unchecked<E, I, T, const N: usize>(
|
|
|
|
|
iter: &mut I,
|
|
|
|
|
) -> Result<[T; N], E>
|
2021-02-14 14:42:47 +01:00
|
|
|
where
|
|
|
|
|
// Note: `TrustedLen` here is somewhat of an experiment. This is just an
|
|
|
|
|
// internal function, so feel free to remove if this bound turns out to be a
|
|
|
|
|
// bad idea. In that case, remember to also remove the lower bound
|
|
|
|
|
// `debug_assert!` below!
|
2021-09-30 07:49:32 -03:00
|
|
|
I: Iterator<Item = Result<T, E>> + TrustedLen,
|
2021-02-14 14:42:47 +01:00
|
|
|
{
|
|
|
|
|
debug_assert!(N <= iter.size_hint().1.unwrap_or(usize::MAX));
|
|
|
|
|
debug_assert!(N <= iter.size_hint().0);
|
|
|
|
|
|
2021-08-30 16:13:56 -04:00
|
|
|
// SAFETY: covered by the function contract.
|
|
|
|
|
unsafe { collect_into_array(iter).unwrap_unchecked() }
|
2021-02-14 14:42:47 +01:00
|
|
|
}
|
|
|
|
|
|
2021-09-30 07:49:32 -03:00
|
|
|
// Infallible version of `collect_into_array_rslt_unchecked`.
|
|
|
|
|
unsafe fn collect_into_array_unchecked<I, const N: usize>(iter: &mut I) -> [I::Item; N]
|
|
|
|
|
where
|
|
|
|
|
I: Iterator + TrustedLen,
|
|
|
|
|
{
|
2021-09-30 14:19:56 +02:00
|
|
|
let mut map = iter.map(Ok::<_, Infallible>);
|
2021-09-30 07:49:32 -03:00
|
|
|
|
2021-09-30 14:19:56 +02:00
|
|
|
// SAFETY: The same safety considerations w.r.t. the iterator length
|
|
|
|
|
// apply for `collect_into_array_rslt_unchecked` as for
|
|
|
|
|
// `collect_into_array_unchecked`
|
|
|
|
|
match unsafe { collect_into_array_rslt_unchecked(&mut map) } {
|
|
|
|
|
Ok(array) => array,
|
|
|
|
|
}
|
2021-09-30 07:49:32 -03:00
|
|
|
}
|
|
|
|
|
|
2021-02-14 14:42:47 +01:00
|
|
|
/// Pulls `N` items from `iter` and returns them as an array. If the iterator
|
|
|
|
|
/// yields fewer than `N` items, `None` is returned and all already yielded
|
|
|
|
|
/// items are dropped.
|
|
|
|
|
///
|
2021-03-08 11:49:26 +05:30
|
|
|
/// Since the iterator is passed as a mutable reference and this function calls
|
2021-02-14 14:42:47 +01:00
|
|
|
/// `next` at most `N` times, the iterator can still be used afterwards to
|
|
|
|
|
/// retrieve the remaining items.
|
|
|
|
|
///
|
|
|
|
|
/// If `iter.next()` panicks, all items already yielded by the iterator are
|
|
|
|
|
/// dropped.
|
2021-09-30 07:49:32 -03:00
|
|
|
fn collect_into_array<E, I, T, const N: usize>(iter: &mut I) -> Option<Result<[T; N], E>>
|
2021-02-14 14:42:47 +01:00
|
|
|
where
|
2021-09-30 07:49:32 -03:00
|
|
|
I: Iterator<Item = Result<T, E>>,
|
2021-02-14 14:42:47 +01:00
|
|
|
{
|
|
|
|
|
if N == 0 {
|
|
|
|
|
// SAFETY: An empty array is always inhabited and has no validity invariants.
|
2021-09-30 07:49:32 -03:00
|
|
|
return unsafe { Some(Ok(mem::zeroed())) };
|
2021-02-14 14:42:47 +01:00
|
|
|
}
|
|
|
|
|
|
2021-09-30 08:40:05 -03:00
|
|
|
struct Guard<'a, T, const N: usize> {
|
|
|
|
|
array_mut: &'a mut [MaybeUninit<T>; N],
|
2021-02-14 14:42:47 +01:00
|
|
|
initialized: usize,
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 08:40:05 -03:00
|
|
|
impl<T, const N: usize> Drop for Guard<'_, T, N> {
|
2021-02-14 14:42:47 +01:00
|
|
|
fn drop(&mut self) {
|
|
|
|
|
debug_assert!(self.initialized <= N);
|
|
|
|
|
|
2021-09-30 13:53:24 +02:00
|
|
|
// SAFETY: this slice will contain only initialized objects.
|
2021-02-14 14:42:47 +01:00
|
|
|
unsafe {
|
2021-09-30 13:53:24 +02:00
|
|
|
crate::ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(
|
|
|
|
|
&mut self.array_mut.get_unchecked_mut(..self.initialized),
|
|
|
|
|
));
|
2021-02-14 14:42:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut array = MaybeUninit::uninit_array::<N>();
|
2021-09-30 13:53:24 +02:00
|
|
|
let mut guard = Guard { array_mut: &mut array, initialized: 0 };
|
2021-02-14 14:42:47 +01:00
|
|
|
|
2021-09-30 07:49:32 -03:00
|
|
|
while let Some(item_rslt) = iter.next() {
|
|
|
|
|
let item = match item_rslt {
|
|
|
|
|
Err(err) => {
|
|
|
|
|
return Some(Err(err));
|
|
|
|
|
}
|
|
|
|
|
Ok(elem) => elem,
|
|
|
|
|
};
|
2021-02-14 14:42:47 +01:00
|
|
|
|
|
|
|
|
// SAFETY: `guard.initialized` starts at 0, is increased by one in the
|
|
|
|
|
// loop and the loop is aborted once it reaches N (which is
|
|
|
|
|
// `array.len()`).
|
|
|
|
|
unsafe {
|
2021-09-30 08:40:05 -03:00
|
|
|
guard.array_mut.get_unchecked_mut(guard.initialized).write(item);
|
2020-08-13 00:19:53 +02:00
|
|
|
}
|
2021-02-14 14:42:47 +01:00
|
|
|
guard.initialized += 1;
|
|
|
|
|
|
|
|
|
|
// Check if the whole array was initialized.
|
|
|
|
|
if guard.initialized == N {
|
|
|
|
|
mem::forget(guard);
|
2020-08-13 00:19:53 +02:00
|
|
|
|
2021-02-14 14:42:47 +01:00
|
|
|
// SAFETY: the condition above asserts that all elements are
|
|
|
|
|
// initialized.
|
|
|
|
|
let out = unsafe { MaybeUninit::array_assume_init(array) };
|
2021-09-30 07:49:32 -03:00
|
|
|
return Some(Ok(out));
|
2021-02-14 14:42:47 +01:00
|
|
|
}
|
2020-08-13 00:19:53 +02:00
|
|
|
}
|
2021-02-14 14:42:47 +01:00
|
|
|
|
|
|
|
|
// This is only reached if the iterator is exhausted before
|
|
|
|
|
// `guard.initialized` reaches `N`. Also note that `guard` is dropped here,
|
|
|
|
|
// dropping all already initialized elements.
|
|
|
|
|
None
|
2020-08-06 07:40:06 +00:00
|
|
|
}
|