pub struct ScalarAsVec<T>(pub T);Expand description
Nothing but a wrapped value, a tuple struct, that allows to represent its internal scalar value as an infinite-length vector having the same value at all positions.
§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 all 42s. Probably not 42s, but all zeros/ones vectors/matrices are often very useful. Following would work:
let numbers = vec![42, 42, 42, 42, 42];
assert_eq!(Some(42), third_element(&numbers));However, this would not be the best way to achieve this:
- we allocate a vector just to return 42, and
- we make method calls just to read 42 and lose compiler optimization potential.
We can instead use ScalarAsVec wrapper which implements FunVec<1, _>:
let numbers = ScalarAsVec(42);
assert_eq!(Some(42), third_element(&numbers));Actually, ScalarAsVec implements FunVec for all dimensions:
let numbers = ScalarAsVec(42);
assert_eq!(Some(42), numbers.at(3));
assert_eq!(Some(42), numbers.at([7, 2]));
assert_eq!(Some(42), numbers.at([14, 1, 0]));
assert_eq!(Some(42), numbers.at((4, 1, 3, 6))); // array or tuple indices can be used interchangeablyTuple Fields§
§0: TTrait Implementations§
Source§impl<const DIM: usize, T: Clone + Copy> FunVec<DIM, T> for ScalarAsVec<T>
impl<const DIM: usize, T: Clone + Copy> FunVec<DIM, T> for ScalarAsVec<T>
Auto Trait Implementations§
impl<T> Freeze for ScalarAsVec<T>where
T: Freeze,
impl<T> RefUnwindSafe for ScalarAsVec<T>where
T: RefUnwindSafe,
impl<T> Send for ScalarAsVec<T>where
T: Send,
impl<T> Sync for ScalarAsVec<T>where
T: Sync,
impl<T> Unpin for ScalarAsVec<T>where
T: Unpin,
impl<T> UnwindSafe for ScalarAsVec<T>where
T: UnwindSafe,
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