Struct IntoIter

pub struct IntoIter<T, const N: usize> { pub(in ::array::iter) inner: ManuallyDrop<PolymorphicIter<[MaybeUninit<T>; N]>> }

A by-value [array] iterator.

Fields

inner: ManuallyDrop<PolymorphicIter<[MaybeUninit<T>; N]>>

Implementations

impl<T, const N: usize> IntoIter<T, N>

fn new(array: [T; N]) -> Self

Creates a new iterator over the given array.

const unsafe fn new_unchecked(buffer: [MaybeUninit<T>; N], initialized: Range<usize>) -> Self

Creates an iterator over the elements in a partially-initialized buffer.

If you have a fully-initialized array, then use IntoIterator. But this is useful for returning partial results from unsafe code.

Safety

  • The buffer[initialized] elements must all be initialized.
  • The range must be canonical, with initialized.start <= initialized.end.
  • The range must be in-bounds for the buffer, with initialized.end <= N. (Like how indexing [0][100..100] fails despite the range being empty.)

It's sound to have more elements initialized than mentioned, though that will most likely result in them being leaked.

Examples

#![feature(array_into_iter_constructors)]
#![feature(maybe_uninit_uninit_array_transpose)]
use std::array::IntoIter;
use std::mem::MaybeUninit;

# // Hi!  Thanks for reading the code. This is restricted to `Copy` because
# // otherwise it could leak. A fully-general version this would need a drop
# // guard to handle panics from the iterator, but this works for an example.
fn next_chunk<T: Copy, const N: usize>(
    it: &mut impl Iterator<Item = T>,
) -> Result<[T; N], IntoIter<T, N>> {
    let mut buffer = [const { MaybeUninit::uninit() }; N];
    let mut i = 0;
    while i < N {
        match it.next() {
            Some(x) => {
                buffer[i].write(x);
                i += 1;
            }
            None => {
                // SAFETY: We've initialized the first `i` items
                unsafe {
                    return Err(IntoIter::new_unchecked(buffer, 0..i));
                }
            }
        }
    }

    // SAFETY: We've initialized all N items
    unsafe { Ok(buffer.transpose().assume_init()) }
}

let r: [_; 4] = next_chunk(&mut (10..16)).unwrap();
assert_eq!(r, [10, 11, 12, 13]);
let r: IntoIter<_, 40> = next_chunk(&mut (10..16)).unwrap_err();
assert_eq!(r.collect::<Vec<_>>(), vec![10, 11, 12, 13, 14, 15]);
const fn empty() -> Self

Creates an iterator over T which returns no elements.

If you just need an empty iterator, then use iter::empty() instead. And if you need an empty array, use [].

But this is useful when you need an array::IntoIter<T, N> specifically.

Examples

#![feature(array_into_iter_constructors)]
use std::array::IntoIter;

let empty = IntoIter::<i32, 3>::empty();
assert_eq!(empty.len(), 0);
assert_eq!(empty.as_slice(), &[]);

let empty = IntoIter::<std::convert::Infallible, 200>::empty();
assert_eq!(empty.len(), 0);

[1, 2].into_iter() and [].into_iter() have different types

#![feature(array_into_iter_constructors)]
use std::array::IntoIter;

pub fn get_bytes(b: bool) -> IntoIter<i8, 4> {
    if b {
        [1, 2, 3, 4].into_iter()
    } else {
        [].into_iter() // error[E0308]: mismatched types
    }
}

But using this method you can get an empty iterator of appropriate size:

#![feature(array_into_iter_constructors)]
use std::array::IntoIter;

pub fn get_bytes(b: bool) -> IntoIter<i8, 4> {
    if b {
        [1, 2, 3, 4].into_iter()
    } else {
        IntoIter::empty()
    }
}

assert_eq!(get_bytes(true).collect::<Vec<_>>(), vec![1, 2, 3, 4]);
assert_eq!(get_bytes(false).collect::<Vec<_>>(), vec![]);
fn as_slice(&self) -> &[T]

Returns an immutable slice of all elements that have not been yielded yet.

const fn as_mut_slice(&mut self) -> &mut [T]

Returns a mutable slice of all elements that have not been yielded yet.

impl<T, const N: usize> IntoIter<T, N>

const fn unsize(&self) -> &PolymorphicIter<[MaybeUninit<T>]>
const fn unsize_mut(&mut self) -> &mut PolymorphicIter<[MaybeUninit<T>]>

Trait Implementations

impl<T> OneShot for IntoIter<T, 0>

impl<T> OneShot for IntoIter<T, 1>

impl<T, const N: usize> Default for IntoIter<T, N>

fn default() -> Self

impl<T, const N: usize> DoubleEndedIterator for IntoIter<T, N>

fn next_back(&mut self) -> Option<Self::Item>
fn rfold<Acc, Fold>(self, init: Acc, rfold: Fold) -> Acc
where
    Fold: FnMut(Acc, Self::Item) -> Acc,
fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
where
    Self: Sized,
    F: FnMut(B, Self::Item) -> R,
    R: Try<Output = B>,
fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>>

impl<T, const N: usize> Drop for IntoIter<T, N>

fn drop(&mut self)

impl<T, const N: usize> ExactSizeIterator for IntoIter<T, N>

fn len(&self) -> usize
fn is_empty(&self) -> bool

impl<T, const N: usize> FusedIterator for IntoIter<T, N>

impl<T, const N: usize> Iterator for IntoIter<T, N>

type Item = T;
fn next(&mut self) -> Option<Self::Item>
fn size_hint(&self) -> (usize, Option<usize>)
fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
where
    Fold: FnMut(Acc, Self::Item) -> Acc,
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
where
    Self: Sized,
    F: FnMut(B, Self::Item) -> R,
    R: Try<Output = B>,
fn count(self) -> usize
fn last(self) -> Option<Self::Item>
fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item

impl<T, const N: usize> TrustedLen for IntoIter<T, N>

impl<T, const N: usize> TrustedRandomAccessNoCoerce for IntoIter<T, N> where T: NonDrop,

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl<T: Clone, const N: usize> Clone for IntoIter<T, N>

fn clone(&self) -> IntoIter<T, N>

impl<T: Debug, const N: usize> Debug for IntoIter<T, N>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Auto Trait Implementations

impl<T, const N: usize> Freeze for IntoIter<T, N> where ManuallyDrop<PolymorphicIter<[MaybeUninit<T>; N]>>: Freeze,

impl<T, const N: usize> RefUnwindSafe for IntoIter<T, N> where ManuallyDrop<PolymorphicIter<[MaybeUninit<T>; N]>>: RefUnwindSafe,

impl<T, const N: usize> Send for IntoIter<T, N> where ManuallyDrop<PolymorphicIter<[MaybeUninit<T>; N]>>: Send,

impl<T, const N: usize> Sync for IntoIter<T, N> where ManuallyDrop<PolymorphicIter<[MaybeUninit<T>; N]>>: Sync,

impl<T, const N: usize> Unpin for IntoIter<T, N> where ManuallyDrop<PolymorphicIter<[MaybeUninit<T>; N]>>: Unpin,

impl<T, const N: usize> UnsafeUnpin for IntoIter<T, N> where ManuallyDrop<PolymorphicIter<[MaybeUninit<T>; N]>>: UnsafeUnpin,

impl<T, const N: usize> UnwindSafe for IntoIter<T, N> where ManuallyDrop<PolymorphicIter<[MaybeUninit<T>; N]>>: UnwindSafe,

Blanket Implementations

impl<I> IntoIterator for IntoIter<T, N> where I: Iterator,

type Item = <I as Iterator>::Item;
type IntoIter = I;
fn into_iter(self) -> I

impl<T> Any for IntoIter<T, N> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for IntoIter<T, N> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for IntoIter<T, N> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for IntoIter<T, N> where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for IntoIter<T, N>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for IntoIter<T, N> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for IntoIter<T, N>

impl<T, U> Into<U> for IntoIter<T, N> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for IntoIter<T, N> where U: Into<T>,

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto<U> for IntoIter<T, N> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>