Struct Vector

Source
pub struct Vector<T, const N: usize> { /* private fields */ }
Expand description

A compile-time n-dimensional vector, how fancy!

Implementations§

Source§

impl<T, const N: usize> Vector<T, N>

Source

pub fn new(components: [T; N]) -> Self

Create a new vector with the given components.

Vector::new([1, 2, 3]);
Source

pub fn zero() -> Self
where T: Num + Copy,

Create a new vector with zeroed components.

Source

pub fn as_slice(&self) -> &[T]

Returns the components of the vector as a slice.

Source§

impl<T: Copy, const N: usize> Vector<T, N>

Source

pub fn num_cast<K: Num + Copy + NumCast>(&self) -> Option<Vector<K, N>>
where T: ToPrimitive,

Allows numerically casting each component of the vector. Makes use of the num_traits::NumCast trait. If the cast fails, None is returned.

let a = vector!(1.0, 2.0, 3.0);
let b = vector!(1, 2, 3);

assert_eq!(a.num_cast::<i32>().unwrap(), b);
Source

pub fn try_cast<K: Num + Copy + TryFrom<T>>( &self, ) -> Result<Vector<K, N>, <K as TryFrom<T>>::Error>

Allows casting each component of the vector using the TryFrom trait. If the cast fails, an error is returned.

In many cases Vector::num_cast is more versatile as it supports more conversions. For example u32 to f32 is supported by Vector::num_cast but not by Vector::try_cast.

let a: Vector<u32, 3> = vector!(1, 2, 3);
let b: Vector<u8, 3> = vector!(1, 2, 3);

assert_eq!(a.try_cast().unwrap(), b);
Source

pub fn cast<K: Num + Copy + From<T>>(&self) -> Vector<K, N>

Casts each component of the vector to the given type.

This is similar to Vector::try_cast but only works if the cast is infallible. Because of this, it should be preferred over Vector::try_cast when casting from smaller to larger types.

Source§

impl<T: Num + Copy, const N: usize> Vector<T, N>

Source

pub fn hadamard_product(&self, other: &Self) -> Self

Computes the Hadamard product of two vectors (component-wise multiplication).

Source§

impl<T: Num + Copy + Ord, const N: usize> Vector<T, N>

Source

pub fn min(&self, other: &Self) -> Self

Takes the minimum of each component of two vectors.

Source

pub fn max(&self, other: &Self) -> Self

Takes the maximum of each component of two vectors.

Source

pub fn min_component(&self) -> T

Takes the minimum component of a vector.

Source

pub fn max_component(&self) -> T

Takes the maximum component of a vector.

Source§

impl<T: Num + Copy + Signed, const N: usize> Vector<T, N>

Source

pub fn opposite(&self) -> Self

Calculates the opposite of a vector. This is the vector with all components negated.

Source

pub fn signum(&self) -> Self

Calculates the sign of each component of a vector. This is -1 if the component is negative, 0 if it is zero, and 1 if it is positive.

Source

pub fn manhattan_distance(&self, other: &Self) -> T

Calculates the Manhattan Distance of two vectors.

Source§

impl<T: Num + Copy + Sum, const N: usize> Vector<T, N>

Source

pub fn sum(&self) -> T

Source

pub fn magnitude_squared(&self) -> T

Calculates the sum of all squared components. Used for calculating the magnitude of a vector.

Source

pub fn dot(&self, other: &Self) -> T

Calculates the dot product of two vectors.

Source§

impl<T: Num + Copy + Sum + Real, const N: usize> Vector<T, N>

Source

pub fn magnitude(&self) -> T

Calculates the magnitude of a vector. This is the square root of the sum of all squared components.

Source

pub fn normalize(&self) -> Self

Normalizes a vector. This is the vector divided by its magnitude.

Source

pub fn distance(&self, other: &Self) -> T

Calculates the Euclidean Distance of two vectors.

Source§

impl<T: Num + Signed + Copy, const N: usize> Vector<T, N>

Source

pub fn abs(&self) -> Self

Calculates the absolute value of each component of a vector.

Source§

impl<T: Copy> Vector<T, 2>

Source

pub fn x(&self) -> T

Source

pub fn y(&self) -> T

Source§

impl<T: Copy> Vector<T, 3>

Source

pub fn x(&self) -> T

Source

pub fn y(&self) -> T

Source

pub fn z(&self) -> T

Trait Implementations§

Source§

impl<T: Num + Copy, const N: usize> Add<T> for Vector<T, N>

Source§

type Output = Vector<T, N>

The resulting type after applying the + operator.
Source§

fn add(self, other: T) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: Num + Copy, const N: usize> Add for Vector<T, N>

Source§

type Output = Vector<T, N>

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: Num + Copy, const N: usize> AddAssign for Vector<T, N>

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<T: Clone, const N: usize> Clone for Vector<T, N>

Source§

fn clone(&self) -> Vector<T, N>

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: Num + Copy + Display, const N: usize> Debug for Vector<T, N>

Source§

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

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

impl<T: Default + Copy, const N: usize> Default for Vector<T, N>

Source§

fn default() -> Self

Create a new vector with zeroed components.

Source§

impl<T: Num + Copy, const N: usize> Div<T> for Vector<T, N>

Source§

type Output = Vector<T, N>

The resulting type after applying the / operator.
Source§

fn div(self, other: T) -> Self::Output

Performs the / operation. Read more
Source§

impl<T: Num + Copy, const N: usize> Div for Vector<T, N>

Source§

type Output = Vector<T, N>

The resulting type after applying the / operator.
Source§

fn div(self, other: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl<T: Num + Copy, const N: usize> DivAssign for Vector<T, N>

Source§

fn div_assign(&mut self, rhs: Self)

Performs the /= operation. Read more
Source§

impl<T: Num + Copy, const N: usize> FromIterator<T> for Vector<T, N>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Create a new vector from an iterator. If the iterator has less than N items, the remaining components will be zeroed. If the iterator has more than N items, the remaining items will be ignored.

Source§

impl<T: Hash, const N: usize> Hash for Vector<T, N>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Num + Copy + Send + Sync, const N: usize> Mul<T> for Vector<T, N>

Source§

type Output = Vector<T, N>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: T) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: Num + Copy, const N: usize> Neg for Vector<T, N>

Source§

fn neg(self) -> Self::Output

Negates all components of a vector.

Source§

type Output = Vector<T, N>

The resulting type after applying the - operator.
Source§

impl<T: Num + Copy, const N: usize> PartialEq for Vector<T, N>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Num + Copy, const N: usize> Rem<T> for Vector<T, N>

Source§

type Output = Vector<T, N>

The resulting type after applying the % operator.
Source§

fn rem(self, other: T) -> Self::Output

Performs the % operation. Read more
Source§

impl<T: Num + Copy, const N: usize> Rem for Vector<T, N>

Source§

type Output = Vector<T, N>

The resulting type after applying the % operator.
Source§

fn rem(self, other: Self) -> Self::Output

Performs the % operation. Read more
Source§

impl<T: Num + Copy, const N: usize> RemAssign for Vector<T, N>

Source§

fn rem_assign(&mut self, rhs: Self)

Performs the %= operation. Read more
Source§

impl<T: Num + Copy, const N: usize> Sub<T> for Vector<T, N>

Source§

type Output = Vector<T, N>

The resulting type after applying the - operator.
Source§

fn sub(self, other: T) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Num + Copy, const N: usize> Sub for Vector<T, N>

Source§

type Output = Vector<T, N>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Num + Copy, const N: usize> SubAssign for Vector<T, N>

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<T: Num + Copy, const N: usize> Copy for Vector<T, N>

Source§

impl<T: Num + Copy, const N: usize> Eq for Vector<T, N>

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for Vector<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for Vector<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for Vector<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for Vector<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for Vector<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnwindSafe for Vector<T, N>
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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.
Source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for T
where T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,