Struct EmptyVec

Source
pub struct EmptyVec<T: ?Sized>(/* private fields */);
Expand description

A zero-sized empty vector which returns None for all indices.

§Examples

Say, for instance, we have a function requiring a vector, FunVec<1, i32>.

use orx_funvec::*;
fn third_element<V: FunVec<1, i32>>(vec: &V) -> Option<i32> {
    vec.at(2)
}

We might often call this function with a Vec<i32>.

let numbers = vec![1, 2, 3, 4, 5, 6];
assert_eq!(Some(3), third_element(&numbers));

There might however be special cases where our input vector is empty. Following could still work:

let numbers = vec![];
assert_eq!(None, third_element(&numbers));

However, this would not be the best way to achieve this:

  • since the third_element is already generic, we can take complete benefit of monomorphization and inlining by using a specific type that does nothing but returns None for all indices.

We can instead use EmptyVec for this purpose which implements FunVec<1, _>.

let numbers = EmptyVec::default();
assert_eq!(None, third_element(&numbers));

Actually, EmptyVec implements FunVec for all dimensions::


let numbers: EmptyVec<&str> = EmptyVec::default();

assert_eq!(None, numbers.at(3));
assert_eq!(None, numbers.at([7, 2]));
assert_eq!(None, numbers.at([14, 1, 0]));
assert_eq!(None, numbers.at((4, 1, 3, 6))); // array or tuple indices can be used interchangeably

Implementations§

Source§

impl<T: ?Sized> EmptyVec<T>

Source

pub fn new() -> Self

Constructs a new EmptyVec.

Trait Implementations§

Source§

impl<T: Default + ?Sized> Default for EmptyVec<T>

Source§

fn default() -> EmptyVec<T>

Returns the “default value” for a type. Read more
Source§

impl<const DIM: usize, T: Clone + Copy> FunVec<DIM, T> for EmptyVec<T>

Source§

fn at<Idx: IntoIndex<DIM>>(&self, _: Idx) -> Option<T>

Returns the value at the given index or None if the position is empty. Read more
Source§

fn iter_over<'a, Idx, IdxIter>( &self, indices: IdxIter, ) -> IterOverValues<'_, DIM, T, Idx, IdxIter, Self>
where Idx: IntoIndex<DIM>, IdxIter: Iterator<Item = Idx> + 'a,

Returns an iterator of elements of the vector for the given indices. Read more
Source§

impl<const DIM: usize, T: ?Sized> FunVecRef<DIM, T> for EmptyVec<T>

Source§

fn ref_at<Idx: IntoIndex<DIM>>(&self, _: Idx) -> Option<&T>

Returns a reference to the element at the given index or None if the position is empty. Read more
Source§

fn ref_iter_over<'a, Idx, IdxIter>( &self, indices: IdxIter, ) -> IterOverRefs<'_, DIM, T, Idx, IdxIter, Self>
where Idx: IntoIndex<DIM>, IdxIter: Iterator<Item = Idx> + 'a,

Returns an iterator yielding references to elements of the vector for the given indices. Read more

Auto Trait Implementations§

§

impl<T> Freeze for EmptyVec<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for EmptyVec<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> Send for EmptyVec<T>
where T: Send + ?Sized,

§

impl<T> Sync for EmptyVec<T>
where T: Sync + ?Sized,

§

impl<T> Unpin for EmptyVec<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for EmptyVec<T>
where T: UnwindSafe + ?Sized,

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