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

👎Deprecated

Generate a VecX initialized with a single value.

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

§Deprecated

This method is deprecated. Use VecX::from() instead.

互換性のために残されていますが、VecX::from()を使用してください。

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

Source§

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,

Source§

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,

Source§

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,

Source§

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> From<[T; N]> for VecX<T, N>
where T: Sized + Send + Copy,

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§

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

Converts to this type from the input type.
Source§

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

§Examples

use vec_x::{VecX};

let vec:VecX<u32,3> = VecX::from(1);

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

fn from(value: T) -> Self

Converts to this type from the input type.
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,

Source§

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,

Source§

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,

Source§

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

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

Compare all elements.

全ての要素を比較する。

§Examples

use vec_x::{VecX};

let vec1 = VecX::new([1, 2, 3]);
let vec2 = VecX::new([1, 2, 3]);
assert_eq!(vec1, vec2);
assert!(vec1 <= vec2);
assert!(vec1 >= vec2);

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

let vec1 = VecX::new([1, 2, 3]);
let vec2 = VecX::new([1, 2, 2]);
assert_ne!(vec1, vec2);
assert!(vec1 > vec2);
use vec_x::{VecX};

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

assert!(vec1 > vec2); // panic!
use vec_x::{VecX};

let vec1 = VecX::new([1, 2, 1]);
let vec2 = VecX::new([2, 1, 2]);

assert!(vec1 < vec2|| vec1 == vec2 || vec1 > vec2); // panic!
Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
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,

Source§

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,

Source§

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,

Source§

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,

Source§

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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> 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>,