Struct OptionCore

Source
pub struct OptionCore<T> { /* private fields */ }
Expand description

A Core implementation that is essentially a Vec<Option<T>>.

This implementation is quite different from the BitVecCore. This one only manages one allocation, meaning it does not suffer from the same disadvantages as BitVecCore. This can sometimes lead to better memory access times due to caching effects. However, this implementation has the major disadvantage of wasting memory in most cases.

Usually, size_of::<Option<T>>() > size_of::<T>(). The difference can be as high as 4 byte due to alignment. But as only one bit is used to store the None/Some information, all that memory is wasted. A worst case scenario is something like Option<u32>: this is 8 byte large (on most platforms), meaning that a stable vector with OptionCore would use twice as much memory as a Vec<u32>. This is not only wasteful, but has a negative effect on speed as the information is not very densely packed.

In general, only use this implementation in one of these cases:

  • Your T is NonNull, meaning that Option<T> has the same size as T. This is then even more memory efficient than the default core implementation. Iterating over indices of a stable vector is still slower in this case as the relevant information is further apart.
  • Your T is very large, meaning that the amount of wasted memory is small in comparison.

In both cases, switching the implementation from default to this only makes sense after you measured that you actually gain performance from it. The interface of both implementations is exactly the same.

Trait Implementations§

Source§

impl<T: Clone> Clone for OptionCore<T>

Source§

fn clone(&self) -> Self

Returns a copy 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> Core<T> for OptionCore<T>

Source§

unsafe fn has_element_at(&self, idx: usize) -> bool

Assumes that idx < capacity

Source§

fn new() -> Self

Creates an empty instance without any elements. Must not allocate memory. Read more
Source§

fn len(&self) -> usize

Returns the length of this core (the len). See the trait docs for more information.
Source§

fn cap(&self) -> usize

Returns the capacity of this core (the cap). See the trait docs for more information.
Source§

unsafe fn set_len(&mut self, new_len: usize)

Sets the len to a new value. Read more
Source§

unsafe fn realloc(&mut self, new_cap: usize)

Reallocates the memory to have a cap of at least new_cap. This method should try its best to allocate exactly new_cap. Read more
Source§

unsafe fn insert_at(&mut self, idx: usize, elem: T)

Inserts elem at the index idx. Does not updated the used_len. Read more
Source§

unsafe fn remove_at(&mut self, idx: usize) -> T

Removes the element at index idx and returns it. Read more
Source§

unsafe fn get_unchecked(&self, idx: usize) -> &T

Returns a reference to the element at the index idx. Read more
Source§

unsafe fn get_unchecked_mut(&mut self, idx: usize) -> &mut T

Returns a mutable reference to the element at the index idx. Read more
Source§

fn clear(&mut self)

Deletes all elements without deallocating memory. Drops all existing elements. Sets len to 0. Read more
Source§

unsafe fn swap(&mut self, a: usize, b: usize)

Swaps the two slots with indices a and b. That is: the element and the “filled/empty” status are swapped. The slots at indices a and b can be empty or filled. Read more
Source§

unsafe fn first_filled_slot_from(&self, idx: usize) -> Option<usize>

Performs a forwards search starting at index idx, returning the index of the first filled slot that is found. Read more
Source§

unsafe fn first_filled_slot_below(&self, idx: usize) -> Option<usize>

Performs a backwards search starting at index idx - 1, returning the index of the first filled slot that is found. Read more
Source§

unsafe fn first_empty_slot_from(&self, idx: usize) -> Option<usize>

Performs a forwards search starting at index idx, returning the index of the first empty slot that is found. Read more
Source§

unsafe fn first_empty_slot_below(&self, idx: usize) -> Option<usize>

Performs a backwards search starting at index idx - 1, returning the index of the first empty slot that is found. Read more
Source§

impl<T: Debug> Debug for OptionCore<T>

Source§

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

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

impl<T> Drop for OptionCore<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for OptionCore<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for OptionCore<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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.