Crate embed_collections

Crate embed_collections 

Source
Expand description

§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).

§Modules

  • dlist: Intrusive Doubly Linked List.
  • slist: Intrusive Singly Linked List (FIFO Queue).

§Example

use embed_collections::{dlist::{DLinkedList, ListItem, ListNode}, Pointer};
use std::cell::UnsafeCell;

struct MyItem {
    val: i32,
    link: UnsafeCell<ListNode<MyItem, ()>>,
}

impl MyItem {
    fn new(val: i32) -> Self {
        Self {
            val,
            link: UnsafeCell::new(ListNode::default()),
        }
    }
}

unsafe impl ListItem<()> for MyItem {
    fn get_node(&self) -> &mut ListNode<Self, ()> {
        unsafe { &mut *self.link.get() }
    }
}

let mut list = DLinkedList::<Box<MyItem>, ()>::new();
list.push_back(Box::new(MyItem::new(10)));
list.push_back(Box::new(MyItem::new(20)));

assert_eq!(list.pop_front().unwrap().val, 10);
assert_eq!(list.pop_front().unwrap().val, 20);

Modules§

dlistdlist
An intrusive doubly linked list implementation.
slistslist
An intrusive singly linked list implementation.

Traits§

Pointer
Abstract pointer trait to support various pointer types in collections.