pub struct DirectPool<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle = DefaultHandle> { /* private fields */ }
Expand description

A direct-mapped object pool with constant capacity.

See the super module documentation for information on pool-based memory management, and this module’s documentation for details on this variation of it.

Implementations

Returns the number of elements the pool can hold.

Returns the number of elements currently in the pool.

Returns true if the pool contains no elements.

Returns true if the pool contains the maximum number of elements.

Returns true if the specified handle is valid for this pool.

Examples
let mut pool: DirectArenaPool<u128, DefaultHandle> = arena.with_capacity(8);
let h = pool.insert(0xDEAD_BEEF);
assert!(pool.contains(h));
pool.remove(h);
assert!(!pool.contains(h));

Returns a reference to the value corresponding to the handle.

Returns None if the handle is invalid for this pool.

Returns a mutable reference to the value corresponding to the handle.

Returns None if the handle is invalid for this pool.

Returns mutable references to the values corresponding to the specified handles.

Returns None if any one of the handles is invalid, or if any two of them are equal to each other.

Examples
let mut pool: DirectArenaPool<&'static str, DefaultHandle> = arena.with_capacity(8);
let ha = pool.insert("apple");
let hb = pool.insert("berry");
let hc = pool.insert("coconut");
assert!(pool.get_disjoint_mut([ha, ha]).is_none());
if let Some([a, c]) = pool.get_disjoint_mut([ha, hc]) {
    core::mem::swap(a, c);
}
assert_eq!(pool[ha], "coconut");
assert_eq!(pool[hc], "apple");

Inserts a value into the pool, returning a unique handle to access it.

Returns Err(value) if the pool is already at capacity.

Examples
let mut pool: DirectArenaPool<u128, DefaultHandle> = arena.with_capacity(8);
let h = pool.try_insert(42)?;
assert_eq!(pool[h], 42);

Inserts a value into the pool, returning a unique handle to access it.

Panics

Panics if the pool is already full. See try_insert for a checked version.

Inserts a value given by f into the pool. The handle where the value will be stored is passed into f. This is useful for storing values containing their own handle.

Returns None if the pool is already full, without calling f.

Examples
let mut pool: DirectArenaPool<(DefaultHandle, u64), DefaultHandle> = arena.with_capacity(10);
let h = pool.insert_with_handle(|h| (h, 20));
assert_eq!(pool[h], (h, 20));

Inserts a value given by f into the pool. The handle where the value will be stored is passed into f. This is useful for storing values containing their own handle.

Panics

Panics if the pool is already full. See try_insert_with_handle for a checked version.

Removes the value referred to by the specified handle from the pool, returning it unless the handle is invalid. This invalidates the handle.

Examples
let mut pool: DirectArenaPool<u128, DefaultHandle> = arena.with_capacity(8);
let h = pool.insert(42);
assert_eq!(pool.remove(h), Some(42));
assert_eq!(pool.remove(h), None);

Retains only the elements specified by the predicate.

In other words, remove all handle-value pairs (h, t) such that f(h, &mut t) returns false. This method invalidates any removed handles.

This function must iterate over all slots, empty or not. In the face of many deleted elements it can be inefficient.

Examples
let mut pool: DirectArenaPool<u128, DefaultHandle> = arena.with_capacity(4);
let h1 = pool.insert(1);
let h2 = pool.insert(2);
let h3 = pool.insert(3);
pool.retain(|_, val| *val % 2 == 1);

assert!(pool.contains(h1));
assert!(!pool.contains(h2));
assert!(pool.contains(h3));
assert_eq!(pool.len(), 2);

Clears the pool, dropping all values. This invalidates all handles.

This function must iterate over all slots, empty or not. In the face of many deleted elements it can be inefficient.

Examples
let mut pool: DirectArenaPool<u128, DefaultHandle> = arena.with_capacity(5);
for i in 0..5 {
    pool.insert(i);
}

assert!(pool.is_full());
pool.clear();
assert!(pool.is_empty());

Creates an iterator visiting all handle-value pairs in arbitrary order, yielding (H, &'a T).

This iterator must visit all slots, empty or not. In the face of many deleted elements it can be inefficient.

Examples
let mut pool: DirectArenaPool<u128, DefaultHandle> = arena.with_capacity(5);
let h0 = pool.insert(0);
let h1 = pool.insert(1);
let h2 = pool.insert(2);

let mut counts = [0, 0, 0];
for (h, v) in pool.iter() {
    if h == h0 && v == &0 { counts[0] += 1; }
    else if h == h1 && v == &1 { counts[1] += 1; }
    else if h == h2 && v == &2 { counts[2] += 1; }
}

assert_eq!(counts, [1, 1, 1]);

Creates an iterator visiting all handle-value pairs in arbitrary order, yielding (H, &'a mut T).

This iterator must visit all slots, empty or not. In the face of many deleted elements it can be inefficient.

Examples
let mut pool: DirectArenaPool<u128, DefaultHandle> = arena.with_capacity(5);
let h1 = pool.insert(1);
let h2 = pool.insert(2);
let h3 = pool.insert(3);

for (k, v) in pool.iter_mut() {
    *v *= 2;
}

assert_eq!(pool[h1], 2);
assert_eq!(pool[h2], 4);
assert_eq!(pool[h3], 6);

Creates an iterator yielding all valid handles in arbitrary order.

This iterator must visit all slots, empty or not. In the face of many deleted elements it can be inefficient.

Examples
let mut pool: DirectArenaPool<u128, DefaultHandle> = arena.with_capacity(5);
assert!(pool.handles().next().is_none());

let h0 = pool.insert(10);
let h1 = pool.insert(20);
let h2 = pool.insert(30);

let mut counts = [0, 0, 0];
for handle in pool.handles() {
    if handle == h0 { counts[0] += 1; }
    else if handle == h1 { counts[1] += 1; }
    else if handle == h2 { counts[2] += 1; }
}

assert_eq!(counts, [1, 1, 1]);

Creates an iterator yielding references to all stored values in arbitrary order.

This iterator must visit all slots, empty or not. In the face of many deleted elements it can be inefficient.

Examples
let mut pool: DirectArenaPool<u128, DefaultHandle> = arena.with_capacity(5);
assert!(pool.values().next().is_none());

let h0 = pool.insert(10);
let h1 = pool.insert(20);
let h2 = pool.insert(30);

let mut counts = [0, 0, 0];
for value in pool.values() {
    if *value == 10 { counts[0] += 1; }
    else if *value == 20 { counts[1] += 1; }
    else if *value == 30 { counts[2] += 1; }
}

assert_eq!(counts, [1, 1, 1]);

Creates an iterator yielding mutable references to all stored values in arbitrary order.

This iterator must visit all slots, empty or not. In the face of many deleted elements it can be inefficient.

Examples
let mut pool: DirectArenaPool<u128, DefaultHandle> = arena.with_capacity(5);
assert!(pool.values_mut().next().is_none());

let h0 = pool.insert(10);
let h1 = pool.insert(20);
let h2 = pool.insert(30);
pool.values_mut().for_each(|n| *n *= 2);

assert_eq!(pool[h0], 20);
assert_eq!(pool[h1], 40);
assert_eq!(pool[h2], 60);

Creates a draining iterator that removes all values from the pool and yields them and their handles in an arbitrary order.

When the iterator is dropped, all remaining elements are removed from the pool, even if the iterator was not fully consumed. If the iterator is not dropped (with core::mem::forget for example), it is unspecified how many elements are removed.

This iterator must visit all slots, empty or not. In the face of many deleted elements it can be inefficient.

Examples
let mut pool: DirectArenaPool<u128, DefaultHandle> = arena.with_capacity(5);
pool.insert(0);
let mut it = pool.drain();
assert!(matches!(it.next(), Some((_, 0))));
assert!(it.next().is_none());
drop(it);

assert_eq!(pool.len(), 0);

Creates an iterator which uses a closure to determine if an element should be removed.

If the closure returns true, the element is removed and yielded with its handle. If the closure returns false, the element will remain in the pool and will not be yielded by the iterator.

When the iterator is dropped, all remaining elements matching the filter are removed from the pool, even if the iterator was not fully consumed. If the iterator is not dropped (with core::mem::forget for example), it is unspecified how many such elements are removed.

This iterator must visit all slots, empty or not. In the face of many deleted elements it can be inefficient.

Examples
let mut pool: DirectArenaPool<u128, DefaultHandle> = arena.with_capacity(10);
for i in 1..=10 {
    pool.insert(i);
}

// filter out the even values:
let mut it = pool.drain_filter(|_, val| *val % 2 == 0);
for i in 1..=5 {
    assert!(matches!(it.next(), Some((_, x)) if x == 2 * i));
}
assert!(it.next().is_none());
drop(it);

assert_eq!(pool.len(), 5);
This is supported on crate feature alloc only.

Constructs a new, empty DirectAllocPool with the specified capacity.

Panics

Panics if the specified capacity is greater than or equal to H::MAX_INDEX.

Constructs a new, empty DirectPool backed by InlineStorage.

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

Executes the destructor for this type. Read more

Performs the conversion.

The returned type after indexing.

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

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

Which kind of iterator are we turning this into?

The type of the elements being iterated over.

Creates an iterator from a value. Read more

Which kind of iterator are we turning this into?

The type of the elements being iterated over.

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

Performs the conversion.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

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.