pub struct BumpVec<T> { /* private fields */ }
Expand description

A vector of T stored within a BumpArena.

This is something like a normal Vec, except that all accesses and updates require a separate borrow of the BumpArena. This, in turn, makes the Vec itself very compact: only three u32s (12 bytes). The BumpSlice variant is only two u32s (8 bytes) and is sufficient to reconstruct a slice, but not grow the vector.

The BumpVec does not implement Clone or Copy; it represents unique ownership of a range of indices in the arena. If dropped, those indices will be unavailable until the arena is freed. This is “fine” (it is normally how arena allocation works). To explicitly free and make available for some allocations, a very rudimentary reuse mechanism exists via BumpVec::free(arena). (The allocation path opportunistically checks the first range on the freelist, and can carve off a piece of it if larger than needed, but it does not attempt to traverse the entire freelist; this is a compromise between bump-allocation speed and memory efficiency, which also influences speed through cached-memory reuse.)

The type T should not have a Drop implementation. This typically means that it does not own any boxed memory, sub-collections, or other resources. This is important for the efficiency of the data structure (otherwise, to call Drop impls, the arena needs to track which indices are live or dead; the BumpVec itself cannot do the drop because it does not retain a reference to the arena). Note that placing a T with a Drop impl in the arena is still safe, because leaking (that is, never calling Drop::drop()) is safe. It is merely less efficient, and so should be avoided if possible.

Implementations§

source§

impl<T> BumpVec<T>

source

pub fn as_slice<'a>(&'a self, arena: &'a BumpArena<T>) -> &'a [T]

Returns a slice view of this BumpVec, given a borrow of the arena.

source

pub fn as_mut_slice<'a>(&'a mut self, arena: &'a mut BumpArena<T>) -> &'a mut [T]

Returns a mutable slice view of this BumpVec, given a mutable borrow of the arena.

source

pub fn len(&self) -> usize

Returns the length of this vector. Does not require access to the arena.

source

pub fn cap(&self) -> usize

Returns the capacity of this vector. Does not require access to the arena.

source

pub fn reserve(&mut self, extra_len: usize, arena: &mut BumpArena<T>)

Reserve extra_len capacity at the end of the vector, reallocating if necessary.

source

pub fn push(&mut self, t: T, arena: &mut BumpArena<T>)

Push an item, growing the capacity if needed.

source

pub fn clone(&self, arena: &mut BumpArena<T>) -> BumpVec<T>where T: Clone,

Clone, if T is cloneable.

source

pub fn truncate(&mut self, len: usize)

Truncate the length to a smaller-or-equal length.

source

pub fn free(self, arena: &mut BumpArena<T>)

Consume the BumpVec and return its indices to a free pool in the arena.

source

pub fn freeze(self, arena: &mut BumpArena<T>) -> BumpSlice<T>

Freeze the capacity of this BumpVec, turning it into a slice, for a smaller struct (8 bytes rather than 12). Once this exists, it is copyable, because the slice will never be freed.

Trait Implementations§

source§

impl<T: Debug> Debug for BumpVec<T>

source§

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

Formats the value using the given formatter. Read more
source§

impl<T> Default for BumpVec<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for BumpVec<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.