[][src]Struct static_alloc::fixed_vec::FixedVec

pub struct FixedVec<'a, T> { /* fields omitted */ }

A Vec-like structure that does not manage its allocation.

This vector type will never (re-)allocate but it can also not free underused memory. As opposed to other similar crates, it does require and additional bounds on its type parameter as it truly manages uninitialized memory to store instances.

Basic Usage

Advanaced Usage

One focus of the library is composability. It should not be surprising that FixedVec interacts with the Slab allocator, which implements a specialized interface providing the Uninit type instead of a raw *const u8. Hence, the FixedVec can use this instead of its own local stack variables.

let alloc: Slab<[u8; 1 << 12]> = Slab::uninit();
let some_usize = alloc.leak(0_usize).unwrap();

let mut vec: FixedVec<&usize> = FixedVec::from_available(
    alloc.get_layout(Layout::new::<[&usize; 1]>()).unwrap().uninit);
// Push the reference to the other allocation.
vec.push(some_usize);

// … do something else

// Ensure lifetimes work out.
drop(vec);

Methods

impl<'_, T> FixedVec<'_, T>[src]

pub fn as_slice(&self) -> &[T][src]

Extracts a slice containing the entire vector.

pub fn as_mut_slice(&mut self) -> &mut [T][src]

Extracts the mutable slice containing the entire vector.

pub fn len(&self) -> usize[src]

Returns the number of elements in the vector.

pub fn capacity(&mut self) -> usize[src]

Returns the number of elements the vector can hold.

pub fn is_empty(&self) -> bool[src]

Returns true if the vector contains no elements.

pub fn push(&mut self, val: T) -> Result<(), T>[src]

Appends an element to the back of a collection.

Return Err(val) if it is not possible to append the element.

use static_alloc::{FixedVec, Uninit};
use core::mem::MaybeUninit;

// Only enough storage for one element.
let mut allocation: MaybeUninit<[u32; 1]> = MaybeUninit::uninit();
let uninit = Uninit::from_maybe_uninit(&mut allocation);
let mut vec = FixedVec::from_available(uninit);

// First push succeeds.
assert_eq!(vec.push(1), Ok(()));

// The second push fails.
assert_eq!(vec.push(2), Err(2));

pub fn pop(&mut self) -> Option<T>[src]

Removes the last element from a vector and returns it, or None if it is empty.

pub fn split_and_shrink_to(&mut self, at: usize) -> Self[src]

Split the capacity of the collection into two at a given index.

In contrast to Vec::split_off calling this method reduces the capacity of self to at.

Panics

This method panics if at > self.capacity().

impl<'a, T> FixedVec<'a, T>[src]

pub fn new(uninit: Uninit<'a, [T]>) -> Self[src]

Create a FixedVec in a pre-allocated region.

The capacity will be that of the underlying allocation.

pub fn from_available<U>(generic: Uninit<'a, U>) -> Self[src]

Create a FixedVec with as large of a capacity as available.

When no aligned slice can be create within the provided memory then the constructor will fallback to an empty dangling slice.

This is only a utility function.

pub fn shrink_to_fit(&mut self) -> Uninit<'a, ()>[src]

Return trailing bytes that can not be used by the FixedVec.

This operation is idempotent.

Trait Implementations

impl<'_, T> DerefMut for FixedVec<'_, T>[src]

impl<'_, T> Deref for FixedVec<'_, T>[src]

type Target = [T]

The resulting type after dereferencing.

impl<'_, T> Drop for FixedVec<'_, T>[src]

Auto Trait Implementations

impl<'a, T> Unpin for FixedVec<'a, T> where
    T: Unpin

impl<'a, T> !Send for FixedVec<'a, T>

impl<'a, T> !Sync for FixedVec<'a, T>

Blanket Implementations

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]