embed-collections 0.1.0

A collection of intrusive data structures (linked lists) for Rust, supporting various pointer types.
Documentation
embed-collections-0.1.0 has been yanked.

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);