libutils-array 0.2.4

A maximum-sized array stored on the stack
Documentation

Array

Array<Type, N> is an stack-allocated vector of maximum capacity N, but not restricted to having an exact amount of elements as [_; N].

Usage

use libutils::array::Array;

let mut array = Array::<u8, 5>::new();

array.push(0);
array.push(3);

array.extend([1, 2]);

The Array type exposes an API similar to that of Vec with common functions:

  • .push(Type) -> (),
  • .pop() -> Option<Type>,
  • .extend(IntoIterator<Item = Type>) -> (),
  • .clear() -> (),
  • .insert(usize, Type) -> (),
  • .remove(usize) -> Type,
  • .retain(impl FnMut(&mut Type) -> bool) -> ()

The type implements Deref<Target = [Type]> along with DerefMut and DerefPure to access the methods of the slice type.

Furthermore, most of its methods and implementations use cutting-edge nightly const-features, which allows for complex compile-time constants:

use libutils::array::Array;

static ARRAY: Array<u8, 6> = const {
    let mut instance = Array::<u8, 6>::from([1, 2, 3]);
    instance.push(4);
    instance.insert(0, 0);
    instance.pop().unwrap();
    instance
};

Performance

Array is stack-allocated, which means it does not make use of any allocator and maintains items inlined on the runtime stack or in the program memory.

Since data does not need to travel from RAM but rather stays on CPU cache, it is faster than a regular Vec.

When to use this type

This type should be used when you need to store a finite and reasonable number of elements in a list, and you care about performance.