Skip to main content

nexus_collections/
lib.rs

1//! High-performance collections with slab-backed storage.
2//!
3//! Collections use thread-local slab allocators for O(1) insert/remove with
4//! stable handles and no allocation on the hot path.
5//!
6//! # Collections
7//!
8//! - **List** — Doubly-linked list with `RcSlot` handles and external allocation
9//! - **Heap** — Pairing heap with `RcSlot` handles and external allocation
10//! - **RbTree** — Red-black tree sorted map with deterministic O(log n) worst case
11//! - **BTree** — B-tree sorted map with cache-friendly, tunable node layout
12//!
13//! # Quick Start (RbTree)
14//!
15//! ```ignore
16//! mod levels {
17//!     nexus_collections::rbtree_allocator!(u64, String, bounded);
18//! }
19//!
20//! fn main() {
21//!     levels::Allocator::builder().capacity(1000).build().unwrap();
22//!
23//!     let mut map = levels::RbTree::new(levels::Allocator);
24//!     map.try_insert(100, "hello".into()).unwrap();
25//!     assert_eq!(map.get(&100), Some(&"hello".into()));
26//! }
27//! ```
28
29#![warn(missing_docs)]
30
31use std::cell::Cell;
32
33pub mod btree;
34pub mod compare;
35pub mod exclusive;
36pub mod heap;
37pub mod list;
38mod macros;
39pub mod rbtree;
40
41// Re-export comparison types at crate root
42pub use compare::{Compare, Natural, Reverse};
43// Re-export ExclusiveCell types at crate root
44pub use exclusive::{ExMut, ExRef, ExclusiveCell};
45// Re-export slab types, traits, and macros so users don't need nexus-slab
46// as a direct dependency. The slab is an implementation detail.
47pub use nexus_slab::{Alloc, BoundedAlloc, Full, UnboundedAlloc};
48pub use nexus_slab::{
49    bounded_allocator, bounded_rc_allocator, unbounded_allocator, unbounded_rc_allocator,
50};
51
52// =============================================================================
53// Collection identity
54// =============================================================================
55
56thread_local! {
57    static NEXT_COLLECTION_ID: Cell<usize> = const { Cell::new(1) };
58}
59
60/// Returns a unique (per-thread) collection ID for ownership checking.
61fn next_collection_id() -> usize {
62    NEXT_COLLECTION_ID.with(|c| {
63        let id = c.get();
64        c.set(id + 1);
65        id
66    })
67}