Struct orx_funvec::ScalarAsVec

source ·
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 interchangeably

Tuple Fields§

§0: T

Trait Implementations§

source§

impl<const DIM: usize, T: Clone + Copy> FunVec<DIM, T> for ScalarAsVec<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> FunVecRef<DIM, T> for ScalarAsVec<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> 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> 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>,

§

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

§

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.