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
pub trait Is { fn is(&self, other: &Self) -> bool; } impl Is for f32 { fn is(&self, other: &f32) -> bool { let left_bits: u32 = unsafe { ::std::mem::transmute(*self) }; let right_bits: u32 = unsafe { ::std::mem::transmute(*other) }; left_bits == right_bits } } impl Is for f64 { fn is(&self, other: &f64) -> bool { let left_bits: u64 = unsafe { ::std::mem::transmute(*self) }; let right_bits: u64 = unsafe { ::std::mem::transmute(*other) }; left_bits == right_bits } } impl<T: Is> Is for Vec<T> { fn is(&self, other: &Self) -> bool { if self.len() != other.len() { return false; } for (i, left) in self.iter().enumerate() { if !left.is(&other[i]) { return false; } } true } } impl Is for [f32; 20] { fn is(&self, other: &Self) -> bool { for i in 0..20 { if !self[i].is(&other[i]) { return false; } } true } } #[cfg(test)] mod test_float_is { use super::Is; #[test] fn test_f32_is() { assert!(::std::f32::NAN.is(&::std::f32::NAN)) } }