Struct vec_x::VecX

source ·
pub struct VecX<T, const N: usize>
where T: Sized + Send,
{ pub data: [T; N], }
Expand description

A structure representing a fixed-length array of arbitrary elements and arbitrary length. Since it was created primarily to represent mathematical vectors and colors, it supports four arithmetic operations.

任意の要素、任意の長さの固定長配列を表す構造体です。 主に数学的なベクトルや色を表すために作成したため、四則演算をサポートしています。

use vec_x::{VecX};

let vec1 = VecX::new([1, 2, 3]);
let vec2 = VecX::new([4, 5, 6]);

// Add
assert_eq!(vec1 + vec2, VecX::new([5, 7, 9]));
// Sub
assert_eq!(vec1 - vec2, VecX::new([-3, -3, -3]));
// Mul
assert_eq!(vec1 * vec2, VecX::new([4, 10, 18]));
// Div
assert_eq!(vec1 / vec2, VecX::new([0, 0, 0]));
// Rem
assert_eq!(vec1 % vec2, VecX::new([1, 2, 3]));

// AddAssign
let mut vec = VecX::new([1, 2, 3]);
vec += VecX::new([4, 5, 6]);
assert_eq!(vec, VecX::new([5, 7, 9]));
// SubAssign
let mut vec = VecX::new([1, 2, 3]);
vec -= VecX::new([4, 5, 6]);
assert_eq!(vec, VecX::new([-3, -3, -3]));
// MulAssign
let mut vec = VecX::new([1, 2, 3]);
vec *= VecX::new([4, 5, 6]);
assert_eq!(vec, VecX::new([4, 10, 18]));
// DivAssign
let mut vec = VecX::new([1, 2, 3]);
vec /= VecX::new([4, 5, 6]);
assert_eq!(vec, VecX::new([0, 0, 0]));
// RemAssign
let mut vec = VecX::new([1, 2, 3]);
vec %= VecX::new([4, 5, 6]);
assert_eq!(vec, VecX::new([1, 2, 3]));

 You can also perform arithmetic operations with numeric values.

数値型の値との演算も可能です。

use vec_x::{VecX};

let vec = VecX::new([1, 2, 3]);

// Add
assert_eq!(vec + 1, VecX::new([2, 3, 4]));

// Sub
assert_eq!(vec - 1, VecX::new([0, 1, 2]));

// Mul
assert_eq!(vec * 2, VecX::new([2, 4, 6]));

// Div
assert_eq!(vec / 2, VecX::new([0, 1, 1]));

// Rem
assert_eq!(vec % 2, VecX::new([1, 0, 1]));

// AddAssign
let mut vec = VecX::new([1, 2, 3]);
vec += 1;
assert_eq!(vec, VecX::new([2, 3, 4]));

// SubAssign
let mut vec = VecX::new([1, 2, 3]);
vec -= 1;
assert_eq!(vec, VecX::new([0, 1, 2]));

// MulAssign
let mut vec = VecX::new([1, 2, 3]);
vec *= 2;
assert_eq!(vec, VecX::new([2, 4, 6]));

// DivAssign
let mut vec = VecX::new([1, 2, 3]);
vec /= 2;
assert_eq!(vec, VecX::new([0, 1, 1]));

// RemAssign
let mut vec = VecX::new([1, 2, 3]);
vec %= 2;
assert_eq!(vec, VecX::new([1, 0, 1]));

In operations, arrays that implement From/Into traits are implicitly converted to the left-hand side type.

演算において、From/Intoトレイトが実装されている配列同士は暗黙的に左辺の型に変換されます。

use vec_x::{VecX};
use std::ops::Add;

let vec1:VecX<f32,3> = VecX::new([1., 2., 3.]);
let vec2:VecX<u8,3> = VecX::new([4, 5, 6]);

let vec3 = vec1 + vec2;

Arrays that do not implement From/Into trait will fail to compile together. Thus, there is no loss of precision due to implicit type conversion.

From/Intoトレイトが実装されていない配列同士はコンパイルエラーになります。 よって、暗黙的な型変換によって精度が失われることはありません。

use vec_x::{VecX};
use std::ops::Add;

let vec1:VecX<f32,3> = VecX::new([1., 2., 3.]);
let vec2:VecX<u8,3> = VecX::new([4, 5, 6]);

let vec3 = vec2 + vec1; // compile error! Cannot add `VecX<f32, 3>` to `VecX<u8, 3>`[E0369]

Element casts are also supported.

要素のキャストにも対応しています。

use vec_x::{VecX};

let vec = VecX::new([1, 2, 3]);

let vec_f64:VecX<f64,3> = vec.as_();

Non-numeric elements can also be array elements.

数値以外を配列要素にすることもできます。

use vec_x::{VecX};

let vec1 = VecX::new(["a", "b", "c"]);
use vec_x::{VecX};


let vec1 = VecX::new(["a", "b", "c"]);
let vec2 = VecX::new(["d", "e", "f"]);

vec1 + vec2; // compile error!

Using type aliases, as shown below, improves code readability.

以下のように型エイリアスを使用することで、コードの可読性が向上します。

use vec_x::{VecX};

type XYZ = VecX<f64, 3>;
type RGBA = VecX<u8, 4>;

struct Point {
   position: XYZ,
   color: RGBA,
}

let point = Point {
   position: XYZ::new([1.0, 2.0, 3.0]),
   color: RGBA::new([255, 0, 0, 255]),
};

Fields§

§data: [T; N]

Implementations§

source§

impl<T, const N: usize> VecX<T, N>
where T: Default + Copy + Sized + Send,

source

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

Generate a new VecX.

新しい VecX を生成する。

§Examples
use vec_x::{VecX};

let vec = VecX::new([1, 2, 3]);
source

pub fn new_with(value: T) -> Self
where T: Copy,

Generate a VecX initialized with a single value.

単一の値で初期化された VecX を生成する。

§Examples
use vec_x::{VecX};

let vec = VecX::new_with(1);

assert_eq!(vec, VecX::new([1, 1, 1]));
source

pub fn into<U>(self) -> VecX<U, N>
where T: Into<U>, U: Sized + Send,

Convert VecX<T, N> to VecX<U, N>.

VecX<T, N>VecX<U, N>に変換する。

§Examples
use vec_x::{VecX};

let vec:VecX<u8,3> = VecX::new([1, 2, 3]);
let vec_f64:VecX<f64,3> = vec.into();
source

pub fn from<U>(vec: VecX<U, N>) -> Self
where T: From<U>, U: Sized + Send,

Convert VecX<T, N> from VecX<U, N>.

VecX<U, N>からVecX<T, N>に変換する。

§Examples
use vec_x::{VecX};

let vec = VecX::new([1, 2, 3]);
let vec_i32:VecX<i32,3> = VecX::from(vec);
source

pub fn as_<U>(&self) -> VecX<U, N>
where U: AsPrimitive<T> + Sized + Send, T: AsPrimitive<U>,

Cast VecX<T, N> to VecX<U, N>. Array elements are cast in the same way as numeric types.

VecX<T, N>VecX<U, N>にキャストする。 配列の要素は数値型と同じ方法でキャストされる。

§Examples
use vec_x::{VecX};

let vec = VecX::new([1, 2, 3]);

let vec_f64:VecX<f64,3> = vec.as_();
source

pub fn fit<const M: usize>(&self) -> VecX<T, M>
where T: Default + Copy,

Match the number of elements in VecX<T, N> to M. If M > T, empty elements are filled with the value of T::default().

VecX<T, N>の要素数をMに合わせる。 M > Tである場合、空の要素はT::default()の値で満たされる。

§Examples
use vec_x::{VecX};

let vec = VecX::new([1, 2, 3]);
assert_eq!(vec.fit::<2>(), VecX::new([1, 2]));

let vec = VecX::new([1, 2, 3]);
assert_eq!(vec.fit::<5>(), VecX::new([1, 2, 3, 0, 0]));
source

pub fn batch<U, F>(self, callback: F) -> VecX<U, N>
where U: Sized + Send, F: Fn(T) -> U,

Apply a function to each element of VecX<T, N>.

VecX<T, N>の各要素に関数を適用する。

§Examples
use vec_x::{VecX};

let do_something = |v:u32| v.abs_diff(1);

let vec = VecX::new([1, 2, 3]);

assert_eq!(vec.batch(do_something), VecX::new([0, 1, 2]));
source

pub fn batch_with<U, V, F>(self, other: VecX<U, N>, callback: F) -> VecX<V, N>
where T: Copy, U: Copy + Sized + Send, V: Default + Copy + Sized + Send, F: Fn(T, U) -> V,

Apply a function to each element of VecX<T, N> and VecX<U, N>.

VecX<T, N>VecX<U, N>の各要素に関数を適用する。

§Examples
use vec_x::{VecX};

let vec1 = VecX::new([1, 5, 3]);
let vec2 = VecX::new([4, 2, 6]);

assert_eq!(vec1.batch_with(vec2, |a, b| a.min(b)), VecX::new([1, 2, 3]));

Trait Implementations§

source§

impl<T, U, const N: usize> Add<U> for VecX<T, N>
where T: Num + Copy + Sized + Send, U: Num + Copy + Into<T> + Sized + Send,

§

type Output = VecX<T, N>

The resulting type after applying the + operator.
source§

fn add(self, rhs: U) -> Self::Output

Performs the + operation. Read more
source§

impl<T, U, const N: usize> Add<VecX<U, N>> for VecX<T, N>
where T: Num + Copy + Sized + Send, U: Num + Copy + Into<T> + Sized + Send,

§

type Output = VecX<T, N>

The resulting type after applying the + operator.
source§

fn add(self, rhs: VecX<U, N>) -> Self::Output

Performs the + operation. Read more
source§

impl<T, U, const N: usize> AddAssign<U> for VecX<T, N>
where T: Num + AddAssign + Copy + Sized + Send, U: Num + AddAssign + Copy + Into<T> + Sized + Send,

source§

fn add_assign(&mut self, rhs: U)

Performs the += operation. Read more
source§

impl<T, U, const N: usize> AddAssign<VecX<U, N>> for VecX<T, N>
where T: Num + AddAssign + Copy + Sized + Send, U: Num + AddAssign + Copy + Into<T> + Sized + Send,

source§

fn add_assign(&mut self, rhs: VecX<U, N>)

Performs the += operation. Read more
source§

impl<T, const N: usize> Clone for VecX<T, N>
where T: Sized + Send + Clone,

source§

fn clone(&self) -> VecX<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, const N: usize> Debug for VecX<T, N>
where T: Sized + Send + Debug,

source§

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

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

impl<T, const N: usize> Default for VecX<T, N>
where T: Default + Copy + Sized + Send,

source§

fn default() -> Self

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

impl<T, U, const N: usize> Div<U> for VecX<T, N>
where T: Num + Copy + Sized + Send, U: Num + Copy + Into<T> + Sized + Send,

§

type Output = VecX<T, N>

The resulting type after applying the / operator.
source§

fn div(self, rhs: U) -> Self::Output

Performs the / operation. Read more
source§

impl<T, U, const N: usize> Div<VecX<U, N>> for VecX<T, N>
where T: Num + Copy + Sized + Send, U: Num + Copy + Into<T> + Sized + Send,

§

type Output = VecX<T, N>

The resulting type after applying the / operator.
source§

fn div(self, rhs: VecX<U, N>) -> Self::Output

Performs the / operation. Read more
source§

impl<T, U, const N: usize> DivAssign<U> for VecX<T, N>
where T: Num + DivAssign + Copy + Sized + Send, U: Num + DivAssign + Copy + Into<T> + Sized + Send,

source§

fn div_assign(&mut self, rhs: U)

Performs the /= operation. Read more
source§

impl<T, U, const N: usize> DivAssign<VecX<U, N>> for VecX<T, N>
where T: Num + DivAssign + Copy + Sized + Send, U: Num + DivAssign + Copy + Into<T> + Sized + Send,

source§

fn div_assign(&mut self, rhs: VecX<U, N>)

Performs the /= operation. Read more
source§

impl<T, const N: usize> Hash for VecX<T, N>
where T: Sized + Send + Hash,

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, const N: usize> Index<usize> for VecX<T, N>
where T: Sized + Send,

§

type Output = T

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<T, const N: usize> IndexMut<usize> for VecX<T, N>
where T: Sized + Send,

source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<T, U, const N: usize> Mul<U> for VecX<T, N>
where T: Num + Copy + Sized + Send, U: Num + Copy + Into<T> + Sized + Send,

§

type Output = VecX<T, N>

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl<T, U, const N: usize> Mul<VecX<U, N>> for VecX<T, N>
where T: Num + Copy + Sized + Send, U: Num + Copy + Into<T> + Sized + Send,

§

type Output = VecX<T, N>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: VecX<U, N>) -> Self::Output

Performs the * operation. Read more
source§

impl<T, U, const N: usize> MulAssign<U> for VecX<T, N>
where T: Num + MulAssign + Copy + Sized + Send, U: Num + MulAssign + Copy + Into<T> + Sized + Send,

source§

fn mul_assign(&mut self, rhs: U)

Performs the *= operation. Read more
source§

impl<T, U, const N: usize> MulAssign<VecX<U, N>> for VecX<T, N>
where T: Num + MulAssign + Copy + Sized + Send, U: Num + MulAssign + Copy + Into<T> + Sized + Send,

source§

fn mul_assign(&mut self, rhs: VecX<U, N>)

Performs the *= operation. Read more
source§

impl<T, const N: usize> PartialEq for VecX<T, N>
where T: Sized + Send + PartialEq,

source§

fn eq(&self, other: &VecX<T, N>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T, U, const N: usize> Rem<U> for VecX<T, N>
where T: Num + Copy + Sized + Send, U: Num + Copy + Into<T> + Sized + Send,

§

type Output = VecX<T, N>

The resulting type after applying the % operator.
source§

fn rem(self, rhs: U) -> Self::Output

Performs the % operation. Read more
source§

impl<T, U, const N: usize> Rem<VecX<U, N>> for VecX<T, N>
where T: Num + Copy + Sized + Send, U: Num + Copy + Into<T> + Sized + Send,

§

type Output = VecX<T, N>

The resulting type after applying the % operator.
source§

fn rem(self, rhs: VecX<U, N>) -> Self::Output

Performs the % operation. Read more
source§

impl<T, U, const N: usize> RemAssign<U> for VecX<T, N>
where T: Num + RemAssign + Copy + Sized + Send, U: Num + RemAssign + Copy + Into<T> + Sized + Send,

source§

fn rem_assign(&mut self, rhs: U)

Performs the %= operation. Read more
source§

impl<T, U, const N: usize> RemAssign<VecX<U, N>> for VecX<T, N>
where T: Num + RemAssign + Copy + Sized + Send, U: Num + RemAssign + Copy + Into<T> + Sized + Send,

source§

fn rem_assign(&mut self, rhs: VecX<U, N>)

Performs the %= operation. Read more
source§

impl<T, U, const N: usize> Sub<U> for VecX<T, N>
where T: Num + Copy + Sized + Send, U: Num + Copy + Into<T> + Sized + Send,

§

type Output = VecX<T, N>

The resulting type after applying the - operator.
source§

fn sub(self, rhs: U) -> Self::Output

Performs the - operation. Read more
source§

impl<T, U, const N: usize> Sub<VecX<U, N>> for VecX<T, N>
where T: Num + Copy + Sized + Send, U: Num + Copy + Into<T> + Sized + Send,

§

type Output = VecX<T, N>

The resulting type after applying the - operator.
source§

fn sub(self, rhs: VecX<U, N>) -> Self::Output

Performs the - operation. Read more
source§

impl<T, U, const N: usize> SubAssign<U> for VecX<T, N>
where T: Num + SubAssign + Copy + Sized + Send, U: Num + SubAssign + Copy + Into<T> + Sized + Send,

source§

fn sub_assign(&mut self, rhs: U)

Performs the -= operation. Read more
source§

impl<T, U, const N: usize> SubAssign<VecX<U, N>> for VecX<T, N>
where T: Num + SubAssign + Copy + Sized + Send, U: Num + SubAssign + Copy + Into<T> + Sized + Send,

source§

fn sub_assign(&mut self, rhs: VecX<U, N>)

Performs the -= operation. Read more
source§

impl<T, const N: usize> Copy for VecX<T, N>
where T: Sized + Send + Copy,

source§

impl<T, const N: usize> Eq for VecX<T, N>
where T: Sized + Send + Eq,

source§

impl<T, const N: usize> StructuralPartialEq for VecX<T, N>
where T: Sized + Send,

Auto Trait Implementations§

§

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

§

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

§

impl<T, const N: usize> Send for VecX<T, N>

§

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

§

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

§

impl<T, const N: usize> UnwindSafe for VecX<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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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,

§

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

§

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

impl<T, Rhs> NumAssignOps<Rhs> for T
where T: AddAssign<Rhs> + SubAssign<Rhs> + MulAssign<Rhs> + DivAssign<Rhs> + RemAssign<Rhs>,

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