Embed Collections
embed-collections provides intrusive data structures for Rust. Unlike standard collections,
intrusive collections require the elements to store the node data (links) themselves.
This allows for:
- Memory Efficiency: No extra allocation for nodes.
- Deterministic Memory Management: You control where the node lives.
- Flexibility: Works with various pointer types (
Box,Arc,Rc,NonNull, raw pointers).
Difference to crate intrusive-collections:
This crate choose to use DListItem::get_node() instead of c like offset_of!, mainly because:
-
Mangling with offset conversion makes the code hard to read (for people not used to c style coding).
-
You don't have to understand some complex macro style.
-
It's dangerous to use pointer offset conversion when the embedded Node not perfectly aligned, and using memtion to return the node ref is more safer approach. (For example, the default
repr(Rust) might reorder the field, or you mistakenly userepr(packed))
There're three usage scenarios:
-
Push smart pointer to the list, so that the list hold 1 ref count when the type is
Arc/Rc, but you have to use UnsafeCell for internal mutation. -
Push
Boxto the list, the list own the items until they are popped, it's better than std LinkedList because no additional allocation is needed. -
Push raw pointer (better use NonNull instead of *const T for smaller footprint) to the list, for temporary usage. You must ensure the list item not dropped be other refcount (for example, the item is holding by Arc in other structure).
Modules
- [
dlist]: Intrusive Doubly Linked List. - [
slist]: Intrusive Singly Linked List (FIFO Queue).
Example
use ;
use UnsafeCell;
unsafe
let mut list = new;
list.push_back;
list.push_back;
assert_eq!;
assert_eq!;