Crate tiny_vec

Source
Expand description

Tiny Vec

A dynamic array that can store a small amount of elements on the stack.

This struct provides a vec-like API, but performs small-vector optimization. This means that a TinyVec<T, N> stores up to N elements on the stack. If the vector grows bigger than that, it moves the contents to the heap.

§Example

use tiny_vec::TinyVec;

let mut tv = TinyVec::<u8, 16>::new();

for n in 0..16 {
    tv.push(n);
}

// Up to this point, no heap allocations are needed.
// All the elements are stored on the stack.

tv.push(123); // This moves the vector to the heap

assert_eq!(&tv[..], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
                      10, 11, 12, 13, 14, 15, 123])

§Memory layout

For a TinyVec<T, N>

On the stack (length <= N)

  • [T; N] : Data
  • usize : Length

On the heap (length > N)

  • T* : Data
  • usize : Capacity
  • usize : Length

If N is equal to sizeof (T*, usize) / sizeof T, the TinyVec is the same size as a regular vector
NOTE: The n_elements_for_stack function returns the maximun number of elements for a type, such that it doesn’t waste extra space when moved to the heap

Modules§

drain
drain implementation
extract_if
extract_if implementation for TinyVec
iter
Iterator implementation for TinyVec

Macros§

tinyvec
Macro to create TinyVecs

Structs§

TinyVec
A dynamic array that can store a small amount of elements on the stack.

Functions§

n_elements_for_bytes
The maximun number of elements of type T, that can be stored on the given byte size
n_elements_for_stack
The maximun number of elements that can be stored in the stack for the vector, without incrementing it’s size