Skip to content

Commit c9bff92

Browse files
committed
Fix PartialEq between Value and f32
Caught by test_partialeq_number: thread 'test_partialeq_number' panicked at 'assertion failed: `(left == right)` left: `-3.4028235e38`, right: `Number(-3.4028235e38)`', tests/test.rs:2033:5
1 parent 06f3443 commit c9bff92

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/number.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,17 @@ impl Number {
279279
}
280280
}
281281

282+
pub(crate) fn as_f32(&self) -> Option<f32> {
283+
#[cfg(not(feature = "arbitrary_precision"))]
284+
match self.n {
285+
N::PosInt(n) => Some(n as f32),
286+
N::NegInt(n) => Some(n as f32),
287+
N::Float(n) => Some(n as f32),
288+
}
289+
#[cfg(feature = "arbitrary_precision")]
290+
self.n.parse::<f32>().ok().filter(|float| float.is_finite())
291+
}
292+
282293
pub(crate) fn from_f32(f: f32) -> Option<Number> {
283294
if f.is_finite() {
284295
let n = {

src/value/partial_eq.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ fn eq_u64(value: &Value, other: u64) -> bool {
99
value.as_u64().map_or(false, |i| i == other)
1010
}
1111

12+
fn eq_f32(value: &Value, other: f32) -> bool {
13+
match value {
14+
Value::Number(n) => n.as_f32().map_or(false, |i| i == other),
15+
_ => false,
16+
}
17+
}
18+
1219
fn eq_f64(value: &Value, other: f64) -> bool {
1320
value.as_f64().map_or(false, |i| i == other)
1421
}
@@ -90,6 +97,7 @@ macro_rules! partialeq_numeric {
9097
partialeq_numeric! {
9198
eq_i64[i8 i16 i32 i64 isize]
9299
eq_u64[u8 u16 u32 u64 usize]
93-
eq_f64[f32 f64]
100+
eq_f32[f32]
101+
eq_f64[f64]
94102
eq_bool[bool]
95103
}

0 commit comments

Comments
 (0)