Skip to main content

topcoat_runtime/surrogate/
_f64.rs

1use ref_cast::RefCast;
2use serde::{Deserialize, Serialize};
3
4use crate::{BoolSurrogate, impl_surrogate, impl_surrogate_mut, impl_surrogate_ref};
5
6#[derive(Debug, RefCast, Clone, Copy, Serialize, Deserialize)]
7#[repr(transparent)]
8#[serde(transparent)]
9pub struct F64Surrogate(f64);
10
11impl F64Surrogate {
12    #[inline]
13    pub(crate) const fn new(v: f64) -> Self {
14        Self(v)
15    }
16}
17
18impl_surrogate!(f64, F64Surrogate);
19impl_surrogate_ref!(f64, F64Surrogate);
20impl_surrogate_mut!(f64, F64Surrogate);
21
22macro_rules! impl_math_op {
23    ($trait:ident, $method:ident, $op:tt) => {
24        impl core::ops::$trait for F64Surrogate {
25            type Output = F64Surrogate;
26
27            #[inline]
28            fn $method(self, rhs: F64Surrogate) -> F64Surrogate {
29                F64Surrogate(self.0 $op rhs.0)
30            }
31        }
32    };
33}
34
35impl_math_op!(Add, add, +);
36impl_math_op!(Sub, sub, -);
37impl_math_op!(Mul, mul, *);
38impl_math_op!(Div, div, /);
39
40impl core::ops::Neg for F64Surrogate {
41    type Output = F64Surrogate;
42
43    #[inline]
44    fn neg(self) -> F64Surrogate {
45        F64Surrogate(-self.0)
46    }
47}
48
49macro_rules! impl_cmp_op {
50    ($method:ident, $op:tt) => {
51        impl F64Surrogate {
52            #[inline]
53            pub fn $method(&self, rhs: &F64Surrogate) -> BoolSurrogate {
54                BoolSurrogate::new(self.0 $op rhs.0)
55            }
56        }
57    };
58}
59
60impl_cmp_op!(eq, ==);
61impl_cmp_op!(ne, !=);
62impl_cmp_op!(gt, >);
63impl_cmp_op!(lt, <);
64impl_cmp_op!(ge, >=);
65impl_cmp_op!(le, <=);
66
67impl std::fmt::Display for F64Surrogate {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        self.0.fmt(f)
70    }
71}