Inplace Containers
Zero-allocation, stack-allocated container types for Rust. inplace_containers provides InplaceVector and InplaceString — high-performance alternatives to Vec and String when heap allocation is undesirable.
Features
- Fully stack-allocated, fixed-capacity containers.
- No heap allocations.
- API familiar to Rust’s standard library (
Vec,String). - Iterators,
IntoIterator, andExtendsupport. - Clone, Debug, PartialEq, Hash, Ord, and conversions implemented.
- UTF-8 correctness for
InplaceString. - Compile-time capacity checks with
inplace_vec!macro. - Optional
nightlyfeature enables unstable Pattern-gated APIs (requires nightly Rust).
Nightly
To enable unstable Pattern-gated APIs, build with nightly and the nightly feature:
[]
= { = "0.3.1", = ["nightly"] }
Containers
InplaceVector<T, N>
A fixed-capacity vector that stores N elements of type T inline.
use InplaceVector;
let mut vec = new;
vec.push;
vec.push;
vec.extend_from_slice;
assert_eq!;
assert!;
let last = vec.pop;
assert_eq!;
Key methods reference:
| Method | Signature | Description |
|---|---|---|
new |
fn new() -> Self |
Creates an empty vector |
len |
fn len(&self) -> usize |
Returns current length |
is_empty |
fn is_empty(&self) -> bool |
Checks if vector is empty |
is_full |
fn is_full(&self) -> bool |
Checks if vector reached capacity |
capacity |
fn capacity(&self) -> usize |
Returns fixed capacity |
remaining_capacity |
fn remaining_capacity(&self) -> usize |
Returns remaining capacity |
push |
fn push(&mut self, value: T) |
Adds element, panics if full |
try_push |
fn try_push(&mut self, value: T) -> Result<(), InplaceError> |
Adds element safely |
pop |
fn pop(&mut self) -> Option<T> |
Removes last element |
insert |
fn insert(&mut self, idx: usize, value: T) |
Inserts at index |
remove |
fn remove(&mut self, idx: usize) -> T |
Removes element at index |
swap_remove |
fn swap_remove(&mut self, idx: usize) -> T |
Removes element, replaces with last |
extend_from_slice |
fn extend_from_slice(&mut self, slice: &[T]) |
Appends elements from slice |
truncate |
fn truncate(&mut self, new_len: usize) |
Shortens vector |
clear |
fn clear(&mut self) |
Removes all elements |
split_off |
fn split_off(&mut self, at: usize) -> Self |
Splits vector at index |
drain |
fn drain<R>(&mut self, range: R) -> InplaceVector<T, N> |
Extracts range |
InplaceString<N>
A fixed-capacity, stack-allocated string type.
use InplaceString;
let mut s: = new;
s.push_str;
s.push;
s.push_str;
assert_eq!;
assert_eq!;
Key methods reference:
| Method | Signature | Description |
|---|---|---|
new |
fn new() -> Self |
Creates an empty string |
len |
fn len(&self) -> usize |
Returns length in bytes |
is_empty |
fn is_empty(&self) -> bool |
Checks if empty |
capacity |
fn capacity(&self) -> usize |
Returns fixed capacity |
remaining_capacity |
fn remaining_capacity(&self) -> usize |
Returns remaining capacity |
push |
fn push(&mut self, ch: char) |
Appends a char, panics if full |
try_push |
fn try_push(&mut self, ch: char) -> Result<(), InplaceError> |
Safe char push |
push_str |
fn push_str(&mut self, s: &str) |
Appends string, panics if full |
try_push_str |
fn try_push_str(&mut self, s: &str) -> Result<(), InplaceError> |
Safe string push |
insert |
fn insert(&mut self, idx: usize, ch: char) |
Inserts char at index |
insert_str |
fn insert_str(&mut self, idx: usize, s: &str) |
Inserts string at index |
remove |
fn remove(&mut self, idx: usize) -> char |
Removes char at index |
pop |
fn pop(&mut self) -> Option<char> |
Removes last char |
clear |
fn clear(&mut self) |
Clears the string |
truncate |
fn truncate(&mut self, new_len: usize) |
Shortens string to new_len |
split_off |
fn split_off(&mut self, at: usize) -> Self |
Splits string at index |
into_bytes |
fn into_bytes(self) -> InplaceVector<u8, N> |
Converts to byte vector |
as_bytes |
fn as_bytes(&self) -> &[u8] |
Returns byte slice |
as_mut_bytes |
unsafe fn as_mut_bytes(&mut self) -> &mut [u8] |
Mutable byte slice |
Macros
-
inplace_string![CAP; "literal"]- creates anInplaceStringwith explicit capacity. -
inplace_string![CAP;]- creates an emptyInplaceStringwith explicit capacity. -
inplace_vec![...]– stack-allocated vector creation with optional compile-time capacity checking. -
inplace_string!("...")– creates anInplaceStringfrom a literal.
use ;
let vec = inplace_vec!;
let s = inplace_string!;
let s2 = inplace_string!;
let s3 = inplace_string!;
Safety Notes
unsafeis used internally for performance.- Methods like
unchecked_push,unchecked_insert, andset_lenbypass checks. - Always ensure capacity is not exceeded to avoid undefined behavior.
InplaceStringUTF-8 safety is only guaranteed for literals or checked strings.
License
MIT OR Apache-2.0