#generational #arena #tree #graph #entity

no-std components-arena

Simple library for creating complex domain-specific self-referential data structures

72 releases (45 stable)

new 4.1.3 Feb 19, 2024
4.1.1 Sep 23, 2023
3.5.1 Apr 21, 2023
3.5.0 Jan 26, 2023
0.9.0 Sep 15, 2020

#337 in Rust patterns

Download history 191/week @ 2023-10-31 64/week @ 2023-11-07 45/week @ 2023-11-14 128/week @ 2023-11-21 258/week @ 2023-11-28 179/week @ 2023-12-05 34/week @ 2023-12-12 113/week @ 2023-12-19 292/week @ 2023-12-26 31/week @ 2024-01-02 41/week @ 2024-01-09 37/week @ 2024-01-16 111/week @ 2024-01-23 297/week @ 2024-01-30 50/week @ 2024-02-06 417/week @ 2024-02-13

881 downloads per month
Used in 3 crates

MIT/Apache

61KB
1K SLoC

maintenance: actively developed

components-arena

Strong-typed arena. Simple library for creating complex domain-specific self-referential data structures.

This arena does not use generations approach in a strict sense, but it uses some similar technique for avoiding the ABA effect.

Example: circular linked list

use std::mem::replace;
use macro_attr_2018::macro_attr;
use components_arena::{Id, Arena, Component};

macro_attr! {
    #[derive(Component!)]
    struct Node {
        next: Id<Node>,
        data: (),
    }
}

struct List {
    last: Option<Id<Node>>,
    nodes: Arena<Node>,
}

impl List {
    fn new() -> Self {
        List { last: None, nodes: Arena::new() }
    }

    fn push(&mut self, data: ()) -> Id<Node> {
        let id = self.nodes.insert(|id| (Node { next: id, data }, id));
        if let Some(last) = self.last {
            self.nodes[id].next = replace(&mut self.nodes[last].next, id);
        } else {
            self.last = Some(id);
        }
        id
    }
}

Dependencies

~1.2–2.2MB
~47K SLoC