2019-07-25 00:39:39 +02:00
|
|
|
//! Defines the `IntoIter` owned iterator for arrays.
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
fmt,
|
2021-06-01 02:21:26 +02:00
|
|
|
iter::{self, ExactSizeIterator, FusedIterator, TrustedLen},
|
2019-07-25 00:39:39 +02:00
|
|
|
mem::{self, MaybeUninit},
|
|
|
|
|
ops::Range,
|
|
|
|
|
ptr,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// A by-value [array] iterator.
|
2020-12-29 09:16:46 +01:00
|
|
|
#[stable(feature = "array_value_iter", since = "1.51.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
pub struct IntoIter<T, const N: usize> {
|
2019-07-25 00:39:39 +02:00
|
|
|
/// This is the array we are iterating over.
|
|
|
|
|
///
|
|
|
|
|
/// Elements with index `i` where `alive.start <= i < alive.end` have not
|
|
|
|
|
/// been yielded yet and are valid array entries. Elements with indices `i
|
|
|
|
|
/// < alive.start` or `i >= alive.end` have been yielded already and must
|
|
|
|
|
/// not be accessed anymore! Those dead elements might even be in a
|
|
|
|
|
/// completely uninitialized state!
|
|
|
|
|
///
|
|
|
|
|
/// So the invariants are:
|
|
|
|
|
/// - `data[alive]` is alive (i.e. contains valid elements)
|
|
|
|
|
/// - `data[..alive.start]` and `data[alive.end..]` are dead (i.e. the
|
|
|
|
|
/// elements were already read and must not be touched anymore!)
|
|
|
|
|
data: [MaybeUninit<T>; N],
|
|
|
|
|
|
|
|
|
|
/// The elements in `data` that have not been yielded yet.
|
|
|
|
|
///
|
|
|
|
|
/// Invariants:
|
|
|
|
|
/// - `alive.start <= alive.end`
|
|
|
|
|
/// - `alive.end <= N`
|
|
|
|
|
alive: Range<usize>,
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-05 15:02:01 +03:00
|
|
|
impl<T, const N: usize> IntoIter<T, N> {
|
2019-07-25 00:39:39 +02:00
|
|
|
/// Creates a new iterator over the given `array`.
|
|
|
|
|
///
|
2020-12-29 09:16:46 +01:00
|
|
|
/// *Note*: this method might be deprecated in the future,
|
2021-09-03 11:24:52 +02:00
|
|
|
/// since [`IntoIterator`] is now implemented for arrays.
|
2020-12-29 09:16:46 +01:00
|
|
|
///
|
2021-01-21 15:52:53 +01:00
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::array;
|
|
|
|
|
///
|
|
|
|
|
/// for value in array::IntoIter::new([1, 2, 3, 4, 5]) {
|
2021-08-22 16:20:58 +02:00
|
|
|
/// // The type of `value` is an `i32` here, instead of `&i32`
|
2021-01-21 15:52:53 +01:00
|
|
|
/// let _: i32 = value;
|
|
|
|
|
/// }
|
2021-09-03 11:24:52 +02:00
|
|
|
///
|
|
|
|
|
/// // Since Rust 1.53, arrays implement IntoIterator directly:
|
|
|
|
|
/// for value in [1, 2, 3, 4, 5] {
|
|
|
|
|
/// // The type of `value` is an `i32` here, instead of `&i32`
|
|
|
|
|
/// let _: i32 = value;
|
|
|
|
|
/// }
|
2021-01-21 15:52:53 +01:00
|
|
|
/// ```
|
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 fn new(array: [T; N]) -> Self {
|
2019-10-25 18:35:51 +02:00
|
|
|
// SAFETY: The transmute here is actually safe. The docs of `MaybeUninit`
|
2019-07-25 00:39:39 +02:00
|
|
|
// promise:
|
|
|
|
|
//
|
|
|
|
|
// > `MaybeUninit<T>` is guaranteed to have the same size and alignment
|
|
|
|
|
// > as `T`.
|
|
|
|
|
//
|
|
|
|
|
// The docs even show a transmute from an array of `MaybeUninit<T>` to
|
|
|
|
|
// an array of `T`.
|
|
|
|
|
//
|
|
|
|
|
// With that, this initialization satisfies the invariants.
|
|
|
|
|
|
|
|
|
|
// FIXME(LukasKalbertodt): actually use `mem::transmute` here, once it
|
|
|
|
|
// works with const generics:
|
2020-08-07 13:51:46 -07:00
|
|
|
// `mem::transmute::<[T; N], [MaybeUninit<T>; N]>(array)`
|
2019-07-25 00:39:39 +02:00
|
|
|
//
|
2020-08-07 13:51:46 -07:00
|
|
|
// Until then, we can use `mem::transmute_copy` to create a bitwise copy
|
|
|
|
|
// as a different type, then forget `array` so that it is not dropped.
|
|
|
|
|
unsafe {
|
|
|
|
|
let iter = Self { data: mem::transmute_copy(&array), alive: 0..N };
|
2019-07-25 00:39:39 +02:00
|
|
|
mem::forget(array);
|
2020-08-07 13:51:46 -07:00
|
|
|
iter
|
|
|
|
|
}
|
2019-07-25 00:39:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns an immutable slice of all elements that have not been yielded
|
|
|
|
|
/// yet.
|
2020-12-29 09:16:46 +01:00
|
|
|
#[stable(feature = "array_value_iter", since = "1.51.0")]
|
2020-11-19 08:28:49 +01:00
|
|
|
pub fn as_slice(&self) -> &[T] {
|
2020-08-07 13:51:46 -07:00
|
|
|
// SAFETY: We know that all elements within `alive` are properly initialized.
|
|
|
|
|
unsafe {
|
|
|
|
|
let slice = self.data.get_unchecked(self.alive.clone());
|
2020-09-01 23:16:51 +02:00
|
|
|
MaybeUninit::slice_assume_init_ref(slice)
|
2020-08-07 13:51:46 -07:00
|
|
|
}
|
2019-07-25 00:39:39 +02:00
|
|
|
}
|
2019-10-25 18:11:20 +02:00
|
|
|
|
|
|
|
|
/// Returns a mutable slice of all elements that have not been yielded yet.
|
2020-12-29 09:16:46 +01:00
|
|
|
#[stable(feature = "array_value_iter", since = "1.51.0")]
|
2020-11-19 08:28:49 +01:00
|
|
|
pub fn as_mut_slice(&mut self) -> &mut [T] {
|
2020-08-07 13:51:46 -07:00
|
|
|
// SAFETY: We know that all elements within `alive` are properly initialized.
|
|
|
|
|
unsafe {
|
|
|
|
|
let slice = self.data.get_unchecked_mut(self.alive.clone());
|
2020-09-01 23:16:51 +02:00
|
|
|
MaybeUninit::slice_assume_init_mut(slice)
|
2020-08-07 13:51:46 -07:00
|
|
|
}
|
2019-10-25 18:11:20 +02:00
|
|
|
}
|
2019-07-25 00:39:39 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-25 10:50:21 +02:00
|
|
|
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
impl<T, const N: usize> Iterator for IntoIter<T, N> {
|
2019-07-25 00:39:39 +02:00
|
|
|
type Item = T;
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2020-08-07 13:51:46 -07:00
|
|
|
// Get the next index from the front.
|
2019-07-25 00:39:39 +02:00
|
|
|
//
|
2020-08-07 13:51:46 -07:00
|
|
|
// Increasing `alive.start` by 1 maintains the invariant regarding
|
|
|
|
|
// `alive`. However, due to this change, for a short time, the alive
|
|
|
|
|
// zone is not `data[alive]` anymore, but `data[idx..alive.end]`.
|
|
|
|
|
self.alive.next().map(|idx| {
|
|
|
|
|
// Read the element from the array.
|
|
|
|
|
// SAFETY: `idx` is an index into the former "alive" region of the
|
|
|
|
|
// array. Reading this element means that `data[idx]` is regarded as
|
|
|
|
|
// dead now (i.e. do not touch). As `idx` was the start of the
|
|
|
|
|
// alive-zone, the alive zone is now `data[alive]` again, restoring
|
|
|
|
|
// all invariants.
|
2020-09-09 11:27:42 +02:00
|
|
|
unsafe { self.data.get_unchecked(idx).assume_init_read() }
|
2020-08-07 13:51:46 -07:00
|
|
|
})
|
2019-07-25 00:39:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
|
let len = self.len();
|
|
|
|
|
(len, Some(len))
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-24 17:42:20 +02:00
|
|
|
#[inline]
|
|
|
|
|
fn fold<Acc, Fold>(mut self, init: Acc, mut fold: Fold) -> Acc
|
|
|
|
|
where
|
|
|
|
|
Fold: FnMut(Acc, Self::Item) -> Acc,
|
|
|
|
|
{
|
|
|
|
|
let data = &mut self.data;
|
2021-07-27 00:14:19 +02:00
|
|
|
// FIXME: This uses try_fold(&mut iter) instead of fold(iter) because the latter
|
|
|
|
|
// would go through the blanket `impl Iterator for &mut I` implementation
|
|
|
|
|
// which lacks inline annotations on its methods and adding those would be a larger
|
|
|
|
|
// perturbation than using try_fold here.
|
|
|
|
|
// Whether it would be beneficial to add those annotations should be investigated separately.
|
2021-07-24 17:42:20 +02:00
|
|
|
(&mut self.alive)
|
|
|
|
|
.try_fold::<_, _, Result<_, !>>(init, |acc, idx| {
|
|
|
|
|
// SAFETY: idx is obtained by folding over the `alive` range, which implies the
|
|
|
|
|
// value is currently considered alive but as the range is being consumed each value
|
|
|
|
|
// we read here will only be read once and then considered dead.
|
|
|
|
|
Ok(fold(acc, unsafe { data.get_unchecked(idx).assume_init_read() }))
|
|
|
|
|
})
|
|
|
|
|
.unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 00:39:39 +02:00
|
|
|
fn count(self) -> usize {
|
|
|
|
|
self.len()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn last(mut self) -> Option<Self::Item> {
|
|
|
|
|
self.next_back()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 10:50:21 +02:00
|
|
|
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
impl<T, const N: usize> DoubleEndedIterator for IntoIter<T, N> {
|
2019-07-25 00:39:39 +02:00
|
|
|
fn next_back(&mut self) -> Option<Self::Item> {
|
2020-08-07 13:51:46 -07:00
|
|
|
// Get the next index from the back.
|
2019-07-25 00:39:39 +02:00
|
|
|
//
|
2020-08-07 13:51:46 -07:00
|
|
|
// Decreasing `alive.end` by 1 maintains the invariant regarding
|
|
|
|
|
// `alive`. However, due to this change, for a short time, the alive
|
|
|
|
|
// zone is not `data[alive]` anymore, but `data[alive.start..=idx]`.
|
|
|
|
|
self.alive.next_back().map(|idx| {
|
|
|
|
|
// Read the element from the array.
|
|
|
|
|
// SAFETY: `idx` is an index into the former "alive" region of the
|
|
|
|
|
// array. Reading this element means that `data[idx]` is regarded as
|
|
|
|
|
// dead now (i.e. do not touch). As `idx` was the end of the
|
|
|
|
|
// alive-zone, the alive zone is now `data[alive]` again, restoring
|
|
|
|
|
// all invariants.
|
2020-09-09 11:27:42 +02:00
|
|
|
unsafe { self.data.get_unchecked(idx).assume_init_read() }
|
2020-08-07 13:51:46 -07:00
|
|
|
})
|
2019-07-25 00:39:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 10:50:21 +02:00
|
|
|
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
impl<T, const N: usize> Drop for IntoIter<T, N> {
|
2021-09-21 04:04:50 -04:00
|
|
|
#[rustc_insignificant_dtor]
|
2019-07-25 00:39:39 +02:00
|
|
|
fn drop(&mut self) {
|
2019-10-25 18:11:20 +02:00
|
|
|
// SAFETY: This is safe: `as_mut_slice` returns exactly the sub-slice
|
|
|
|
|
// of elements that have not been moved out yet and that remain
|
|
|
|
|
// to be dropped.
|
2019-12-22 17:42:04 -05:00
|
|
|
unsafe { ptr::drop_in_place(self.as_mut_slice()) }
|
2019-07-25 00:39:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 10:50:21 +02:00
|
|
|
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
impl<T, const N: usize> ExactSizeIterator for IntoIter<T, N> {
|
2019-07-25 00:39:39 +02:00
|
|
|
fn len(&self) -> usize {
|
|
|
|
|
// Will never underflow due to the invariant `alive.start <=
|
|
|
|
|
// alive.end`.
|
|
|
|
|
self.alive.end - self.alive.start
|
|
|
|
|
}
|
|
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
|
self.alive.is_empty()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 10:50:21 +02:00
|
|
|
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
impl<T, const N: usize> FusedIterator for IntoIter<T, N> {}
|
2019-07-25 00:39:39 +02:00
|
|
|
|
|
|
|
|
// The iterator indeed reports the correct length. The number of "alive"
|
|
|
|
|
// elements (that will still be yielded) is the length of the range `alive`.
|
|
|
|
|
// This range is decremented in length in either `next` or `next_back`. It is
|
|
|
|
|
// always decremented by 1 in those methods, but only if `Some(_)` is returned.
|
2019-10-25 10:50:21 +02:00
|
|
|
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
unsafe impl<T, const N: usize> TrustedLen for IntoIter<T, N> {}
|
2019-07-25 00:39:39 +02:00
|
|
|
|
2019-10-25 10:50:21 +02:00
|
|
|
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
impl<T: Clone, const N: usize> Clone for IntoIter<T, N> {
|
2019-07-25 00:39:39 +02:00
|
|
|
fn clone(&self) -> Self {
|
2020-08-07 13:51:46 -07:00
|
|
|
// 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: 0..0 };
|
|
|
|
|
|
|
|
|
|
// Clone all alive elements.
|
2021-03-08 15:32:41 -08:00
|
|
|
for (src, dst) in iter::zip(self.as_slice(), &mut new.data) {
|
2020-08-07 13:51:46 -07:00
|
|
|
// Write a clone into the new array, then update its alive range.
|
|
|
|
|
// If cloning panics, we'll correctly drop the previous items.
|
|
|
|
|
dst.write(src.clone());
|
|
|
|
|
new.alive.end += 1;
|
2019-07-25 00:39:39 +02:00
|
|
|
}
|
2020-08-07 13:51:46 -07:00
|
|
|
|
|
|
|
|
new
|
2019-07-25 00:39:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 10:50:21 +02:00
|
|
|
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
|
2020-07-05 15:02:01 +03:00
|
|
|
impl<T: fmt::Debug, const N: usize> fmt::Debug for IntoIter<T, N> {
|
2019-07-25 00:39:39 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
// Only print the elements that were not yielded yet: we cannot
|
|
|
|
|
// access the yielded elements anymore.
|
2019-12-22 17:42:04 -05:00
|
|
|
f.debug_tuple("IntoIter").field(&self.as_slice()).finish()
|
2019-07-25 00:39:39 +02:00
|
|
|
}
|
|
|
|
|
}
|