pub struct Block16<T> { /* private fields */ }Expand description
A fixed block of optionals masked by a u16,
which may thus contain at most 16 elements.
Implementations§
Source§impl<T> Block16<T>
impl<T> Block16<T>
Sourcepub const fn is_vacant(&self, index: usize) -> bool
pub const fn is_vacant(&self, index: usize) -> bool
Checks whether the item at the index is vacant (i.e. contains None).
§Panic
Panics if index >= CAPACITY. See the maximum capacity.
Sourcepub const unsafe fn get_unchecked(&self, index: usize) -> &T
pub const unsafe fn get_unchecked(&self, index: usize) -> &T
Sourcepub const fn get(&self, index: usize) -> Option<&T>
pub const fn get(&self, index: usize) -> Option<&T>
Attempts to retrieve a shared reference to the element at index.
Returns None if the slot is vacant (i.e. uninitialized).
§Panic
Panics if index >= CAPACITY. See the maximum capacity.
Sourcepub const unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
pub const unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
Sourcepub const fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub const fn get_mut(&mut self, index: usize) -> Option<&mut T>
Attempts to retrieve an exclusive reference to the element at
index. Returns None if the slot is vacant (i.e. uninitialized).
§Panic
Panics if index >= CAPACITY. See the maximum capacity.
Sourcepub fn get_or_else(&mut self, index: usize, func: impl FnOnce() -> T) -> &mut T
pub fn get_or_else(&mut self, index: usize, func: impl FnOnce() -> T) -> &mut T
If the slot at the given index is already occupied, this method returns a mutable
reference to the inner data. Otherwise, if the slot is vacant, then this method
inserts the value constructed by func. A mutable reference to the inner data is
nevertheless returned.
Sourcepub fn get_or(&mut self, index: usize, val: T) -> &mut T
pub fn get_or(&mut self, index: usize, val: T) -> &mut T
Convenience wrapper for the get_or_else method.
Sourcepub const fn lowest_occupied_index(&self) -> Option<u32>
pub const fn lowest_occupied_index(&self) -> Option<u32>
Returns the index of the first (i.e., lowest index) non-vacant element in the block.
Note that a u32 is returned for maximum flexibility, but its value will never
exceed Self::CAPACITY. It should be safe to cast to a usize without loss of
information. You may also safely unwrap the conversion via the TryFrom trait.
Sourcepub const fn first_occupied(&self) -> Option<&T>
pub const fn first_occupied(&self) -> Option<&T>
Returns a shared reference to the first non-vacant element in the block.
Convenience wrapper around Self::lowest_occupied_index followed by Self::get_unchecked.
Sourcepub const fn first_occupied_mut(&mut self) -> Option<&mut T>
pub const fn first_occupied_mut(&mut self) -> Option<&mut T>
Returns an exclusive reference to the first non-vacant element in the block.
Convenience wrapper around Self::lowest_occupied_index followed by Self::get_unchecked_mut.
Sourcepub const fn highest_occupied_index(&self) -> Option<u32>
pub const fn highest_occupied_index(&self) -> Option<u32>
Returns the index of the last (i.e., highest index) non-vacant element in the block.
Note that a u32 is returned for maximum flexibility, but its value will never
exceed Self::CAPACITY. It should be safe to cast to a usize without loss of
information. You may also safely unwrap the conversion via the TryFrom trait.
Sourcepub const fn last_occupied(&self) -> Option<&T>
pub const fn last_occupied(&self) -> Option<&T>
Returns a shared reference to the last non-vacant element in the block.
Convenience wrapper around Self::highest_occupied_index followed by Self::get_unchecked.
Sourcepub const fn last_occupied_mut(&mut self) -> Option<&mut T>
pub const fn last_occupied_mut(&mut self) -> Option<&mut T>
Returns an exclusive reference to the last non-vacant element in the block.
Convenience wrapper around Self::highest_occupied_index followed by Self::get_unchecked_mut.
Sourcepub const fn lowest_vacant_index(&self) -> Option<u32>
pub const fn lowest_vacant_index(&self) -> Option<u32>
Returns the index of the first (i.e., lowest index) vacant element in the block.
Note that a u32 is returned for maximum flexibility, but its value will never
exceed Self::CAPACITY. It should be safe to cast to a usize without loss of
information. You may also safely unwrap the conversion via the TryFrom trait.
Sourcepub const fn insert_at_first_vacancy(
&mut self,
value: T,
) -> Result<Option<T>, T>
pub const fn insert_at_first_vacancy( &mut self, value: T, ) -> Result<Option<T>, T>
Attempts to insert value at the first vacant slot in the block.
Convenience wrapper around Self::lowest_vacant_index followed by Self::insert.
§Return Value
Ok(option)if a vacant slot was found, whereoptionis the return value fromSelf::insert.Err(value)if the block is full, returning the originalvalueback to the caller.
Sourcepub const fn highest_vacant_index(&self) -> Option<u32>
pub const fn highest_vacant_index(&self) -> Option<u32>
Returns the index of the last (i.e., highest index) vacant element in the block.
Note that a u32 is returned for maximum flexibility, but its value will never
exceed Self::CAPACITY. It should be safe to cast to a usize without loss of
information. You may also safely unwrap the conversion via the TryFrom trait.
Sourcepub const fn insert_at_last_vacancy(&mut self, value: T) -> Result<Option<T>, T>
pub const fn insert_at_last_vacancy(&mut self, value: T) -> Result<Option<T>, T>
Attempts to insert value at the last vacant slot in the block.
Convenience wrapper around Self::highest_vacant_index followed by Self::insert.
§Return Value
Ok(option)if a vacant slot was found, whereoptionis the return value fromSelf::insert.Err(value)if the block is full, returning the originalvalueback to the caller.
Sourcepub const fn insert(&mut self, index: usize, value: T) -> Option<T>
pub const fn insert(&mut self, index: usize, value: T) -> Option<T>
Inserts the value at the index. If a value already exists, it returns Some
containing the old value. Otherwise, it returns None.
§Panic
Panics if index >= CAPACITY. See the maximum capacity.
Sourcepub const fn remove(&mut self, index: usize) -> Option<T>
pub const fn remove(&mut self, index: usize) -> Option<T>
Removes the value at the index. If a value already exists, it returns Some
containing that value. Otherwise, it returns None.
§Panic
Panics if index >= CAPACITY. See the maximum capacity.
Sourcepub fn iter(&self) -> Block16Iter<'_, T> ⓘ
pub fn iter(&self) -> Block16Iter<'_, T> ⓘ
Create a by-reference iterator for this block.
Sourcepub fn iter_mut(&mut self) -> Block16IterMut<'_, T> ⓘ
pub fn iter_mut(&mut self) -> Block16IterMut<'_, T> ⓘ
Create a mutable by-reference iterator for this block.
Source§impl<T: Default> Block16<T>
impl<T: Default> Block16<T>
Sourcepub fn get_or_default(&mut self, index: usize) -> &mut T
pub fn get_or_default(&mut self, index: usize) -> &mut T
Convenience wrapper for the get_or_else method.
Trait Implementations§
Source§impl<T> Drop for Block16<T>
Ensure that all remaining items in the block are dropped. Since the implementation
internally uses MaybeUninit, we must manually drop the valid
(i.e., initialized) contents ourselves.
impl<T> Drop for Block16<T>
Ensure that all remaining items in the block are dropped. Since the implementation
internally uses MaybeUninit, we must manually drop the valid
(i.e., initialized) contents ourselves.