1pub trait Numeric: Clone + Copy + PartialEq + PartialOrd + 'static {
3 const INTEGRAL: bool;
5
6 const MIN: Self;
8
9 const MAX: Self;
11
12 fn to_f64(self) -> f64;
13
14 fn from_f64(num: f64) -> Self;
15}
16
17macro_rules! impl_numeric_float {
18 ($t: ident) => {
19 impl Numeric for $t {
20 const INTEGRAL: bool = false;
21 const MIN: Self = std::$t::MIN;
22 const MAX: Self = std::$t::MAX;
23
24 #[inline(always)]
25 fn to_f64(self) -> f64 {
26 #[allow(trivial_numeric_casts)]
27 {
28 self as f64
29 }
30 }
31
32 #[inline(always)]
33 fn from_f64(num: f64) -> Self {
34 #[allow(trivial_numeric_casts)]
35 {
36 num as Self
37 }
38 }
39 }
40 };
41}
42
43macro_rules! impl_numeric_integer {
44 ($t: ident) => {
45 impl Numeric for $t {
46 const INTEGRAL: bool = true;
47 const MIN: Self = std::$t::MIN;
48 const MAX: Self = std::$t::MAX;
49
50 #[inline(always)]
51 fn to_f64(self) -> f64 {
52 self as f64
53 }
54
55 #[inline(always)]
56 fn from_f64(num: f64) -> Self {
57 num as Self
58 }
59 }
60 };
61}
62
63impl_numeric_float!(f32);
64impl_numeric_float!(f64);
65impl_numeric_integer!(i8);
66impl_numeric_integer!(u8);
67impl_numeric_integer!(i16);
68impl_numeric_integer!(u16);
69impl_numeric_integer!(i32);
70impl_numeric_integer!(u32);
71impl_numeric_integer!(i64);
72impl_numeric_integer!(u64);
73impl_numeric_integer!(isize);
74impl_numeric_integer!(usize);