prefix-arena
prefix-arena is a small no_std crate for carving initialized prefixes out of
caller-provided byte storage.
It is aimed at code that needs predictable, allocation-free memory handling: embedded firmware, packet encoders/decoders, serializers, and other situations where a growing prefix is useful but a heap allocator is not.
What it provides
PrefixArenafor detaching prefixes from the front of a mutable byte buffer.ArenaViewfor temporarily inspecting or initializing the remaining arena.StagingBufferfor writing into arena-backed storage before committing the written prefix.StagingBufferErrorfor capacity overflow when staging output.
Features
no_std- No global allocator
- Backed by caller-owned storage
- Explicit handling of initialized vs uninitialized bytes
- Suitable for incremental writing into fixed-size buffers
Installation
Add the crate to your Cargo.toml:
[]
= "0.1"
Basic usage
Detach a prefix directly from the arena:
use PrefixArena;
let mut storage = ;
let arena = new;
let prefix = arena
.init_prefix_with
.unwrap;
assert_eq!;
Stage bytes and commit only what was written:
use ;
let mut storage = ;
let mut arena = new;
let mut staging = new;
staging.extend_from_slice.unwrap;
staging.push_byte.unwrap;
staging.extend_from_slice.unwrap;
let written = staging.into_written_slice;
assert_eq!;
assert_eq!;
When to use which type
- Use
PrefixArenawhen you already know how many bytes you want to detach. - Use
ArenaViewwhen you need temporary access to the remaining buffer. - Use
StagingBufferwhen output is assembled incrementally and should only be committed after the final size is known.
Safety
This crate intentionally distinguishes between initialized and uninitialized storage.
Safe methods only expose initialized u8 slices when the written prefix length
is known. Unsafe methods such as unchecked slice access require the caller to
ensure that the referenced bytes are initialized before any read occurs.
no_std
The crate uses core only and is suitable for no_std environments.
Documentation
API documentation is intended to be published on docs.rs:
License
Licensed under either of:
- MIT license
- Apache License, Version 2.0
at your option.