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
//! Contains implementations to convert between `UVec`/`IVec` and `Vec`/`DVec`.
//!
//! To realize such conversions we make use of crate-private traits `TryFromExt` and `TryIntoExt` to
//! simulate the behaviour of the official [From] and [Into].

use crate::util::{TryFromExt, TryIntoExt};
use crate::*;
use core::convert::TryFrom;
use std::error::Error;
use std::fmt;

/// The error type that may happen when converting a `f32` or `f64` to any other numerical
/// representation.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum FloatConversionError {
    NaN,
    Infinite,
    PosOverflow,
    NegOverflow,
}

impl fmt::Display for FloatConversionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FloatConversionError::NaN => f.write_str("NaN"),
            FloatConversionError::Infinite => f.write_str("Infinite"),
            FloatConversionError::PosOverflow => f.write_str("PosOverflow"),
            FloatConversionError::NegOverflow => f.write_str("NegOverflow"),
        }
    }
}

impl Error for FloatConversionError {}

macro_rules! impl_try_from_float {
    ($source:ty => $($target:ident),*) => {$(
        impl TryFromExt<$source> for $target {
            type Error = FloatConversionError;

            /// Tries to convert the source to Self in a lossy way, flooring any float value.
            ///
            /// # Errors
            /// * `NaN` - If the float value is `NaN`.
            /// * `Infinite` - If the float value is infinity or negative infinity.
            /// * `PosOverflow` - If the float value would be greater than the target max value.
            /// * `NegOverflow` - If the float value would be less than the target min value.
            #[inline]
            fn try_from(source: $source) -> Result<Self, Self::Error> {
                if source.is_nan() {
                    return Err(FloatConversionError::NaN)
                }
                if source.is_infinite() {
                    return Err(FloatConversionError::Infinite)
                }

                let min = Self::MIN as $source;
                if source < min {
                    return Err(FloatConversionError::NegOverflow)
                }

                let max = Self::MAX as $source;
                if source > max {
                    return Err(FloatConversionError::PosOverflow)
                }

                Ok(source as Self)
            }
        }
    )*}
}

impl_try_from_float!(f32 => i32, u32);
impl_try_from_float!(f64 => i32, u32);

macro_rules! impl_try_from_float_vec {
    ($(($name:ident => $target:ident, [$($var:ident),*])),+) => {
        $(
        impl TryFrom<$name> for $target {
            type Error = FloatConversionError;

            /// Tries to convert the source to Self in a lossy way, flooring any float value.
            ///
            /// # Errors
            /// * `NaN` - If a float value is `NaN`.
            /// * `NotFinite` - If a float value is infinity or negative infinity.
            /// * `PosOverflow` - If a float value would be greater than the the self.component max value.
            /// * `NegOverflow` - If a float value would be less than the self.component min value.
            #[inline]
            fn try_from(v: $name) -> Result<Self, Self::Error> {
                Ok(Self::new($(v.$var.try_into()?,)* ))
            }
        }
        )+
    }
}

macro_rules! impl_from_int_vec {
    ($(($name:ident => $target:ident, $target_type:ident, [$($var:ident),*])),+) => {
        $(
        impl From<$name> for $target {
            #[inline]
            fn from(v: $name) -> Self {
                Self::new($(v.$var as $target_type,)*)
            }
        }
        )+
    };
}

impl_try_from_float_vec!(
    (Vec2 => IVec2, [x, y]),
    (Vec3 => IVec3, [x, y, z]),
    (Vec4 => IVec4, [x, y, z, w]),

    (Vec2 => UVec2, [x, y]),
    (Vec3 => UVec3, [x, y, z]),
    (Vec4 => UVec4, [x, y, z, w])
);

#[cfg(feature = "f64")]
impl_try_from_float_vec!(
    (DVec2 => IVec2, [x, y]),
    (DVec3 => IVec3, [x, y, z]),
    (DVec4 => IVec4, [x, y, z, w]),

    (DVec2 => UVec2, [x, y]),
    (DVec3 => UVec3, [x, y, z]),
    (DVec4 => UVec4, [x, y, z, w])
);

impl_from_int_vec!(
    (IVec2 => Vec2, f32, [x, y]),
    (IVec3 => Vec3, f32, [x, y, z]),
    (IVec4 => Vec4, f32, [x, y, z, w]),

    (UVec2 => Vec2, f32, [x, y]),
    (UVec3 => Vec3, f32, [x, y, z]),
    (UVec4 => Vec4, f32, [x, y, z, w])
);

#[cfg(feature = "f64")]
impl_from_int_vec!(
    (IVec2 => DVec2, f64, [x, y]),
    (IVec3 => DVec3, f64, [x, y, z]),
    (IVec4 => DVec4, f64, [x, y, z, w]),

    (UVec2 => DVec2, f64, [x, y]),
    (UVec3 => DVec3, f64, [x, y, z]),
    (UVec4 => DVec4, f64, [x, y, z, w])
);

// tests only for Vec2
#[cfg(test)]
mod tests {
    use crate::*;
    use core::convert::TryFrom;

    #[test]
    #[cfg(feature = "int")]
    fn vec2_to_ivec2_exact() {
        let vec2 = Vec2::new(1.0, 2.0);
        let ivec2 = IVec2::try_from(vec2);

        assert_eq!(ivec2.ok().unwrap(), IVec2::new(1, 2));
    }

    #[test]
    #[cfg(feature = "int")]
    fn vec2_to_ivec2_floored() {
        let vec2 = Vec2::new(1.99, 2.99);
        let ivec2 = IVec2::try_from(vec2);

        assert_eq!(ivec2.ok().unwrap(), IVec2::new(1, 2));
    }

    #[test]
    #[cfg(feature = "int")]
    fn vec2_to_ivec2_nan() {
        let vec2 = Vec2::new(f32::NAN, 0.0);
        let ivec2 = IVec2::try_from(vec2);

        assert_eq!(ivec2.err().unwrap(), FloatConversionError::NaN);
    }

    #[test]
    #[cfg(feature = "int")]
    fn vec2_to_ivec2_infinity() {
        let vec2 = Vec2::new(f32::INFINITY, 0.0);
        let ivec2 = IVec2::try_from(vec2);

        assert_eq!(ivec2.err().unwrap(), FloatConversionError::Infinite);
    }

    #[test]
    #[cfg(feature = "int")]
    fn vec2_to_ivec2_neg_infinity() {
        let vec2 = Vec2::new(f32::NEG_INFINITY, 0.0);
        let ivec2 = IVec2::try_from(vec2);

        assert_eq!(ivec2.err().unwrap(), FloatConversionError::Infinite);
    }

    #[test]
    #[cfg(feature = "int")]
    fn vec2_to_ivec2_pos_overflow() {
        let vec2 = Vec2::new(f32::MAX, 0.0);
        let ivec2 = IVec2::try_from(vec2);

        assert_eq!(ivec2.err().unwrap(), FloatConversionError::PosOverflow);
    }

    #[test]
    #[cfg(feature = "int")]
    fn vec2_to_ivec2_neg_overflow() {
        let vec2 = Vec2::new(f32::MIN, 0.0);
        let ivec2 = IVec2::try_from(vec2);

        assert_eq!(ivec2.err().unwrap(), FloatConversionError::NegOverflow);
    }

    #[test]
    #[cfg(feature = "int")]
    fn ivec2_to_vec2() {
        let ivec2 = IVec2::new(1, 2);
        let vec2 = Vec2::from(ivec2);

        assert_eq!(vec2, Vec2::new(1.0, 2.0));
    }

    #[test]
    #[cfg(feature = "int")]
    fn vec2_to_uvec2_neg_overflow() {
        let vec2 = Vec2::new(-1.0, 0.0);
        let uvec2 = UVec2::try_from(vec2);

        assert_eq!(uvec2.err().unwrap(), FloatConversionError::NegOverflow);
    }
}