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 returnsNone
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§
Trait Implementations§
Source§impl<const DIM: usize, T: Clone + Copy> FunVec<DIM, T> for EmptyVec<T>
impl<const DIM: usize, T: Clone + Copy> FunVec<DIM, T> for EmptyVec<T>
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>
impl<T> Sync for EmptyVec<T>
impl<T> Unpin for EmptyVec<T>
impl<T> UnwindSafe for EmptyVec<T>where
T: UnwindSafe + ?Sized,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more