Skip to main content

clt_database/numeric/
nonnan.rs

1#[repr(transparent)]
2#[derive(Debug, Clone, Copy, PartialEq)]
3#[cfg_attr(clt_turso_feature = "serde", derive(serde::Serialize, serde::Deserialize))]
4pub struct NonNan(f64);
5
6impl NonNan {
7    pub const fn new(value: f64) -> Option<Self> {
8        if value.is_nan() {
9            return None;
10        }
11
12        Some(NonNan(value))
13    }
14}
15
16impl PartialEq<NonNan> for f64 {
17    fn eq(&self, other: &NonNan) -> bool {
18        *self == other.0
19    }
20}
21
22impl PartialEq<f64> for NonNan {
23    fn eq(&self, other: &f64) -> bool {
24        self.0 == *other
25    }
26}
27
28impl PartialOrd<f64> for NonNan {
29    fn partial_cmp(&self, other: &f64) -> Option<std::cmp::Ordering> {
30        self.0.partial_cmp(other)
31    }
32}
33
34impl PartialOrd<NonNan> for f64 {
35    fn partial_cmp(&self, other: &NonNan) -> Option<std::cmp::Ordering> {
36        self.partial_cmp(&other.0)
37    }
38}
39
40impl From<i64> for NonNan {
41    fn from(value: i64) -> Self {
42        NonNan(value as f64)
43    }
44}
45
46impl From<NonNan> for f64 {
47    fn from(value: NonNan) -> Self {
48        value.0
49    }
50}
51
52impl std::ops::Deref for NonNan {
53    type Target = f64;
54
55    fn deref(&self) -> &Self::Target {
56        &self.0
57    }
58}
59
60impl std::ops::Add for NonNan {
61    type Output = Option<NonNan>;
62
63    fn add(self, rhs: Self) -> Self::Output {
64        Self::new(self.0 + rhs.0)
65    }
66}
67
68impl std::ops::Sub for NonNan {
69    type Output = Option<NonNan>;
70
71    fn sub(self, rhs: Self) -> Self::Output {
72        Self::new(self.0 - rhs.0)
73    }
74}
75
76impl std::ops::Mul for NonNan {
77    type Output = Option<NonNan>;
78
79    fn mul(self, rhs: Self) -> Self::Output {
80        Self::new(self.0 * rhs.0)
81    }
82}
83
84impl std::ops::Div for NonNan {
85    type Output = Option<NonNan>;
86
87    fn div(self, rhs: Self) -> Self::Output {
88        Self::new(self.0 / rhs.0)
89    }
90}
91
92impl std::ops::Rem for NonNan {
93    type Output = Option<NonNan>;
94
95    fn rem(self, rhs: Self) -> Self::Output {
96        Self::new(self.0 % rhs.0)
97    }
98}
99
100impl std::fmt::Display for NonNan {
101    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102        self.0.fmt(f)
103    }
104}
105
106impl std::ops::Neg for NonNan {
107    type Output = Self;
108
109    fn neg(self) -> Self::Output {
110        Self(-self.0)
111    }
112}