pub struct Arena<T> { /* private fields */ }
Expand description

An Arena structure containing certain Nodes.

Implementations

Creates a new empty Arena.

Create a new empty Arena with pre-allocated memory for n items.

Creates a new node from its associated data.

Panics

Panics if the arena already has usize::max_value() nodes.

Examples
let mut arena = Arena::new();
let foo = arena.new_node("foo");

assert_eq!(*arena[foo].get(), "foo");

Creates a new node via specified create function.

create is called with the new node’s node ID, allowing nodes that know their own ID.

Panics

Panics if the arena already has usize::max_value() nodes.

Examples
let mut arena = Arena::new();
struct A { id: NodeId, val: u32 }
let foo = arena.new_node_with(|id| A { id, val: 10 });

assert_eq!(arena[foo].get().val, 10);
assert_eq!(arena[foo].get().id, foo);

Counts the number of nodes in arena and returns it.

Examples
let mut arena = Arena::new();
let foo = arena.new_node("foo");
let _bar = arena.new_node("bar");
assert_eq!(arena.count(), 2);

foo.remove(&mut arena);
assert_eq!(arena.count(), 1);

Returns true if arena has no nodes, false otherwise.

Examples
let mut arena = Arena::new();
assert!(arena.is_empty());

let foo = arena.new_node("foo");
assert!(!arena.is_empty());

foo.remove(&mut arena);
assert!(arena.is_empty());

Returns a reference to the node with the given id if in the arena.

Returns None if not available.

Examples
let mut arena = Arena::new();
let foo = arena.new_node("foo");
assert_eq!(arena.get(foo).map(|node| *node.get()), Some("foo"));

Note that this does not check whether the given node ID is created by the arena.

let mut arena = Arena::new();
let foo = arena.new_node("foo");
let bar = arena.new_node("bar");
assert_eq!(arena.get(foo).map(|node| *node.get()), Some("foo"));

let mut another_arena = Arena::new();
let _ = another_arena.new_node("Another arena");
assert_eq!(another_arena.get(foo).map(|node| *node.get()), Some("Another arena"));
assert!(another_arena.get(bar).is_none());

Returns a mutable reference to the node with the given id if in the arena.

Returns None if not available.

Examples
let mut arena = Arena::new();
let foo = arena.new_node("foo");
assert_eq!(arena.get(foo).map(|node| *node.get()), Some("foo"));

*arena.get_mut(foo).expect("The `foo` node exists").get_mut() = "FOO!";
assert_eq!(arena.get(foo).map(|node| *node.get()), Some("FOO!"));

Get a pair of exclusive references to the elements at index i1 and i2 if it is in the arena.

If the element at index i1 or i2 is not in the arena, then None is returned for this element.

Panics

Panics if i1 and i2 are pointing to the same item of the arena.

Examples
use generational_indextree::Arena;

let mut arena = Arena::new();
let idx1 = arena.new_node("foo");
let idx2 = arena.new_node("bar");

{
    let (item1, item2) = arena.get2_mut(idx1, idx2);

    *item1.unwrap().get_mut() = "jig";
    *item2.unwrap().get_mut() = "saw";
}

assert_eq!(arena[idx1].get(), &"jig");
assert_eq!(arena[idx2].get(), &"saw");

Returns an iterator of all nodes in the arena in storage-order.

Examples
let mut arena = Arena::new();
let _foo = arena.new_node("foo");
let _bar = arena.new_node("bar");

let mut iter = arena.iter();
assert_eq!(iter.next().map(|node| *node.get()), Some("foo"));
assert_eq!(iter.next().map(|node| *node.get()), Some("bar"));
assert_eq!(iter.next().map(|node| *node.get()), None);
let mut arena = Arena::new();
let _foo = arena.new_node("foo");
let bar = arena.new_node("bar");
bar.remove(&mut arena);

let mut iter = arena.iter();
assert_eq!(iter.next().map(|node| *node.get()), Some("foo"));
assert_eq!(iter.next().map(|node| *node.get()), None);

Returns an iterator of all pairs (NodeId, &Node) in the arena in storage-order.

let mut arena = Arena::new();
let _foo = arena.new_node("foo");
let _bar = arena.new_node("bar");

let mut iter = arena.iter_pairs();
assert_eq!(iter.next().map(|node| (node.0, *node.1.get())), Some((_foo, "foo")));
assert_eq!(iter.next().map(|node| (node.0, *node.1.get())), Some((_bar, "bar")));
assert_eq!(iter.next().map(|node| (node.0, *node.1.get())), None);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Performs the mutable indexing (container[index]) operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.