#generational #arena #tree #graph #entity

no-std components-arena

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

32 releases (5 stable)

new 1.1.0 Sep 21, 2020
0.9.0 Sep 15, 2020

#112 in Rust patterns

Download history 25/week @ 2020-08-06 72/week @ 2020-08-13 67/week @ 2020-08-20 158/week @ 2020-08-27 286/week @ 2020-09-03 145/week @ 2020-09-10 75/week @ 2020-09-17

277 downloads per month
Used in 3 crates

MIT/Apache

30KB
435 lines

travis

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

~1.1–1.8MB
~40K SLoC