Struct coca::collections::pool::direct::DirectPool
source · [−]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
sourceimpl<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> DirectPool<T, S, H>
impl<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> DirectPool<T, S, H>
sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Returns true
if the pool contains the maximum number of elements.
sourcepub fn get(&self, handle: H) -> Option<&T>
pub fn get(&self, handle: H) -> Option<&T>
Returns a reference to the value corresponding to the handle.
Returns None
if the handle is invalid for this pool.
sourcepub fn get_mut(&mut self, handle: H) -> Option<&mut T>
pub fn get_mut(&mut self, handle: H) -> Option<&mut T>
Returns a mutable reference to the value corresponding to the handle.
Returns None
if the handle is invalid for this pool.
sourcepub fn get_disjoint_mut<const N: usize>(
&mut self,
handles: [H; N]
) -> Option<[&mut T; N]>
pub fn get_disjoint_mut<const N: usize>(
&mut self,
handles: [H; N]
) -> Option<[&mut T; N]>
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");
sourcepub fn try_insert(&mut self, value: T) -> Result<H, T>
pub fn try_insert(&mut self, value: T) -> Result<H, T>
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);
sourcepub fn insert(&mut self, value: T) -> H
pub fn insert(&mut self, value: T) -> H
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.
sourcepub fn try_insert_with_handle<F: FnOnce(H) -> T>(&mut self, f: F) -> Option<H>
pub fn try_insert_with_handle<F: FnOnce(H) -> T>(&mut self, f: F) -> Option<H>
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));
sourcepub fn insert_with_handle<F: FnOnce(H) -> T>(&mut self, f: F) -> H
pub fn insert_with_handle<F: FnOnce(H) -> T>(&mut self, f: F) -> H
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.
sourcepub fn remove(&mut self, handle: H) -> Option<T>
pub fn remove(&mut self, handle: H) -> Option<T>
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);
sourcepub fn retain<F: FnMut(H, &mut T) -> bool>(&mut self, f: F)
pub fn retain<F: FnMut(H, &mut T) -> bool>(&mut self, f: F)
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);
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
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());
sourcepub fn iter(&self) -> Iter<'_, T, S, H>ⓘNotable traits for Iter<'a, T, S, H>impl<'a, T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Iterator for Iter<'a, T, S, H> type Item = (H, &'a T);
pub fn iter(&self) -> Iter<'_, T, S, H>ⓘNotable traits for Iter<'a, T, S, H>impl<'a, T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Iterator for Iter<'a, T, S, H> type Item = (H, &'a T);
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]);
sourcepub fn iter_mut(&mut self) -> IterMut<'_, T, S, H>ⓘNotable traits for IterMut<'a, T, S, H>impl<'a, T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Iterator for IterMut<'a, T, S, H> type Item = (H, &'a mut T);
pub fn iter_mut(&mut self) -> IterMut<'_, T, S, H>ⓘNotable traits for IterMut<'a, T, S, H>impl<'a, T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Iterator for IterMut<'a, T, S, H> type Item = (H, &'a mut T);
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);
sourcepub fn handles(&self) -> Handles<'_, T, S, H>ⓘNotable traits for Handles<'_, T, S, H>impl<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Iterator for Handles<'_, T, S, H> type Item = H;
pub fn handles(&self) -> Handles<'_, T, S, H>ⓘNotable traits for Handles<'_, T, S, H>impl<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Iterator for Handles<'_, T, S, H> type Item = H;
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]);
sourcepub fn values(&self) -> Values<'_, T, S, H>ⓘNotable traits for Values<'a, T, S, H>impl<'a, T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Iterator for Values<'a, T, S, H> type Item = &'a T;
pub fn values(&self) -> Values<'_, T, S, H>ⓘNotable traits for Values<'a, T, S, H>impl<'a, T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Iterator for Values<'a, T, S, H> type Item = &'a T;
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]);
sourcepub fn values_mut(&mut self) -> ValuesMut<'_, T, S, H>ⓘNotable traits for ValuesMut<'a, T, S, H>impl<'a, T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Iterator for ValuesMut<'a, T, S, H> type Item = &'a mut T;
pub fn values_mut(&mut self) -> ValuesMut<'_, T, S, H>ⓘNotable traits for ValuesMut<'a, T, S, H>impl<'a, T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Iterator for ValuesMut<'a, T, S, H> type Item = &'a mut T;
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);
sourcepub fn drain(&mut self) -> Drain<'_, T, S, H>ⓘNotable traits for Drain<'_, T, S, H>impl<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Iterator for Drain<'_, T, S, H> type Item = (H, T);
pub fn drain(&mut self) -> Drain<'_, T, S, H>ⓘNotable traits for Drain<'_, T, S, H>impl<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Iterator for Drain<'_, T, S, H> type Item = (H, T);
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);
sourcepub fn drain_filter<F: FnMut(H, &mut T) -> bool>(
&mut self,
filter: F
) -> DrainFilter<'_, T, S, H, F>ⓘNotable traits for DrainFilter<'_, T, S, H, F>impl<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle, F: FnMut(H, &mut T) -> bool> Iterator for DrainFilter<'_, T, S, H, F> type Item = (H, T);
pub fn drain_filter<F: FnMut(H, &mut T) -> bool>(
&mut self,
filter: F
) -> DrainFilter<'_, T, S, H, F>ⓘNotable traits for DrainFilter<'_, T, S, H, F>impl<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle, F: FnMut(H, &mut T) -> bool> Iterator for DrainFilter<'_, T, S, H, F> type Item = (H, T);
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);
sourceimpl<T, H: Handle> DirectPool<T, AllocStorage<DirectPoolLayout<T, H>>, H>
impl<T, H: Handle> DirectPool<T, AllocStorage<DirectPoolLayout<T, H>>, H>
sourcepub fn with_capacity(capacity: H::Index) -> Self
This is supported on crate feature alloc
only.
pub fn with_capacity(capacity: H::Index) -> Self
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
.
sourceimpl<T, H: Handle, const N: usize> DirectPool<T, InlineStorage<T, H, N>, H>
impl<T, H: Handle, const N: usize> DirectPool<T, InlineStorage<T, H, N>, H>
sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a new, empty DirectPool
backed by InlineStorage
.
Trait Implementations
sourceimpl<T: Clone, H: Handle, const N: usize> Clone for DirectPool<T, InlineStorage<T, H, N>, H>
impl<T: Clone, H: Handle, const N: usize> Clone for DirectPool<T, InlineStorage<T, H, N>, H>
sourceimpl<T: Debug, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Debug for DirectPool<T, S, H>
impl<T: Debug, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Debug for DirectPool<T, S, H>
sourceimpl<T, H: Handle, const N: usize> Default for DirectPool<T, InlineStorage<T, H, N>, H>
impl<T, H: Handle, const N: usize> Default for DirectPool<T, InlineStorage<T, H, N>, H>
sourceimpl<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Drop for DirectPool<T, S, H>
impl<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Drop for DirectPool<T, S, H>
sourceimpl<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> From<S> for DirectPool<T, S, H>
impl<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> From<S> for DirectPool<T, S, H>
sourceimpl<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Index<H> for DirectPool<T, S, H>
impl<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> Index<H> for DirectPool<T, S, H>
sourceimpl<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> IndexMut<H> for DirectPool<T, S, H>
impl<T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> IndexMut<H> for DirectPool<T, S, H>
sourceimpl<'a, T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> IntoIterator for &'a DirectPool<T, S, H>
impl<'a, T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> IntoIterator for &'a DirectPool<T, S, H>
sourceimpl<'a, T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> IntoIterator for &'a mut DirectPool<T, S, H>
impl<'a, T, S: Storage<DirectPoolLayout<T, H>>, H: Handle> IntoIterator for &'a mut DirectPool<T, S, H>
Auto Trait Implementations
impl<T, S, H> RefUnwindSafe for DirectPool<T, S, H> where
S: RefUnwindSafe,
T: RefUnwindSafe,
<H as Handle>::Index: RefUnwindSafe,
impl<T, S, H> Send for DirectPool<T, S, H> where
S: Send,
T: Send,
<H as Handle>::Index: Send,
impl<T, S, H> Sync for DirectPool<T, S, H> where
S: Sync,
T: Sync,
<H as Handle>::Index: Sync,
impl<T, S, H> Unpin for DirectPool<T, S, H> where
S: Unpin,
T: Unpin,
<H as Handle>::Index: Unpin,
impl<T, S, H> UnwindSafe for DirectPool<T, S, H> where
S: UnwindSafe,
T: UnwindSafe,
<H as Handle>::Index: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more