#arena #object #immutability #reference #cyclic #instance #immutable-once-built

immutable_arena

An arena for immutable-once-built objects with possibly cyclic references

2 releases

Uses old Rust 2015

0.1.1 Jun 17, 2016
0.1.0 Jun 17, 2016

#12 in #cyclic

21 downloads per month

MIT license

9KB
153 lines

immutable_arena: an arena for immutable-once-built objects with cyclic refs

Build Status

crates.io

Documentation

This crate implements an arena for objects that are immutable once they are built, aside from references to other objects in the arena. The user creates objects once at allocation time, then after objects exist, may set special smart-pointers (Ref instances) to other objects on the arena exactly once. Subsequently, these objects are completely immutable.

Example usage:

use immutable_arena::{Arena, Ref};

struct S<'arena> {
    id: u32,
    next: Ref<'arena, S<'arena>>,
}

fn alloc_cycle<'arena>(arena: &'arena Arena<S<'arena>>)
    -> &'arena S<'arena> {
    let s1 = arena.alloc(S { id: 1, next: Ref::empty() });
    let s2 = arena.alloc(S { id: 2, next: Ref::empty() });
    s1.next.set(s2);
    s2.next.set(s1);
    s1
}

fn test_cycle() {
    let arena = Arena::new();
    let s1 = alloc_cycle(&arena);
    assert!(s1.next.next.id == s1.id);
}

immutable_arena is Copyright (c) 2016 by Chris Fallin <cfallin@c1f.net> and is released under the MIT license. See the LICENSE file for details.

Dependencies

~36KB