Skip to content

andreiavrammsd/static_vector.rs

Repository files navigation

static_vector

build codecov fuzz documentation license

A no-std, stack-allocated vector with fixed capacity and dynamic length: static_vector::Vec::<T, CAPACITY>.

[Vec] stores elements on the stack using a fixed-size array without heap allocations.

Aims to be suitable for low-level projects and to have an API as safe and explicit as possible. The goal is to allocate only when needed. When first constructed, the vector will not allocate.

Note: It's a learning project, so there are no guarantees.

Features

  • No heap allocation (#![no_std] compatible)
  • Supports iteration, mutable access, clearing, resizing
  • Compile-time enforced capacity

Requirements

  • CAPACITY > 0, otherwise [Vec::new()] panics
  • T: Default only if [Vec::set_len()] is used

Complexity

All operations are O(1) except:

Method Time Complexity Space Complexity
clear O(current length) O(1)
set_len O(new length - current length) O(new length - current length)
extend_from_slice O(slice length) O(slice length)
append O(other vector length) O(other vector length)

Add to project

cargo add static_vector --git https://github.com/andreiavrammsd/static_vector.rs

This crate is not published on crates.io.

Example

use static_vector::vec;

// Create a static vector with capacity 3, length 3, and elements 1, 2, 3.
let mut vec = vec![1, 2, 3];

vec.pop().unwrap();
assert_eq!(vec.len(), 2);

vec.push(4).unwrap();
assert_eq!(vec.len(), 3);
assert_eq!(vec.last(), Some(&4));

assert_eq!(vec.as_slice(), &[1, 2, 4]);

let sum_of_even_numbers = vec.iter().filter(|n| *n % 2 == 0).sum::<i32>();
assert_eq!(sum_of_even_numbers, 6);

vec.push(2).unwrap_err();
assert_eq!(vec.len(), 3);

match vec.set_len(1) {
    Ok(()) => assert_eq!(vec.len(), 1),
    Err(err) => eprintln!("{:?}", err),
}

vec.clear();
assert!(vec.is_empty());

See more examples in the documentation of [Vec] and in examples.

Development on Linux

  • Install Rust (stable is used for main code, nightly is used only for code formatting and fuzz tests).
  • If using VS Code, see setup.
  • See Makefile.

About

A no-std, stack-allocated vector with fixed capacity and dynamic length

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •