pub struct ArrayBuilder<T, const N: usize> { /* private fields */ }
Expand description
Misuse-immune array builder
This ArrayBuilder
uses move semantics to provide an array builder that
never panics or returns errors. Each call to push
takes self
by move, and returns either the builder (if it’s not full yet)
or the fully initialized array (if it is). The builder therefore can only
exist while the array being built isn’t full yet.
use brownstone::move_builder::{ArrayBuilder, PushResult};
let builder = match ArrayBuilder::start() {
PushResult::Full(_) => unreachable!(),
PushResult::NotFull(builder) => builder,
};
assert!(builder.is_empty());
let builder = match builder.push(5) {
PushResult::Full(_) => unreachable!(),
PushResult::NotFull(builder) => builder,
};
assert_eq!(builder.len(), 1);
let builder = match builder.push(6) {
PushResult::Full(_) => unreachable!(),
PushResult::NotFull(builder) => builder,
};
assert_eq!(builder.len(), 2);
assert_eq!(builder.finished_slice(), [5, 6]);
let array = match builder.push(7) {
PushResult::Full(array) => array,
PushResult::NotFull(_) => unreachable!(),
};
assert_eq!(array, [5, 6, 7]);
Implementations§
Source§impl<T, const N: usize> ArrayBuilder<T, N>
impl<T, const N: usize> ArrayBuilder<T, N>
Sourcepub fn start() -> PushResult<T, N>
pub fn start() -> PushResult<T, N>
Create a new ArrayBuilder
. If N == 0
, immediately return an empty
array, rather than the builder.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of initialized elements in the array. Guaranteed to
be less than N
.
Sourcepub fn push(self, value: T) -> PushResult<T, N>
pub fn push(self, value: T) -> PushResult<T, N>
Add a new initialized element to the array. If this causes the array to become fully initialized, the array is returned; otherwise, a new builder is returned.
Sourcepub fn finished_slice(&self) -> &[T]
pub fn finished_slice(&self) -> &[T]
Get the slice of the array that has already been initialized.
Sourcepub fn finished_slice_mut(&mut self) -> &mut [T]
pub fn finished_slice_mut(&mut self) -> &mut [T]
Get the mutable slice of the array that has already been initialized.
Trait Implementations§
Source§impl<T: Clone, const N: usize> Clone for ArrayBuilder<T, N>
impl<T: Clone, const N: usize> Clone for ArrayBuilder<T, N>
Source§fn clone(&self) -> ArrayBuilder<T, N>
fn clone(&self) -> ArrayBuilder<T, N>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more