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
use std::hash::{Hash, Hasher}; macro_rules! finite { (@op => $opname:ty, $opnamety:ty, $func:tt, $name:tt, $ty:ty) => { impl $opname for $name { type Output = Option<$ty>; fn $func(self, other: Self) -> Option<$ty> { let result = (self.0).$func(other.0); if result.is_finite() { Some(result) } else { None } } } impl $opnamety for $name { type Output = $ty; fn $func(self, other: $ty) -> $ty { (self.0).$func(other) } } }; ($name:tt, $ty:ty) => ( finite!(@finish => $name, $ty, stringify!($ty)); ); (@finish => $name:tt, $ty:ty, $tyname:expr) => { #[doc = "A finite `"] #[doc = $tyname] #[doc = "`. May not be infinite nor NaN."] #[derive(Debug, Clone, Copy)] pub struct $name($ty); impl $name { #[doc = "Create a new finite `"] #[doc = $tyname] #[doc = "`. Will return `None` if given value is infinite or NaN."] pub fn new(n: $ty) -> Option<Self> { if n.is_finite() { Some(Self(n)) } else { None } } #[inline(always)] pub fn checked_add(self, other: $ty) -> Option<Self> { Self::new(std::ops::Add::add(self, other)) } #[inline(always)] pub fn checked_sub(self, other: $ty) -> Option<Self> { Self::new(std::ops::Sub::sub(self, other)) } #[inline(always)] pub fn checked_mul(self, other: $ty) -> Option<Self> { Self::new(std::ops::Mul::mul(self, other)) } #[inline(always)] pub fn checked_div(self, other: $ty) -> Option<Self> { Self::new(std::ops::Div::div(self, other)) } } impl PartialEq for $name { fn eq(&self, other: &Self) -> bool { self.0 == other.0 } } impl PartialEq<$ty> for $name { fn eq(&self, other: &$ty) -> bool { &self.0 == other } } impl Eq for $name {} impl PartialOrd for $name { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { self.0.partial_cmp(&other.0) } } impl PartialOrd<$ty> for $name { fn partial_cmp(&self, other: &$ty) -> Option<std::cmp::Ordering> { self.0.partial_cmp(other) } } impl Ord for $name { fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.0.partial_cmp(&other.0).expect("must be finite") } } finite!(@op => std::ops::Add, std::ops::Add<$ty>, add, $name, $ty); finite!(@op => std::ops::Sub, std::ops::Sub<$ty>, sub, $name, $ty); finite!(@op => std::ops::Div, std::ops::Div<$ty>, div, $name, $ty); finite!(@op => std::ops::Mul, std::ops::Mul<$ty>, mul, $name, $ty); impl std::convert::TryFrom<$ty> for $name { type Error = $crate::TryFromFloatError; fn try_from(value: $ty) -> Result<Self, Self::Error> { match Self::new(value) { Some(v) => Ok(v), None => Err($crate::TryFromFloatError(value.classify())) } } } } } pub struct TryFromFloatError(std::num::FpCategory); finite!(FiniteF32, f32); finite!(FiniteF64, f64); impl Hash for FiniteF32 { fn hash<H: Hasher>(&self, state: &mut H) { state.write_u32(unsafe { std::mem::transmute::<f32, u32>(self.0) }); } } impl Hash for FiniteF64 { fn hash<H: Hasher>(&self, state: &mut H) { state.write_u64(unsafe { std::mem::transmute::<f64, u64>(self.0) }); } } #[cfg(test)] mod tests { use super::*; use std::{f32, f64}; #[test] fn smoke() { assert!(FiniteF32::new(1f32).is_some()); assert!(FiniteF64::new(42f64).is_some()); assert!(FiniteF32::new(f32::NAN).is_none()); assert!(FiniteF64::new(f64::NAN).is_none()); assert!(FiniteF32::new(f32::INFINITY).is_none()); assert!(FiniteF64::new(f64::INFINITY).is_none()); assert!(FiniteF32::new(f32::NEG_INFINITY).is_none()); assert!(FiniteF64::new(f64::NEG_INFINITY).is_none()); } #[test] fn cmp32() { let finite = FiniteF32::new(1f32).unwrap(); assert_eq!(finite < 32f32, true); assert_eq!(finite == 1f32, true); assert_eq!(finite > -1f32, true); assert_eq!(finite > f32::NAN, false); assert_eq!(finite > f32::NEG_INFINITY, true); assert_eq!(finite < f32::INFINITY, true); } #[test] fn cmp64() { let finite = FiniteF64::new(1f64).unwrap(); assert_eq!(finite < 64f64, true); assert_eq!(finite == 1f64, true); assert_eq!(finite > -1f64, true); assert_eq!(finite > f64::NAN, false); assert_eq!(finite > f64::NEG_INFINITY, true); assert_eq!(finite < f64::INFINITY, true); } #[test] fn add32() { let finite = FiniteF32::new(1f32).unwrap(); assert_eq!(finite + 32f32, 33f32); assert_eq!(finite - 32f32, -31f32); assert_eq!(finite + f32::INFINITY, f32::INFINITY); assert_eq!(finite - f32::INFINITY, f32::NEG_INFINITY); } #[test] fn add64() { let finite = FiniteF64::new(1f64).unwrap(); assert_eq!(finite + 32f64, 33f64); assert_eq!(finite - 32f64, -31f64); assert_eq!(finite + f64::INFINITY, f64::INFINITY); assert!(finite.checked_add(f64::INFINITY).is_none()); assert_eq!(finite - f64::INFINITY, f64::NEG_INFINITY); assert!(finite.checked_sub(f64::INFINITY).is_none()); } #[test] fn hash() { let mut map = std::collections::HashMap::new(); let f = FiniteF32::new(32f32).unwrap(); map.insert(f, "oh yes"); assert_eq!(map[&f], "oh yes"); } }