#[repr(C)]pub struct ArrayConsumer<T, const N: usize> { /* private fields */ }
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>
impl<T, const N: usize> ArrayConsumer<T, N>
Sourcepub const fn empty() -> Self
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);
Sourcepub const fn assert_is_empty(self)
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
}
Sourcepub const fn as_slice(&self) -> &[T]
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(), &[][..]);
Sourcepub const fn as_mut_slice(&mut self) -> &mut [T]
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 [][..]);
Sourcepub const fn copy(&self) -> Selfwhere
T: Copy,
pub const fn copy(&self) -> Selfwhere
T: Copy,
Gets a bitwise copy of this ArrayConsumer, requires T: Copy
.
Sourcepub const fn next(&mut self) -> Option<ManuallyDrop<T>>
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(), &[][..]);
Sourcepub const fn next_back(&mut self) -> Option<ManuallyDrop<T>>
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(), &[][..]);