Expand description
Stack-allocated data structures with similar APIs to heap-allocated types.
This crate provides fixed-size, stack-allocated alternatives to common heap-allocated data structures from the standard library. These types are useful in environments where heap allocation is unavailable, restricted, or undesirable (such as embedded systems, kernels, interrupt handlers, or early boot code).
§Overview
The crate includes the following data structures:
Always available (no-alloc):
vec::ArrayVec- A fixed-capacity vector backed by a stack-allocated arrayringbuf::RingBuf- A fixed-capacity circular/ring buffer for FIFO operations
With the alloc feature:
smallvec::SmallVec- A vector that stores elements inline then spills to the heapsmallstr::SmallStr- A UTF-8 string backed bySmallVecsmallbytestr::SmallByteStr- A byte string backed bySmallVec
§When to Use This Crate
Use planck-noalloc when:
- You’re working in a
no_stdenvironment without an allocator - You need predictable memory usage and performance
- You want to avoid heap fragmentation
- You’re in an interrupt handler or other restricted context
- Maximum size is known at compile time
§Examples
§Using ArrayVec
use planck_noalloc::vec::ArrayVec;
// Create a vector that can hold up to 5 elements on the stack
let mut vec = ArrayVec::<i32, 5>::new();
vec.push(1);
vec.push(2);
vec.push(3);
assert_eq!(vec.len(), 3);
assert_eq!(vec[0], 1);
for value in vec.iter() {
println!("{}", value);
}§Using RingBuf
use planck_noalloc::ringbuf::RingBuf;
// Create a ring buffer that can hold up to 7 elements (SIZE-1)
let mut buf = RingBuf::<u8, 8>::new();
buf.push(1);
buf.push(2);
buf.push(3);
assert_eq!(buf.pop(), Some(1));
assert_eq!(buf.pop(), Some(2));
assert_eq!(buf.len(), 1);§Features
std(default): Enables std-specific features and impliesallocalloc: Enables types that can spill to the heap (SmallVec,SmallStr,SmallByteStr)
§Performance Characteristics
All operations have the same time complexity as their heap-allocated counterparts:
- Push/pop: O(1)
- Index access: O(1)
- Iteration: O(n)
However, these types avoid heap allocation overhead and have better cache locality since data is stored inline on the stack.
Modules§
- ringbuf
- Circular/ring buffer implementation with fixed capacity.
- smallbytestr
- A byte string backed by
SmallVec. - smallstr
- A UTF-8 string backed by
SmallVec. - smallvec
- A vector that stores elements inline up to a fixed capacity, then spills to the heap.
- vec
- Fixed-capacity vector implementation backed by a stack-allocated array.