Struct ArrayConsumer

Source
#[repr(C)]
pub struct ArrayConsumer<T, const N: usize> { /* private fields */ }
Available on crate feature rust_1_83 only.
Expand description

Const analog of core::array::IntoIter

This isn’t called IntoIter because it does not implement the ConstIntoIter trait, as this type does not have the API that that trait requires

§Example

use konst::array::{ArrayBuilder, ArrayConsumer};
 
use core::mem::ManuallyDrop as MD;
 
assert_eq!(ARR, [21, 13, 8, 5, 3]);
 
const ARR: [u32; 5] = reverse([3, 5, 8, 13, 21]);
 
const fn reverse<T, const LEN: usize>(arr: [T; LEN]) -> [T; LEN] {
    let mut iter = ArrayConsumer::new(arr);
    let mut builder = ArrayBuilder::new();
 
    while let Some(item) = iter.next_back() {
        builder.push(MD::into_inner(item));
    }
 
    // necessary to avoid "destructor cannot be evaluated at compile-time" error
    iter.assert_is_empty();
 
    builder.build()
}

Implementations§

Source§

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

Source

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

Constructs an ArrayConsumer from an array.

Source

pub const fn empty() -> Self

Constructs an already-consumed ArrayConsumer.

§Example
use konst::array::ArrayConsumer;
 
let mut iter = ArrayConsumer::<u8, 4>::empty();
 
assert_eq!(iter.next(), None);
 
Source

pub const fn assert_is_empty(self)

Asserts that the ArrayConsumer is empty, allows using ArrayConsumer in const.

§Example
use konst::array::ArrayConsumer;
 
use core::mem::ManuallyDrop as MD;
 
assert_eq!(SUM, 16);
 
const SUM: u64 = summer(ArrayConsumer::new([3, 5, 8]));
 
const fn summer<const N: usize>(mut iter: ArrayConsumer<u64, N>) -> u64 {
    let mut sum = 0u64;
    while let Some(item) = iter.next() {
        sum += MD::into_inner(item);
    }
    iter.assert_is_empty();
    sum
}
Source

pub const fn as_slice(&self) -> &[T]

Gets the remainder of the array as a slice

§Example
use konst::array::ArrayConsumer;
 
let mut iter = ArrayConsumer::new([3, 5, 8]);
 
assert_eq!(iter.as_slice(), &[3, 5, 8][..]);
 
assert!(iter.next().is_some());
assert_eq!(iter.as_slice(), &[5, 8][..]);
 
assert!(iter.next().is_some());
assert_eq!(iter.as_slice(), &[8][..]);
 
assert!(iter.next().is_some());
assert_eq!(iter.as_slice(), &[][..]);
 
assert_eq!(iter.next(), None);
assert_eq!(iter.as_slice(), &[][..]);
Source

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

Gets the remainder of the array as a mutable slice

§Example
use konst::array::ArrayConsumer;
 
let mut iter = ArrayConsumer::new([3, 5, 8]);
 
assert_eq!(iter.as_mut_slice(), &mut [3, 5, 8][..]);
 
assert!(iter.next().is_some());
assert_eq!(iter.as_mut_slice(), &mut [5, 8][..]);
 
assert!(iter.next().is_some());
assert_eq!(iter.as_mut_slice(), &mut [8][..]);
 
assert!(iter.next().is_some());
assert_eq!(iter.as_mut_slice(), &mut [][..]);
 
assert_eq!(iter.next(), None);
assert_eq!(iter.as_mut_slice(), &mut [][..]);
Source

pub const fn copy(&self) -> Self
where T: Copy,

Gets a bitwise copy of this ArrayConsumer, requires T: Copy.

Source

pub const fn next(&mut self) -> Option<ManuallyDrop<T>>

Gets the next element from the array

Due to limitations of const eval as of Rust 1.83.0, this function returns a ManuallyDrop<T> to be able to return a T: Drop in an Option, you’ll need to call ManuallyDrop::into_inner to get T and avoid leaking it.

§Example
use konst::array::ArrayConsumer;
 
use std::mem::ManuallyDrop as MD;
 
let mut iter = ArrayConsumer::new([3, 5, 8]);
 
assert_eq!(iter.as_slice(), &[3, 5, 8][..]);
 
assert_eq!(iter.next(), Some(MD::new(3)));
assert_eq!(iter.as_slice(), &[5, 8][..]);
 
assert_eq!(iter.next(), Some(MD::new(5)));
assert_eq!(iter.as_slice(), &[8][..]);
 
assert_eq!(iter.next(), Some(MD::new(8)));
assert_eq!(iter.as_slice(), &[][..]);
 
assert_eq!(iter.next(), None);
assert_eq!(iter.as_slice(), &[][..]);
Source

pub const fn next_back(&mut self) -> Option<ManuallyDrop<T>>

Gets the next element from the end of the array

Due to limitations of const eval as of Rust 1.83.0, this function returns a ManuallyDrop<T> to be able to return a T: Drop in an Option, you’ll need to call ManuallyDrop::into_inner to get T and avoid leaking it.

§Example
use konst::array::ArrayConsumer;
 
use std::mem::ManuallyDrop as MD;
 
let mut iter = ArrayConsumer::new([3, 5, 8]);
 
assert_eq!(iter.as_slice(), &[3, 5, 8][..]);
 
assert_eq!(iter.next_back(), Some(MD::new(8)));
assert_eq!(iter.as_slice(), &[3, 5][..]);
 
assert_eq!(iter.next_back(), Some(MD::new(5)));
assert_eq!(iter.as_slice(), &[3][..]);
 
assert_eq!(iter.next_back(), Some(MD::new(3)));
assert_eq!(iter.as_slice(), &[][..]);
 
assert_eq!(iter.next_back(), None);
assert_eq!(iter.as_slice(), &[][..]);

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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

Source§

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

Formats the value using the given formatter. Read more
Source§

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

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for ArrayConsumer<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for ArrayConsumer<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for ArrayConsumer<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for ArrayConsumer<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for ArrayConsumer<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnwindSafe for ArrayConsumer<T, N>
where T: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

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

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

impl<T> Identity for T
where T: ?Sized,

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.