pub struct Slots<IT, const N: usize> { /* private fields */ }
Expand description
Slots object that provides strict access control for the stored data.
The struct has two type parameters:
IT
is the type of the stored dataN
is the number of slots.
For more information, see the module level documentation
Implementations
sourceimpl<IT, const N: usize> Slots<IT, N>
impl<IT, const N: usize> Slots<IT, N>
sourcepub fn iter(&self) -> Iter<'_, IT>ⓘNotable traits for Iter<'a, IT>impl<'a, IT> Iterator for Iter<'a, IT> type Item = &'a IT;
pub fn iter(&self) -> Iter<'_, IT>ⓘNotable traits for Iter<'a, IT>impl<'a, IT> Iterator for Iter<'a, IT> type Item = &'a IT;
Returns a read-only iterator. The iterator can be used to read data from all occupied slots.
Note: Do not rely on the order in which the elements are returned.
slots.store(2).unwrap();
slots.store(4).unwrap();
slots.store(6).unwrap();
assert_eq!(true, slots.iter().any(|&x| x < 3));
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of slots
let slots: Slots<f32, 4> = Slots::new();
assert_eq!(4, slots.capacity());
sourcepub fn count(&self) -> usize
pub fn count(&self) -> usize
Returns the number of occupied slots
let mut slots: Slots<_, 4> = Slots::new();
assert_eq!(0, slots.count());
slots.store(3).unwrap();
slots.store(6).unwrap();
assert_eq!(2, slots.count());
sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Returns whether all the slots are occupied and the next store()
will fail.
let mut slots: Slots<_, 4> = Slots::new();
slots.store(3).unwrap();
slots.store(4).unwrap();
slots.store(5).unwrap();
assert_eq!(false, slots.is_full());
slots.store(6).unwrap();
assert_eq!(true, slots.is_full());
sourcepub fn store(&mut self, item: IT) -> Result<Key<IT, N>, IT>
pub fn store(&mut self, item: IT) -> Result<Key<IT, N>, IT>
Store an element in a free slot and return the key to access it.
Storing a variable takes ownership over it. If the storage is full, the inserted data is returned in the return value.
sourcepub fn take(&mut self, key: Key<IT, N>) -> IT
pub fn take(&mut self, key: Key<IT, N>) -> IT
Remove and return the element that belongs to the key.
sourcepub fn read<T>(&self, key: &Key<IT, N>, function: impl FnOnce(&IT) -> T) -> T
pub fn read<T>(&self, key: &Key<IT, N>, function: impl FnOnce(&IT) -> T) -> T
Read the element that belongs to the key.
This operation does not move ownership so the function
callback must be used
to access the stored element. The callback may return arbitrary derivative of the element.
let k = slots.store(3).unwrap();
assert_eq!(4, slots.read(&k, |elem| {
elem + 1
}));
sourcepub fn try_read<T>(
&self,
index: usize,
function: impl FnOnce(&IT) -> T
) -> Option<T>
pub fn try_read<T>(
&self,
index: usize,
function: impl FnOnce(&IT) -> T
) -> Option<T>
Read the element that belongs to a particular index. Since the index may point to a free slot or outside the collection, this operation may return None without invoking the callback.
This operation does not move ownership so the function
callback must be used
to access the stored element. The callback may return arbitrary derivative of the element.
This operation is fallible. If index
addresses a free slot, None
is returned.
let k = slots.store(3).unwrap();
let idx = k.index();
assert_eq!(Some(4), slots.try_read(idx, |elem| {
elem + 1
}));
slots.take(k);
assert_eq!(None, slots.try_read(idx, |elem| {
elem + 1
}));
sourcepub fn modify<T>(
&mut self,
key: &Key<IT, N>,
function: impl FnOnce(&mut IT) -> T
) -> T
pub fn modify<T>(
&mut self,
key: &Key<IT, N>,
function: impl FnOnce(&mut IT) -> T
) -> T
Access the element that belongs to the key for modification.
This operation does not move ownership so the function
callback must be used
to access the stored element. The callback may return arbitrary derivative of the element.
let k = slots.store(3).unwrap();
assert_eq!("found", slots.modify(&k, |elem| {
*elem = *elem + 1;
"found"
}));
// Assert that the stored data was modified
assert_eq!(4, slots.take(k));
Trait Implementations
Auto Trait Implementations
impl<IT, const N: usize> RefUnwindSafe for Slots<IT, N> where
IT: RefUnwindSafe,
impl<IT, const N: usize> Send for Slots<IT, N> where
IT: Send,
impl<IT, const N: usize> Sync for Slots<IT, N> where
IT: Sync,
impl<IT, const N: usize> Unpin for Slots<IT, N> where
IT: Unpin,
impl<IT, const N: usize> UnwindSafe for Slots<IT, N> where
IT: 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