slotpool/lib.rs
1//! Fixed-capacity object pools for `no_std` / embedded systems.
2//!
3//! Two pool variants, sharing the same pluggable-backend architecture:
4//!
5//! | Variant | Allocate | Drop | Use case |
6//! |---|---|---|---|
7//! | [`BoxPool`] | `alloc(value)` — writes a new value | runs `T`'s destructor | owned objects |
8//! | [`ObjectPool`] | `request()` — no value, previous data persists | no destructor | buffers, messages |
9//!
10//! - Static memory, no heap allocation.
11//! - Safe sharing between ISRs and async tasks.
12//! - Pluggable backends via generic `B`; default is [`CriticalSlots`](critical::CriticalSlots).
13//! Enable `feature = "cas"` for [`CasSlots`](cas::CasSlots).
14//!
15//! ## BoxPool — owned values
16//!
17//! ```ignore
18//! use slotpool::{BoxPool, StaticBoxPool};
19//!
20//! static POOL: StaticBoxPool<[u8; 256], 8> = StaticBoxPool::new();
21//! let pool: &'static BoxPool<[u8; 256], 8> = POOL.init_pool();
22//!
23//! let guard = pool.alloc([0u8; 256]).unwrap();
24//! // guard is returned to the pool when dropped; destructor runs
25//! ```
26//!
27//! ## ObjectPool — persistent buffers / messages
28//!
29//! ```ignore
30//! use slotpool::{ObjectPool, StaticObjectPool};
31//!
32//! static POOL: StaticObjectPool<[u8; 256], 8> = StaticObjectPool::new();
33//! let pool: &'static ObjectPool<[u8; 256], 8> = POOL.init_pool([0u8; 256]);
34//!
35//! let mut buf = pool.request().unwrap();
36//! buf[0..4].copy_from_slice(&header);
37//! // buf is returned to the pool when dropped; no destructor — data persists
38//! ```
39//!
40//! ## Backend selection
41//!
42//! ```ignore
43//! use slotpool::critical::CriticalSlots;
44//! #[cfg(feature = "cas")]
45//! use slotpool::cas::CasSlots;
46//!
47//! type MyPool = StaticBoxPool<[u8; 256], 8>; // default: CriticalSlots
48//! type MyCasPool = StaticBoxPool<[u8; 256], 8, CasSlots<8>>; // CAS backend
49//! ```
50//!
51//! The free-index chain is stored separately from `T`'s slots — this is **not**
52//! an intrusive free list, so slot memory layout is unaffected.
53
54#![cfg_attr(not(test), no_std)]
55
56mod free_slots;
57
58#[cfg(feature = "cas")]
59pub mod cas;
60pub mod critical;
61
62mod boxed;
63mod object;
64
65#[cfg(feature = "async")]
66pub mod async_boxed;
67#[cfg(feature = "async")]
68pub mod async_object;
69
70pub use boxed::{BoxGuard, BoxPool, StaticBoxPool};
71pub use object::{ObjectGuard, ObjectPool, StaticObjectPool};