reliakit-collections
Bounded and reliability-oriented collection types for Rust.
reliakit-collections provides collection types with enforced size constraints. Bounds are expressed as const generic parameters and checked at construction time. Mutations that would violate the bounds return errors instead of panicking.
The crate has no dependencies and forbids unsafe code.
When To Use It
Use this crate when:
- a list must always have at least one element,
- a list must not exceed a known maximum size,
- you want mutation operations to be safe-by-default rather than checked at the call site,
- you are modeling domain concepts like a non-empty recipient list, a capped queue, or a fixed-size batch.
When Not To Use It
This crate covers bounded collection types. The following are out of scope:
- runtime-sized collections without known bounds, served by
std::collections, - fixed-size stack-allocated arrays, served by
[T; N], - unbounded non-empty vectors, which
reliakit-primitivesalready provides asNonEmptyVec<T>.
Installation
[]
= "1.0"
For no_std environments:
[]
= { = "1.0", = false, = ["alloc"] }
Examples
Bounded recipient list
use BoundedVec;
type RecipientList = ;
let mut recipients = new.unwrap;
recipients.push.unwrap;
assert_eq!;
Push and pop with bound enforcement
use BoundedVec;
let mut v = new.unwrap;
assert!; // at capacity
assert_eq!;
assert!; // len = 2, above minimum
assert!; // would go below minimum (1)
Exact-size collection
use BoundedVec;
// Must have exactly 3 elements
type Triple = ;
assert!;
assert!;
assert!;
Rolling window with a ring buffer
use RingBuffer;
// Keep only the most recent 3 samples.
let mut last3 = new.unwrap;
last3.push;
last3.push;
last3.push;
// Pushing onto a full buffer evicts (and returns) the oldest element.
assert_eq!;
assert_eq!;
Bounded map with unique keys
use BoundedMap;
// At most 8 feature flags, keys unique, insertion order preserved.
let mut flags = new.unwrap;
assert_eq!;
assert_eq!; // replaces, count unchanged
assert_eq!;
assert_eq!;
Bounded set of unique elements
use BoundedSet;
// Track up to 3 active sessions; duplicates are ignored, overflow is rejected.
let mut sessions = new.unwrap;
assert!; // added
assert!; // already present, no-op
assert!; // at capacity
assert!;
Available Types
| Type | Description |
|---|---|
BoundedVec<T, MIN, MAX> |
Vec<T> constrained to hold between MIN and MAX elements |
BoundedMap<K, V, MIN, MAX> |
Insertion-ordered map with unique keys and an enforced entry-count range (vec-backed, linear lookup) |
BoundedSet<T, MIN, MAX> |
Insertion-ordered set of unique elements with an enforced count range (vec-backed, linear lookup) |
RingBuffer<T> |
Fixed-capacity circular buffer that overwrites the oldest element when full |
Feature Flags
| Flag | Default | Description |
|---|---|---|
std |
yes | Enables std::error::Error for CollectionError; implies alloc |
alloc |
no | Enables BoundedVec, BoundedMap, BoundedSet, and RingBuffer (backed by alloc) |
no_std
The crate supports no_std. BoundedVec, BoundedMap, BoundedSet, and
RingBuffer require the alloc feature (enabled by default via std). The
error types (CollectionError, CollectionResult) are available without
alloc.
Safety
This crate is #![forbid(unsafe_code)].
Minimum Supported Rust Version
Rust 1.85 stable. No nightly features are used.
Status
Active. The 0.1.x API is considered stable.
Contributing
See CONTRIBUTING.md.
License
Licensed under the MIT License.