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
#![no_std]

extern crate num;

pub mod vec1;
pub mod vec2;
pub mod vec3;
pub mod vec4;
pub mod math;
#[macro_use]
pub mod macros;

// Reexports
pub use vec1::Vec1;
pub use vec2::Vec2;
pub use vec3::Vec3;
pub use vec4::Vec4;
pub use math::{VecNum, VecInt, VecUnsigned, VecSigned, VecFloat};
pub use num::{Num, Integer, Unsigned, Signed, Float};

pub trait VecItem: Copy + Clone + PartialEq {}

pub trait Vec {
    type Item: VecItem;
}

impl VecItem for u8 {}
impl VecItem for u16 {}
impl VecItem for u32 {}
impl VecItem for u64 {}
impl VecItem for u128 {}

impl VecItem for i8 {}
impl VecItem for i16 {}
impl VecItem for i32 {}
impl VecItem for i64 {}
impl VecItem for i128 {}

impl VecItem for f32 {}
impl VecItem for f64 {}

#[cfg(not(feature = "large_defaults"))]
pub mod defaults {
    use super::*;

    pub type Vec1u = Vec1<u32>;
    pub type Vec2u = Vec2<u32>;
    pub type Vec3u = Vec3<u32>;
    pub type Vec4u = Vec4<u32>;

    pub type Vec1i = Vec1<i32>;
    pub type Vec2i = Vec2<i32>;
    pub type Vec3i = Vec3<i32>;
    pub type Vec4i = Vec4<i32>;

    pub type Vec1f = Vec1<f32>;
    pub type Vec2f = Vec2<f32>;
    pub type Vec3f = Vec3<f32>;
    pub type Vec4f = Vec4<f32>;
}

#[cfg(feature = "large_defaults")]
pub mod defaults {
    use super::*;

    pub type Vec1u = Vec1<u64>;
    pub type Vec2u = Vec2<u64>;
    pub type Vec3u = Vec3<u64>;
    pub type Vec4u = Vec4<u64>;

    pub type Vec1i = Vec1<i64>;
    pub type Vec2i = Vec2<i64>;
    pub type Vec3i = Vec3<i64>;
    pub type Vec4i = Vec4<i64>;

    pub type Vec1f = Vec1<f64>;
    pub type Vec2f = Vec2<f64>;
    pub type Vec3f = Vec3<f64>;
    pub type Vec4f = Vec4<f64>;
}

pub mod prelude {
	pub use super::{Num, Integer, Unsigned, Signed, Float};

    pub use super::Vec1;
    pub use super::Vec2;
    pub use super::Vec3;
    pub use super::Vec4;

    pub use super::Vec;
    pub use super::VecNum;
    pub use super::VecInt;
    pub use super::VecUnsigned;
    pub use super::VecSigned;
    pub use super::VecFloat;

    pub use super::defaults::*;
}

#[cfg(test)]
mod tests {
    use super::prelude::*;

    #[test]
    fn basic_construction() {
        let _ = Vec1::new(1u32);
        let _ = Vec2::new(1u32, 2u32);
        let _ = Vec3::new(1u32, 2u32, 3u32);
        let _ = Vec4::new(1u32, 2u32, 3u32, 4u32);

        let _ = Vec2::from((1u32, 2u32));
        let _ = Vec3::from((1u32, 2u32, 3u32));
        let _ = Vec4::from((1u32, 2u32, 3u32, 4u32));
    }

    #[test]
    fn basic_operators() {
        let _ = Vec1u::new(0)          + Vec1u::new(3);
        let _ = Vec2u::new(0, 1)       + Vec2u::new(3, 2);
        let _ = Vec3u::new(0, 1, 2)    + Vec3u::new(3, 2, 1);
        let _ = Vec4u::new(0, 1, 2, 3) + Vec4u::new(3, 2, 1, 0);

        let _ = Vec1u::new(3)          - Vec1u::new(3);
        let _ = Vec2u::new(3, 2)       - Vec2u::new(3, 2);
        let _ = Vec3u::new(3, 2, 1)    - Vec3u::new(3, 2, 1);
        let _ = Vec4u::new(3, 2, 1, 0) - Vec4u::new(3, 2, 1, 0);

        let _ = Vec1u::new(1)          * Vec1u::new(4);
        let _ = Vec2u::new(1, 2)       * Vec2u::new(4, 3);
        let _ = Vec3u::new(1, 2, 3)    * Vec3u::new(4, 3, 2);
        let _ = Vec4u::new(1, 2, 3, 4) * Vec4u::new(4, 3, 2, 1);

        let _ = Vec1u::new(4)          / Vec1u::new(5);
        let _ = Vec2u::new(4, 3)       / Vec2u::new(5, 4);
        let _ = Vec3u::new(4, 3, 2)    / Vec3u::new(5, 4, 3);
        let _ = Vec4u::new(4, 3, 2, 1) / Vec4u::new(5, 4, 3, 2);
    }

    fn pass_vec1u(_: Vec1u) {}
    fn pass_vec2u(_: Vec2u) {}
    fn pass_vec3u(_: Vec3u) {}
    fn pass_vec4u(_: Vec4u) {}

    #[test]
    fn test_pass() {
        pass_vec1u(vec1!(1u32));
        pass_vec2u(vec2!(1u32, 2u32));
        pass_vec3u(vec3!(1u32, 2u32, 3u32));
        pass_vec4u(vec4!(1u32, 2u32, 3u32, 4u32));
    }

    fn length_of<V: VecFloat>(vec: V) -> V::Item where V::Item: Float {
        vec.length()
    }

    #[test]
    fn test_passing() {
        let v2 = vec2!(1, 2) + vec2!(2, 1);
        //println!("{:?}", v2);

        let v3 = vec3!(1.5, 2.0, 6.5) * vec3!(3.2, 7.7, 8.2);
        let l = v3.length();
        //println!("{:?}", v3.length());
    }
}