hexga_math/geometry/vector/
vector1.rs

1use super::*;
2
3/// 1 dimension: x
4#[repr(C)]
5pub struct CoordX<T> { pub x: T }
6impl<T> Deref for Vector<T, 1>
7{
8    type Target=CoordX<T>;
9    fn deref(&self) -> &Self::Target { unsafe { std::mem::transmute(self) } }
10}
11impl<T> DerefMut for Vector<T, 1>
12{
13    fn deref_mut(&mut self) -> &mut Self::Target { unsafe { std::mem::transmute(self) } }
14}
15
16pub type Vector1<T> = Vector<T, 1>;
17
18impl<T> Vector<T,1> // Hardcode N here otherwise rust-analyser will not like it
19{
20    pub const fn new(x: T) -> Self { Self::from_array([x]) }
21    pub fn with_y(self, y: T) -> Vector2<T> { let [x] = self.array; Vector2::new(x, y) }
22}
23
24pub trait SplatCoord1 : Sized + Copy { fn splat1(self) -> Vector1<Self> { Vector1::splat(self) }}
25impl<T:Copy> SplatCoord1 for T {}
26
27impl<T> From<(T,)> for Vector1<T> { fn from(value: (T,)) -> Self { Vector1::new(value.0) }}
28impl<T> From<Vector1<T>> for (T,) { fn from(value: Vector1<T>) -> Self { let [x] = value.array; (x,) }}
29
30pub const fn vector1<T>(x: T) -> Vector1<T> { Vector1::new(x) }
31
32pub type Bool1 = Bool<1>;
33pub const fn bool1(x: bool) -> Bool1 { Bool1::new(x) }
34
35pub type Vec1 = Vector1<float>;
36pub const fn vec1(x: float) -> Vec1 { Vec1::new(x) }
37pub type Coef1 = Vec1;
38
39pub type Point1 = Point<1>;
40pub const fn point1(x: int) -> Point1 { Point1::new(x) }
41
42impl<T> HaveX<T> for Vector1<T>
43{
44    fn iter_x<'a>(&'a self) -> impl Iterator<Item=&'a T> where T: 'a {
45        self.array().as_slice()[0..=Self::X_INDEX].iter()
46    }
47
48    fn iter_x_mut<'a>(&'a mut self) -> impl Iterator<Item=&'a mut T> where T: 'a {
49        self.array_mut().as_mut_slice()[0..=Self::X_INDEX].iter_mut()
50    }
51}
52impl<T> HaveXAndOne<T> for Vector1<T> where T: One + Zero { const X: Self = Vector1::new(T::ONE); }
53impl<T> HaveXAndMinusOne<T> for Vector1<T> where T: MinusOne + Zero { const MINUS_X: Self = Vector1::new(T::MINUS_ONE); }
54
55impl<T> From<Vector2<T>> for Vector1<T> { fn from(value: Vector2<T>) -> Self { let [x,..] = value.to_array(); Self::new(x) } }
56impl<T> From<Vector3<T>> for Vector1<T> { fn from(value: Vector3<T>) -> Self { let [x,..] = value.to_array(); Self::new(x) } }
57impl<T> From<Vector4<T>> for Vector1<T> { fn from(value: Vector4<T>) -> Self { let [x,..] = value.to_array(); Self::new(x) } }
58
59pub type Vector1Iter<T> = VectorIter<Vector1<T>, 1>;
60
61pub(crate) mod prelude
62{
63    pub use super::
64    {
65        SplatCoord1,
66        Vector1,vector1,
67        Vec1,vec1,
68        Bool1,bool1,
69        Point1,point1,
70    };
71}