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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! Shader type wrappers.
//!
//! These types are used, mostly, to be passed to shaders as [`Uniform`] data.
//!
//! [`Uniform`]: crate::shader::Uniform

use std::ops::{Deref, DerefMut};

/// An array of values.
///
/// The array length is indexed at compile time with `N`.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Arr<T, const N: usize>(pub [T; N]);

impl<T, const N: usize> From<[T; N]> for Arr<T, N> {
  fn from(a: [T; N]) -> Self {
    Arr(a)
  }
}

impl<T, const N: usize> From<Arr<T, N>> for [T; N] {
  fn from(Arr(a): Arr<T, N>) -> Self {
    a
  }
}

impl<T, const N: usize> AsRef<[T; N]> for Arr<T, N> {
  fn as_ref(&self) -> &[T; N] {
    &self.0
  }
}

impl<T, const N: usize> Deref for Arr<T, N> {
  type Target = [T; N];

  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

impl<T, const N: usize> DerefMut for Arr<T, N> {
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.0
  }
}

impl<T, const N: usize> Arr<T, N> {
  /// Create a new array.
  pub const fn new(arr: [T; N]) -> Self {
    Self(arr)
  }
}

/// A 2 dimensional vector.
///
/// This is akin to a `[T; 2]`.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Vec2<T>(pub [T; 2]);

impl<T> From<[T; 2]> for Vec2<T> {
  fn from(a: [T; 2]) -> Self {
    Vec2(a)
  }
}

impl<T> From<Vec2<T>> for [T; 2] {
  fn from(Vec2(a): Vec2<T>) -> Self {
    a
  }
}

impl<T> AsRef<[T; 2]> for Vec2<T> {
  fn as_ref(&self) -> &[T; 2] {
    &self.0
  }
}

impl<T> Deref for Vec2<T> {
  type Target = [T; 2];

  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

impl<T> DerefMut for Vec2<T> {
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.0
  }
}

impl<T> Vec2<T> {
  /// Create a new vector.
  pub const fn new(x: T, y: T) -> Self {
    Self([x, y])
  }
}

/// A 3 dimensional vector.
///
/// This is akin to a `[T; 3]`.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Vec3<T>(pub [T; 3]);

impl<T> From<[T; 3]> for Vec3<T> {
  fn from(a: [T; 3]) -> Self {
    Vec3(a)
  }
}

impl<T> From<Vec3<T>> for [T; 3] {
  fn from(Vec3(a): Vec3<T>) -> Self {
    a
  }
}

impl<T> AsRef<[T; 3]> for Vec3<T> {
  fn as_ref(&self) -> &[T; 3] {
    &self.0
  }
}

impl<T> Deref for Vec3<T> {
  type Target = [T; 3];

  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

impl<T> DerefMut for Vec3<T> {
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.0
  }
}

impl<T> Vec3<T> {
  /// Create a new vector.
  pub const fn new(x: T, y: T, z: T) -> Self {
    Self([x, y, z])
  }
}

/// A 4 dimensional vector.
///
/// This is akin to a `[T; 4]`.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Vec4<T>(pub [T; 4]);

impl<T> From<[T; 4]> for Vec4<T> {
  fn from(a: [T; 4]) -> Self {
    Vec4(a)
  }
}

impl<T> From<Vec4<T>> for [T; 4] {
  fn from(Vec4(a): Vec4<T>) -> Self {
    a
  }
}

impl<T> AsRef<[T; 4]> for Vec4<T> {
  fn as_ref(&self) -> &[T; 4] {
    &self.0
  }
}

impl<T> Deref for Vec4<T> {
  type Target = [T; 4];

  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

impl<T> DerefMut for Vec4<T> {
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.0
  }
}

impl<T> Vec4<T> {
  /// Create a new vector.
  pub const fn new(x: T, y: T, z: T, w: T) -> Self {
    Self([x, y, z, w])
  }
}

macro_rules! matrix {
  ($t:ident, $r:literal, $c:literal) => {
    #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
    /// Matrix N×M.
    pub struct $t<T>(pub [[T; $c]; $r]);

    impl<T> From<[[T; $c]; $r]> for $t<T> {
      fn from(a: [[T; $c]; $r]) -> Self {
        $t(a)
      }
    }

    impl<T> From<$t<T>> for [[T; $c]; $r] {
      fn from($t(a): $t<T>) -> Self {
        a
      }
    }

    impl<T> AsRef<[[T; $c]; $r]> for $t<T> {
      fn as_ref(&self) -> &[[T; $c]; $r] {
        &self.0
      }
    }

    impl<T> Deref for $t<T> {
      type Target = [[T; $c]; $r];

      fn deref(&self) -> &Self::Target {
        &self.0
      }
    }

    impl<T> DerefMut for $t<T> {
      fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
      }
    }

    impl<T> $t<T> {
      /// Create a matrix via its array representation.
      pub fn new(array: impl Into<[[T; $c]; $r]>) -> Self {
        $t(array.into())
      }
    }
  };
}

matrix!(Mat22, 2, 2);
matrix!(Mat33, 3, 3);
matrix!(Mat44, 4, 4);