Block16

Struct Block16 

Source
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>

Source

pub const CAPACITY: u32 = 16u32

Maximum capacity of the fixed-size block.

Source

pub const fn new() -> Self

Creates a new empty block. Useful in const contexts.

Source

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.

Source

pub const fn len(&self) -> u32

Returns the number of non-null elements in the block.

Source

pub const fn is_empty(&self) -> bool

Returns true if the block contains zero elements.

Source

pub const unsafe fn get_unchecked(&self, index: usize) -> &T

Returns an immutable reference to the value at index. See the get method for the safe, checked version of this method.

§Safety

The queried value must be properly initialized. Otherwise, the behavior is undefined.

Source

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.

Source

pub const unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T

Returns a mutable reference to the value at index. See the get_mut method for the safe, checked version of this method.

§Safety

The queried value must be properly initialized. Otherwise, the behavior is undefined.

Source

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.

Source

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.

Source

pub fn get_or(&mut self, index: usize, val: T) -> &mut T

Convenience wrapper for the get_or_else method.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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, where option is the return value from Self::insert.
  • Err(value) if the block is full, returning the original value back to the caller.
Source

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.

Source

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, where option is the return value from Self::insert.
  • Err(value) if the block is full, returning the original value back to the caller.
Source

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.

Source

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.

Source

pub fn iter(&self) -> Block16Iter<'_, T>

Create a by-reference iterator for this block.

Source

pub fn iter_mut(&mut self) -> Block16IterMut<'_, T>

Create a mutable by-reference iterator for this block.

Source§

impl<T: Default> Block16<T>

Source

pub fn get_or_default(&mut self, index: usize) -> &mut T

Convenience wrapper for the get_or_else method.

Trait Implementations§

Source§

impl<T: Clone> Clone for Block16<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Block16<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Block16<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
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.

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> From<[T; 16]> for Block16<T>

Create a fully initialized direct-access table.

Source§

fn from(vals: [T; 16]) -> Self

Converts to this type from the input type.
Source§

impl<T> FromIterator<(usize, T)> for Block16<T>

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = (usize, T)>,

Creates a value from an iterator. Read more
Source§

impl<T> Index<usize> for Block16<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, idx: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<usize> for Block16<T>

Source§

fn index_mut(&mut self, idx: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, T> IntoIterator for &'a Block16<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Block16Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut Block16<T>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = Block16IterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for Block16<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = Block16IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Block16<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Block16<T>
where T: RefUnwindSafe,

§

impl<T> Send for Block16<T>
where T: Send,

§

impl<T> Sync for Block16<T>
where T: Sync,

§

impl<T> Unpin for Block16<T>
where T: Unpin,

§

impl<T> UnwindSafe for Block16<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.