1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
/// Trait for structs that represent a [`Vector`], will be implemented by default when using the [`impl_vector`] Macro.
pub trait Vector<T, const LEN: usize>: IntoIterator {
/// Returns the name of the [`Vector`] struct.
///
/// ## Example
/// ```rust
/// let vector = Vector4::new(0, 0, 0, 0);
/// assert_eq!(vector.name(), "Vector4");
/// ```
fn name(&self) -> &'static str;
/// Returns the size of the [`Vector`] struct in bytes.
///
/// ## Example
/// ```rust
/// let vector = Vector2::<u16>::new(0, 0);
/// assert_eq!(vector.size(), 4);
/// ```
fn size(&self) -> usize {
return core::mem::size_of::<T>() * LEN;
}
/// Returns the length of the [`Vector`] struct, length is equal to the number of fields within the struct.
///
/// ## Example
/// ```rust
/// let vec2 = Vector2::new(0, 0);
/// let vec3 = Vector3::new(0, 0, 0);
/// let vec4 = Vector4::new(0, 0, 0, 0);
///
/// assert_eq!(vec2.len(), 2);
/// assert_eq!(vec3.len(), 3);
/// assert_eq!(vec4.len(), 4);
/// ```
fn len(&self) -> usize {
return LEN;
}
/// Converts the given [`Vector`] into an array coresponding to the size of the [`Vector`].
///
/// ## Example
/// ```rust
/// let vector = Vector3::new(1, 2, 3);
/// assert_eq!(vector.to_array(), [1, 2, 3]);
/// ```
fn to_array(self) -> [T; LEN];
/// Converts the given [`Vector`] into a [`Vec`] coresponding to the size of the [`Vector`].
///
/// ## Example
/// ```rust
/// let vector = Vector3::new(1, 2, 3);
/// assert_eq!(vector.to_vec(), vec![1, 2, 3]);
/// ```
fn to_vec(self) -> std::vec::Vec<T>;
}
/// Trait for structs that represent a [`Vector`] that contains primitive integer data types.
pub trait IntegerVector<T: num_traits::PrimInt, const LEN: usize>: Vector<T, LEN> {
/// Raises all numbers within the [`IntegerVector`] to the specified power.
///
/// ## Example
/// ```rust
/// let vector = Vector3::new(2, 4, 6).pow(2);
/// assert_eq!(vector, Vector3::new(4, 16, 36));
/// ```
fn pow(self, n: u32) -> Self;
}
/// Trait for structs that represent a [`Vector`] that contains floating-point data types.
pub trait FloatingPointVector<T: num_traits::Float, const LEN: usize>: Vector<T, LEN> {
/// Converts all numbers within the [`FloatingPointVector`] to the largest integer less than or equal to the value.
///
/// ## Example
/// ```rust
/// let vector = Vector2::new(4.25, 5.9).floor();
/// assert_eq!(vector, Vector2::new(4.0, 5.0));
/// ```
fn floor(self) -> Self;
/// Converts all numbers within the [`FloatingPointVector`] to the largest integer greater than or equal to the value.
///
/// ## Example
/// ```rust
/// let vector = Vector2::new(4.25, 5.9).ceil();
/// assert_eq!(vector, Vector2::new(5.0, 6.0));
/// ```
fn ceil(self) -> Self;
/// Converts all numbers within the [`FloatingPointVector`] to the nearest integer.
///
/// ## Example
/// ```rust
/// let vector = Vector2::new(4.25, 5.9).round();
/// assert_eq!(vector, Vector2::new(4.0, 6.0));
/// ```
fn round(self) -> Self;
/// Converts all numbers within the [`FloatingPointVector`] to their absolute value.
///
/// ## Example
/// ```rust
/// let vector = Vector4::new(-3.0, 4.0, 5.3, -9.87).abs();
/// assert_eq!(vector, Vector4::new(3.0, 4.0, 5.3, 9.87));
/// ```
fn abs(self) -> Self;
/// Raises all numbers within the [`FloatingPointVector`] to an integer power.
///
/// ## Example
/// ```rust
/// let vector = Vector2::new(2.0, 4.0).powi(2);
/// assert_eq!(vector, Vector2::new(4, 16));
/// ```
fn powi(self, n: i32) -> Self;
/// Raises all numbers within the [`FloatingPointVector`] to a floating point power.
///
/// ## Example
/// ```rust
/// let vector = Vector2::new(2.0, 4.0).powf(2.0);
/// assert_eq!(vector, Vector2::new(4.0, 16.0));
/// ```
fn powf(self, n: T) -> Self;
/// Sets all numbers within the [`FloatingPointVector`] to their integer parts.
///
/// ## Example
/// ```rust
/// let vector = Vector3::new(1.5, 2.34, 3.33).trunc();
/// assert_eq!(vector, Vector3::new(1.0, 2.0, 3.0));
/// ```
fn trunc(self) -> Self;
/// Sets all numbers within the [`FloatingPointVector`] to their fractional parts.
///
/// ## Example
/// ```rust
/// let vector = Vector3::new(1.5, 2.34, 3.33).fract();
/// assert_eq!(vector, Vector3::new(0.5, 0.34, 0.33));
/// ```
fn fract(self) -> Self;
/// Sets all numbers within the [`FloatingPointVector`] to their square-root.
///
/// ## Example
/// ```rust
/// let vector = Vector2::new(64.0, 25.0);
/// assert_eq!(vector, Vector2::new(8.0, 5.0));
/// ```
fn sqrt(self) -> Self;
}
/// Trait for structs that represent a [`Vector`] and that can be converted into tuples.
///
/// ## Example
/// ```rust
/// pub struct Vector1<T> {
/// pub x: T,
/// }
///
/// // Implement [`Vector`] Trait for `Vector1`
/// impl_vector!(Vector1 { x }, 1);
///
/// impl<T> TuplableVector<T, { Vector1::<()>::LEN }> for Vector1<T> {
/// type Output = (T);
///
/// fn to_tuple(self) -> Self::Output {
/// return (self.x);
/// }
/// }
/// ```
pub trait TuplableVector<T, const LEN: usize>: Vector<T, LEN> {
type Output;
/// Converts the [`TuplableVector`] into a tuple representing its values.
///
/// ## Example:
/// ```rust
/// let tuple = Vector2::new(1, 2).to_tuple();
/// assert_eq!(tuple, (1, 2));
/// ```
fn to_tuple(self) -> Self::Output;
}