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
use either::Either;
use gazebo::prelude::*;
use crate::{
collections::SmallHashResult,
values::{types::float::StarlarkFloat, UnpackValue, Value},
};
#[derive(Clone, Copy, Dupe, Debug)]
pub enum Num {
Int(i32),
Float(f64),
}
type UnpackNumImpl = Either<i32, StarlarkFloat>;
impl<'v> UnpackValue<'v> for Num {
fn expected() -> String {
UnpackNumImpl::expected()
}
fn unpack_value(value: Value<'v>) -> Option<Self> {
Some(match UnpackNumImpl::unpack_value(value)? {
Either::Left(i) => Num::Int(i),
Either::Right(f) => Num::Float(f.0),
})
}
}
impl Num {
pub fn as_float(self) -> f64 {
match self {
Self::Int(i) => i as f64,
Self::Float(f) => f,
}
}
pub fn as_int(self) -> Option<i32> {
match self {
Self::Int(i) => Some(i),
Self::Float(f) => {
let int_candidate = f as i32;
if f == int_candidate as f64 {
Some(int_candidate)
} else {
None
}
}
}
}
pub fn get_hash(self) -> u64 {
match (self.as_int(), self) {
(Some(i), _) => i as u64,
(None, Self::Float(f)) => {
if f.is_nan() {
0
} else if f.is_infinite() {
u64::MAX
} else if f == 0.0 {
0.0f64.to_bits()
} else {
f.to_bits()
}
}
(None, Self::Int(i)) => {
i as u64
}
}
}
pub(crate) fn get_small_hash_result(self) -> SmallHashResult {
SmallHashResult::hash_64(self.get_hash())
}
}
impl From<i32> for Num {
fn from(i: i32) -> Self {
Self::Int(i)
}
}
impl From<f64> for Num {
fn from(f: f64) -> Self {
Self::Float(f)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_value() {
assert!(Num::unpack_value(Value::new_bool(true)).is_none());
assert!(Num::unpack_value(Value::new_bool(false)).is_none());
assert!(Num::unpack_value(Value::new_empty_string()).is_none());
assert!(Num::unpack_value(Value::new_none()).is_none());
assert_eq!(
Num::unpack_value(Value::new_int(0)).unwrap().as_int(),
Some(0)
);
assert_eq!(
Num::unpack_value(Value::new_int(42)).unwrap().as_int(),
Some(42)
);
assert_eq!(
Num::unpack_value(Value::new_int(-42)).unwrap().as_int(),
Some(-42)
);
}
#[test]
fn test_converstion_to_float() {
assert_eq!(Num::Int(0).as_float(), 0.0);
assert_eq!(Num::Int(i32::MAX).as_float(), i32::MAX as f64);
assert_eq!(Num::Int(i32::MIN).as_float(), i32::MIN as f64);
assert_eq!(Num::Float(0.0).as_float(), 0.0);
assert!(Num::Float(f64::NAN).as_float().is_nan());
}
#[test]
fn test_conversion_to_int() {
assert_eq!(Num::Int(0).as_int(), Some(0));
assert_eq!(Num::Int(42).as_int(), Some(42));
assert_eq!(Num::Int(-42).as_int(), Some(-42));
assert_eq!(Num::Float(0_f64).as_int(), Some(0));
assert_eq!(Num::Float(42_f64).as_int(), Some(42));
assert_eq!(Num::Float(-42_f64).as_int(), Some(-42));
assert_eq!(Num::Float(i32::MIN as f64).as_int(), Some(i32::MIN));
assert_eq!(Num::Float(i32::MAX as f64).as_int(), Some(i32::MAX));
assert_eq!(Num::Float(42.75).as_int(), None);
assert_eq!(Num::Float(-42.75).as_int(), None);
assert_eq!(Num::Float(f64::NAN).as_int(), None);
assert_eq!(Num::Float(f64::INFINITY).as_int(), None);
assert_eq!(Num::Float(f64::NEG_INFINITY).as_int(), None);
}
#[test]
fn test_hashing() {
assert_eq!(Num::Int(0).get_hash(), Num::Float(0.0).get_hash());
assert_eq!(Num::Int(42).get_hash(), Num::Float(42.0).get_hash());
assert_eq!(
Num::Float(f64::INFINITY + f64::NEG_INFINITY).get_hash(),
Num::Float(f64::NAN).get_hash()
);
assert_eq!(
Num::Float("0.25".parse().unwrap()).get_hash(),
Num::Float("25e-2".parse().unwrap()).get_hash()
);
}
}