rafx_base/
decimal.rs

1use std::hash::Hasher;
2
3// This is an f32 that supports Hash and Eq. Generally this is dangerous, but here we're
4// not doing any sort of fp-arithmetic and not expecting NaN. We should be deterministically
5// parsing a string and creating a float from it.
6#[derive(Debug, Copy, Clone, Default)]
7pub struct DecimalF32(pub f32);
8
9impl Into<f32> for DecimalF32 {
10    fn into(self) -> f32 {
11        self.0
12    }
13}
14
15impl Into<i32> for DecimalF32 {
16    fn into(self) -> i32 {
17        self.0 as i32
18    }
19}
20
21impl Into<u32> for DecimalF32 {
22    fn into(self) -> u32 {
23        self.0 as u32
24    }
25}
26
27impl PartialEq for DecimalF32 {
28    fn eq(
29        &self,
30        other: &Self,
31    ) -> bool {
32        self.0 == other.0
33    }
34}
35
36impl Eq for DecimalF32 {}
37
38impl std::hash::Hash for DecimalF32 {
39    fn hash<H: Hasher>(
40        &self,
41        state: &mut H,
42    ) {
43        let bits: u32 = self.0.to_bits();
44        bits.hash(state);
45    }
46}
47
48// This is an f64 that supports Hash and Eq. Generally this is dangerous, but here we're
49// not doing any sort of fp-arithmetic and not expecting NaN. We should be deterministically
50// parsing a string and creating a float from it.
51#[derive(Debug, Copy, Clone, Default)]
52pub struct DecimalF64(pub f64);
53
54impl Into<f64> for DecimalF64 {
55    fn into(self) -> f64 {
56        self.0
57    }
58}
59
60impl Into<f32> for DecimalF64 {
61    fn into(self) -> f32 {
62        self.0 as f32
63    }
64}
65
66impl Into<i32> for DecimalF64 {
67    fn into(self) -> i32 {
68        self.0 as i32
69    }
70}
71
72impl Into<u32> for DecimalF64 {
73    fn into(self) -> u32 {
74        self.0 as u32
75    }
76}
77
78impl PartialEq for DecimalF64 {
79    fn eq(
80        &self,
81        other: &Self,
82    ) -> bool {
83        self.0 == other.0
84    }
85}
86
87impl Eq for DecimalF64 {}
88
89impl std::hash::Hash for DecimalF64 {
90    fn hash<H: Hasher>(
91        &self,
92        state: &mut H,
93    ) {
94        let bits: u64 = self.0.to_bits();
95        bits.hash(state);
96    }
97}