Skip to main content

FrontBackVecBuilder

Struct FrontBackVecBuilder 

Source
pub(crate) struct FrontBackVecBuilder<T> {
    vec: Vec<T>,
    capacity: usize,
    front_len: usize,
    back_len: usize,
}
Expand description

Builds a Vec of a fixed length that is known up front by pushing items to either its front or its back.

Items pushed to the front end up at the start of the Vec in the order that they were pushed. Items pushed to the back end up at the end of the Vec in the reverse of the order that they were pushed. This allows a list of items to be partitioned into two groups in a single pass and with a single allocation.

Exactly capacity items must be pushed before calling into_vec, which panics otherwise. If the FrontBackVecBuilder is dropped before then (or if too many items are pushed), then items that were pushed to it are leaked rather than dropped.

Fields§

§vec: Vec<T>

The underlying storage. Its length is kept at zero until into_vec is called, and its allocated capacity is at least capacity.

§capacity: usize

The total number of items that the Vec will contain

§front_len: usize

The number of items pushed to the front. The slots 0..front_len are initialized.

§back_len: usize

The number of items pushed to the back. The slots (capacity - back_len)..capacity are initialized.

Implementations§

Source§

impl<T> FrontBackVecBuilder<T>

Source

pub(crate) fn with_capacity(capacity: usize) -> Self

Create a new FrontBackVecBuilder that will contain exactly capacity items

Source

pub(crate) fn push_front(&mut self, value: T)

Add an item after the items that have already been pushed to the front.

Pushing more than capacity items in total is not checked here: it either panics or overwrites (and leaks) a previously pushed item, and then causes into_vec to panic.

Source

pub(crate) fn push_back(&mut self, value: T)

Add an item before the items that have already been pushed to the back.

Pushing more than capacity items in total is not checked here: it either panics or overwrites (and leaks) a previously pushed item, and then causes into_vec to panic.

Source

pub(crate) fn into_vec(self) -> (Vec<T>, usize)

Returns the completed Vec, along with the number of items that were pushed to the front (which is also the index of the first item that was pushed to the back).

Panics if the total number of items that have been pushed is not exactly capacity.

Auto Trait Implementations§

§

impl<T> Freeze for FrontBackVecBuilder<T>

§

impl<T> RefUnwindSafe for FrontBackVecBuilder<T>
where T: RefUnwindSafe,

§

impl<T> Send for FrontBackVecBuilder<T>
where T: Send,

§

impl<T> Sync for FrontBackVecBuilder<T>
where T: Sync,

§

impl<T> Unpin for FrontBackVecBuilder<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for FrontBackVecBuilder<T>

§

impl<T> UnwindSafe for FrontBackVecBuilder<T>
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> 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.