#generational #arena #tree #graph #entity

no-std components-arena

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

63 releases (36 stable)

Uses new Rust 2021

3.4.0 Nov 23, 2022
3.3.5 Jun 27, 2022
3.2.1 May 28, 2022
2.2.0 May 4, 2022
0.9.0 Sep 15, 2020

#193 in Rust patterns

Download history 64/week @ 2022-08-15 116/week @ 2022-08-22 50/week @ 2022-08-29 171/week @ 2022-09-05 98/week @ 2022-09-12 32/week @ 2022-09-19 48/week @ 2022-09-26 44/week @ 2022-10-03 33/week @ 2022-10-10 13/week @ 2022-10-17 28/week @ 2022-10-24 80/week @ 2022-10-31 21/week @ 2022-11-07 20/week @ 2022-11-14 62/week @ 2022-11-21 32/week @ 2022-11-28

135 downloads per month
Used in 3 crates

MIT/Apache

52KB
989 lines

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–1.6MB
~34K SLoC