#generational #arena #tree #graph #entity

no-std components-arena

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

37 releases (10 stable)

new 1.3.1 Jul 20, 2021
1.1.1 Sep 30, 2020
0.9.0 Sep 15, 2020

#130 in Rust patterns

Download history 38/week @ 2021-04-03 44/week @ 2021-04-10 80/week @ 2021-04-17 19/week @ 2021-04-24 11/week @ 2021-05-01 2/week @ 2021-05-08 41/week @ 2021-05-15 2/week @ 2021-05-22 4/week @ 2021-05-29 40/week @ 2021-06-05 5/week @ 2021-06-12 3/week @ 2021-06-19 62/week @ 2021-06-26 44/week @ 2021-07-03 54/week @ 2021-07-10 136/week @ 2021-07-17

137 downloads per month
Used in 3 crates

MIT/Apache

31KB
436 lines

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, ComponentClassMutex};

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

static NODE: ComponentClassMutex<Node> = ComponentClassMutex::new();

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

impl List {
    fn new() -> Self {
        List { last: None, nodes: Arena::new(&mut NODE.lock().unwrap()) }
    }

    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

~2.5MB
~48K SLoC