brownstone::move_builder

Struct ArrayBuilder

Source
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>

Source

pub fn start() -> PushResult<T, N>

Create a new ArrayBuilder. If N == 0, immediately return an empty array, rather than the builder.

Source

pub fn is_empty(&self) -> bool

Returns true if there are no initialized elements in the array.

Source

pub fn len(&self) -> usize

Returns the number of initialized elements in the array. Guaranteed to be less than N.

Source

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.

Source

pub fn finished_slice(&self) -> &[T]

Get the slice of the array that has already been initialized.

Source

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>

Source§

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

Returns a copy 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 ArrayBuilder<T, N>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T, const N: usize> UnwindSafe for ArrayBuilder<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, dst: *mut u8)

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.