[−][src]Struct slots::slots::Slots
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, which is a type-level constant provided by thetypenum
crate.
For more information, see the module level documentation
Implementations
impl<IT, N> Slots<IT, N> where
N: Size<IT>,
[src]
N: Size<IT>,
pub fn new() -> Self
[src]
Creates a new, empty Slots object.
pub fn iter(&self) -> Iter<IT>
[src]
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));
pub fn capacity(&self) -> usize
[src]
Returns the number of slots
let slots: Slots<f32, U4> = Slots::new(); assert_eq!(4, slots.capacity());
pub fn count(&self) -> usize
[src]
Returns the number of occupied slots
let mut slots: Slots<_, U4> = Slots::new(); assert_eq!(0, slots.count()); slots.store(3).unwrap(); slots.store(6).unwrap(); assert_eq!(2, slots.count());
pub fn is_full(&self) -> bool
[src]
Returns whether all the slots are occupied and the next store()
will fail.
let mut slots: Slots<_, U4> = 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());
pub fn store(&mut self, item: IT) -> Result<Key<IT, N>, IT>
[src]
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.
pub fn take(&mut self, key: Key<IT, N>) -> IT
[src]
Remove and return the element that belongs to the key.
pub fn read<T>(&self, key: &Key<IT, N>, function: impl FnOnce(&IT) -> T) -> T
[src]
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 }));
pub fn try_read<T>(
&self,
index: usize,
function: impl FnOnce(&IT) -> T
) -> Option<T>
[src]
&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 }));
pub fn modify<T>(
&mut self,
key: &Key<IT, N>,
function: impl FnOnce(&mut IT) -> T
) -> T
[src]
&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, N> Send for Slots<IT, N> where
IT: Send,
IT: Send,
impl<IT, N> Sync for Slots<IT, N> where
IT: Sync,
IT: Sync,
impl<IT, N> Unpin for Slots<IT, N> where
<N as ArrayLength<Entry<IT>>>::ArrayType: Unpin,
<N as ArrayLength<Entry<IT>>>::ArrayType: Unpin,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,