pub struct ArrayBuilder<T, const N: usize> { /* private fields */ }
Expand description
Build an array dynamically without heap allocations.
See module documentation for more.
Implementations§
Source§impl<T, const N: usize> ArrayBuilder<T, N>
impl<T, const N: usize> ArrayBuilder<T, N>
Sourcepub fn push(&mut self, item: T) -> &mut Self
pub fn push(&mut self, item: T) -> &mut Self
Insert an item into the builder.
If the builder is already full, the item is immediately dropped.
Sourcepub fn build_pad(&mut self, item: T) -> Result<[T; N], Error>where
T: Clone,
pub fn build_pad(&mut self, item: T) -> Result<[T; N], Error>where
T: Clone,
Pad out the array, returning an Err
if there were too many calls to Self::push
.
The builder remains unchanged in the Err
case.
let arr = ArrayBuilder::<_, 3>::new().push("first").build_pad("padding").unwrap();
assert_eq!(arr, ["first", "padding", "padding"]);
ArrayBuilder::<_, 1>::new().push("first").push("too many now!").build_pad("").unwrap_err();
Sourcepub fn build_pad_truncate(&mut self, item: T) -> [T; N]where
T: Clone,
pub fn build_pad_truncate(&mut self, item: T) -> [T; N]where
T: Clone,
Pad out the array, ignoring if there were too many calls to Self::push
.
The builder is restored to an empty state.
let arr = ArrayBuilder::<_, 3>::new().push("first").build_pad_truncate("padding");
assert_eq!(arr, ["first", "padding", "padding"]);
let arr =
ArrayBuilder::<_, 1>::new().push("first").push("too many now!").build_pad_truncate("");
assert_eq!(arr, ["first"]);
Sourcepub fn build_truncate(&mut self) -> Result<[T; N], Error>
pub fn build_truncate(&mut self) -> Result<[T; N], Error>
Build the array, ignoring if there were too many calls to Self::push
.
The builder is restored to an empty state, and remains unchanged in the
Err
case.
let arr = ArrayBuilder::<_, 1>::new().push("first").push("ignored").build_truncate().unwrap();
assert_eq!(arr, ["first"]);
ArrayBuilder::<&str, 1>::new().build_truncate().unwrap_err();
Sourcepub fn build_exact(&mut self) -> Result<[T; N], Error>
pub fn build_exact(&mut self) -> Result<[T; N], Error>
Require exactly N
calls to Self::push
.
The builder remains unchanged in the Err
case.
ArrayBuilder::<_, 2>::new().push("too few").build_exact().unwrap_err();
ArrayBuilder::<_, 2>::new().push("way").push("too").push("many").build_exact().unwrap_err();
ArrayBuilder::<_, 2>::new().push("just").push("right").build_exact().unwrap();
Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Return the current collection of items in the array.
Does not include excess items.
Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Return the current collection of items in the array.
Does not include excess items.
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 moreSource§impl<T: Default, const N: usize> Default for ArrayBuilder<T, N>
impl<T: Default, const N: usize> Default for ArrayBuilder<T, N>
Source§fn default() -> ArrayBuilder<T, N>
fn default() -> ArrayBuilder<T, N>
Source§impl<T, const N: usize> Extend<T> for ArrayBuilder<T, N>
impl<T, const N: usize> Extend<T> for ArrayBuilder<T, N>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)