Struct rapier3d::data::arena::Arena[][src]

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

The Arena allows inserting and removing elements that are referred to by Index.

See the module-level documentation for example usage and motivation.

Implementations

Constructs a new, empty Arena.

Examples

use rapier::data::arena::Arena;

let mut arena = Arena::<usize>::new();

Constructs a new, empty Arena<T> with the specified capacity.

The Arena<T> will be able to hold n elements without further allocation.

Examples

use rapier::data::arena::Arena;

let mut arena = Arena::with_capacity(10);

// These insertions will not require further allocation.
for i in 0..10 {
    assert!(arena.try_insert(i).is_ok());
}

// But now we are at capacity, and there is no more room.
assert!(arena.try_insert(99).is_err());

Clear all the items inside the arena, but keep its allocation.

Examples

use rapier::data::arena::Arena;

let mut arena = Arena::with_capacity(1);
arena.insert(42);
arena.insert(43);

arena.clear();

assert_eq!(arena.capacity(), 2);

Attempts to insert value into the arena using existing capacity.

This method will never allocate new capacity in the arena.

If insertion succeeds, then the value’s index is returned. If insertion fails, then Err(value) is returned to give ownership of value back to the caller.

Examples

use rapier::data::arena::Arena;

let mut arena = Arena::new();

match arena.try_insert(42) {
    Ok(idx) => {
        // Insertion succeeded.
        assert_eq!(arena[idx], 42);
    }
    Err(x) => {
        // Insertion failed.
        assert_eq!(x, 42);
    }
};

Attempts to insert the value returned by create into the arena using existing capacity. create is called with the new value’s associated index, allowing values that know their own index.

This method will never allocate new capacity in the arena.

If insertion succeeds, then the new index is returned. If insertion fails, then Err(create) is returned to give ownership of create back to the caller.

Examples

use rapier::data::arena::{Arena, Index};

let mut arena = Arena::new();

match arena.try_insert_with(|idx| (42, idx)) {
    Ok(idx) => {
        // Insertion succeeded.
        assert_eq!(arena[idx].0, 42);
        assert_eq!(arena[idx].1, idx);
    }
    Err(x) => {
        // Insertion failed.
    }
};

Insert value into the arena, allocating more capacity if necessary.

The value’s associated index in the arena is returned.

Examples

use rapier::data::arena::Arena;

let mut arena = Arena::new();

let idx = arena.insert(42);
assert_eq!(arena[idx], 42);

Insert the value returned by create into the arena, allocating more capacity if necessary. create is called with the new value’s associated index, allowing values that know their own index.

The new value’s associated index in the arena is returned.

Examples

use rapier::data::arena::{Arena, Index};

let mut arena = Arena::new();

let idx = arena.insert_with(|idx| (42, idx));
assert_eq!(arena[idx].0, 42);
assert_eq!(arena[idx].1, idx);

Remove the element at index i from the arena.

If the element at index i is still in the arena, then it is returned. If it is not in the arena, then None is returned.

Examples

use rapier::data::arena::Arena;

let mut arena = Arena::new();
let idx = arena.insert(42);

assert_eq!(arena.remove(idx), Some(42));
assert_eq!(arena.remove(idx), None);

Retains only the elements specified by the predicate.

In other words, remove all indices such that predicate(index, &value) returns false.

Examples

use rapier::data::arena::Arena;

let mut crew = Arena::new();
crew.extend(&["Jim Hawkins", "John Silver", "Alexander Smollett", "Israel Hands"]);
let pirates = ["John Silver", "Israel Hands"]; // too dangerous to keep them around
crew.retain(|_index, member| !pirates.contains(member));
let mut crew_members = crew.iter().map(|(_, member)| **member);
assert_eq!(crew_members.next(), Some("Jim Hawkins"));
assert_eq!(crew_members.next(), Some("Alexander Smollett"));
assert!(crew_members.next().is_none());

Is the element at index i in the arena?

Returns true if the element at i is in the arena, false otherwise.

Examples

use rapier::data::arena::Arena;

let mut arena = Arena::new();
let idx = arena.insert(42);

assert!(arena.contains(idx));
arena.remove(idx);
assert!(!arena.contains(idx));

Get a shared reference to the element at index i if it is in the arena.

If the element at index i is not in the arena, then None is returned.

Examples

use rapier::data::arena::Arena;

let mut arena = Arena::new();
let idx = arena.insert(42);

assert_eq!(arena.get(idx), Some(&42));
arena.remove(idx);
assert!(arena.get(idx).is_none());

Get an exclusive reference to the element at index i if it is in the arena.

If the element at index i is not in the arena, then None is returned.

Examples

use rapier::data::arena::Arena;

let mut arena = Arena::new();
let idx = arena.insert(42);

*arena.get_mut(idx).unwrap() += 1;
assert_eq!(arena.remove(idx), Some(43));
assert!(arena.get_mut(idx).is_none());

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 rapier::data::arena::Arena;

let mut arena = Arena::new();
let idx1 = arena.insert(0);
let idx2 = arena.insert(1);

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

    *item1.unwrap() = 3;
    *item2.unwrap() = 4;
}

assert_eq!(arena[idx1], 3);
assert_eq!(arena[idx2], 4);

Get the length of this arena.

The length is the number of elements the arena holds.

Examples

use rapier::data::arena::Arena;

let mut arena = Arena::new();
assert_eq!(arena.len(), 0);

let idx = arena.insert(42);
assert_eq!(arena.len(), 1);

let _ = arena.insert(0);
assert_eq!(arena.len(), 2);

assert_eq!(arena.remove(idx), Some(42));
assert_eq!(arena.len(), 1);

Returns true if the arena contains no elements

Examples

use rapier::data::arena::Arena;

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

let idx = arena.insert(42);
assert!(!arena.is_empty());

assert_eq!(arena.remove(idx), Some(42));
assert!(arena.is_empty());

Get the capacity of this arena.

The capacity is the maximum number of elements the arena can hold without further allocation, including however many it currently contains.

Examples

use rapier::data::arena::Arena;

let mut arena = Arena::with_capacity(10);
assert_eq!(arena.capacity(), 10);

// `try_insert` does not allocate new capacity.
for i in 0..10 {
    assert!(arena.try_insert(1).is_ok());
    assert_eq!(arena.capacity(), 10);
}

// But `insert` will if the arena is already at capacity.
arena.insert(0);
assert!(arena.capacity() > 10);

Allocate space for additional_capacity more elements in the arena.

Panics

Panics if this causes the capacity to overflow.

Examples

use rapier::data::arena::Arena;

let mut arena = Arena::with_capacity(10);
arena.reserve(5);
assert_eq!(arena.capacity(), 15);

Iterate over shared references to the elements in this arena.

Yields pairs of (Index, &T) items.

Order of iteration is not defined.

Examples

use rapier::data::arena::Arena;

let mut arena = Arena::new();
for i in 0..10 {
    arena.insert(i * i);
}

for (idx, value) in arena.iter() {
    println!("{} is at index {:?}", value, idx);
}

Iterate over exclusive references to the elements in this arena.

Yields pairs of (Index, &mut T) items.

Order of iteration is not defined.

Examples

use rapier::data::arena::Arena;

let mut arena = Arena::new();
for i in 0..10 {
    arena.insert(i * i);
}

for (_idx, value) in arena.iter_mut() {
    *value += 5;
}

Iterate over elements of the arena and remove them.

Yields pairs of (Index, T) items.

Order of iteration is not defined.

Note: All elements are removed even if the iterator is only partially consumed or not consumed at all.

Examples

use rapier::data::arena::Arena;

let mut arena = Arena::new();
let idx_1 = arena.insert("hello");
let idx_2 = arena.insert("world");

assert!(arena.get(idx_1).is_some());
assert!(arena.get(idx_2).is_some());
for (idx, value) in arena.drain() {
    assert!((idx == idx_1 && value == "hello") || (idx == idx_2 && value == "world"));
}
assert!(arena.get(idx_1).is_none());
assert!(arena.get(idx_2).is_none());

Given an i of usize without a generation, get a shared reference to the element and the matching Index of the entry behind i.

This method is useful when you know there might be an element at the position i, but don’t know its generation or precise Index.

Use cases include using indexing such as Hierarchical BitMap Indexing or other kinds of bit-efficient indexing.

You should use the get method instead most of the time.

Given an i of usize without a generation, get an exclusive reference to the element and the matching Index of the entry behind i.

This method is useful when you know there might be an element at the position i, but don’t know its generation or precise Index.

Use cases include using indexing such as Hierarchical BitMap Indexing or other kinds of bit-efficient indexing.

You should use the get_mut method instead most of the time.

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

Extends a collection with the contents of an iterator. Read more

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

Extends a collection with exactly one element.

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

Reserves capacity in a collection for the given number of additional elements. Read more

Creates a value from an iterator. Read more

The returned type after indexing.

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

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

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

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait. Read more

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait. Read more

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s. Read more

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s. Read more

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait. Read more

Performs the conversion.

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

Should always be Self

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more

Checks if self is actually part of its subset T (and can be converted to it).

Use with care! Same as self.to_subset but without any property checks. Always succeeds.

The inclusion map: converts self to the equivalent element of its superset.

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)

recently added

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.