[−][src]Struct slots::unrestricted::UnrestrictedSlots
Slots object that provides an unrestricted 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> UnrestrictedSlots<IT, N> where
N: Size<IT>,
[src]
N: Size<IT>,
pub fn new() -> Self
[src]
Creates a new, empty UnrestrictedSlots 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 iter_mut(&mut self) -> IterMut<IT>
[src]
Returns a read-write iterator. The iterator can be used to read and modify data from all occupied slots, but it can't remove data.
Note: Do not rely on the order in which the elements are returned.
let k = slots.store(2).unwrap(); slots.store(4).unwrap(); slots.store(6).unwrap(); for mut x in slots.iter_mut() { *x *= 2; } assert_eq!(4, slots.take(k).unwrap());
pub fn capacity(&self) -> usize
[src]
Returns the number of slots
let slots: UnrestrictedSlots<f32, U4> = UnrestrictedSlots::new(); assert_eq!(4, slots.capacity());
pub fn count(&self) -> usize
[src]
Returns the number of occupied slots
let mut slots: UnrestrictedSlots<_, U4> = UnrestrictedSlots::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: UnrestrictedSlots<_, U4> = UnrestrictedSlots::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<usize, 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: usize) -> Option<IT>
[src]
Remove and return the element that belongs to the key.
This operation is fallible. If key
addresses a free slot, None
is returned.
pub fn read<T>(&self, key: usize, function: impl FnOnce(&IT) -> T) -> Option<T>
[src]
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 key
addresses a free slot, None
is returned.
let k = slots.store(3).unwrap(); assert_eq!(Some(4), slots.read(k, |elem| { elem + 1 })); slots.take(k); assert_eq!(None, slots.read(k, |elem| { elem + 1 }));
pub fn modify<T>(
&mut self,
key: usize,
function: impl FnOnce(&mut IT) -> T
) -> Option<T>
[src]
&mut self,
key: usize,
function: impl FnOnce(&mut IT) -> T
) -> Option<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.
This operation is fallible. If key
addresses a free slot, None
is returned.
let k = slots.store(3).unwrap(); assert_eq!(Some("found"), slots.modify(k, |elem| { *elem = *elem + 1; "found" })); // Assert that the stored data was modified assert_eq!(Some(4), slots.take(k)); assert_eq!(None, slots.modify(k, |elem| { *elem = *elem + 1; "found" }));
Trait Implementations
impl<IT: Default, N: Default> Default for UnrestrictedSlots<IT, N> where
N: Size<IT>,
[src]
N: Size<IT>,
fn default() -> UnrestrictedSlots<IT, N>
[src]
Auto Trait Implementations
impl<IT, N> Send for UnrestrictedSlots<IT, N> where
IT: Send,
IT: Send,
impl<IT, N> Sync for UnrestrictedSlots<IT, N> where
IT: Sync,
IT: Sync,
impl<IT, N> Unpin for UnrestrictedSlots<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>,