Skip to content

Commit

Permalink
bevy_reflect: Added PartialEq to reflected f32 & f64 (bevyengin…
Browse files Browse the repository at this point in the history
…e#4217)

# Objective

Comparing two reflected floating points would always fail:

```rust
let a: &dyn Reflect = &1.23_f32;
let b: &dyn Reflect = &1.23_f32;

// Panics:
assert!(a.reflect_partial_eq(b).unwrap_or_default());
```

The comparison returns `None` since `f32` (and `f64`) does not have a reflected `PartialEq` implementation.

## Solution

Include `PartialEq` in the `impl_reflect_value!` macro call for both `f32` and `f64`.

`Hash` is still excluded since neither implement `Hash`.

Also added equality tests for some of the common types from `std` (including `f32`).
  • Loading branch information
MrGVSV authored and exjam committed May 22, 2022
1 parent 6ba61e0 commit fe43eb2
Showing 1 changed file with 63 additions and 2 deletions.
65 changes: 63 additions & 2 deletions crates/bevy_reflect/src/impls/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ impl_reflect_value!(i32(Hash, PartialEq, Serialize, Deserialize));
impl_reflect_value!(i64(Hash, PartialEq, Serialize, Deserialize));
impl_reflect_value!(i128(Hash, PartialEq, Serialize, Deserialize));
impl_reflect_value!(isize(Hash, PartialEq, Serialize, Deserialize));
impl_reflect_value!(f32(Serialize, Deserialize));
impl_reflect_value!(f64(Serialize, Deserialize));
impl_reflect_value!(f32(PartialEq, Serialize, Deserialize));
impl_reflect_value!(f64(PartialEq, Serialize, Deserialize));
impl_reflect_value!(String(Hash, PartialEq, Serialize, Deserialize));
impl_reflect_value!(Option<T: Serialize + Clone + for<'de> Deserialize<'de> + Reflect + 'static>(Serialize, Deserialize));
impl_reflect_value!(HashSet<T: Serialize + Hash + Eq + Clone + for<'de> Deserialize<'de> + Send + Sync + 'static>(Serialize, Deserialize));
Expand Down Expand Up @@ -392,9 +392,70 @@ impl FromReflect for Cow<'static, str> {
#[cfg(test)]
mod tests {
use crate::Reflect;
use bevy_utils::HashMap;
use std::f32::consts::{PI, TAU};

#[test]
fn can_serialize_duration() {
assert!(std::time::Duration::ZERO.serializable().is_some());
}

#[test]
fn should_partial_eq_i32() {
let a: &dyn Reflect = &123_i32;
let b: &dyn Reflect = &123_i32;
let c: &dyn Reflect = &321_i32;
assert!(a.reflect_partial_eq(b).unwrap_or_default());
assert!(!a.reflect_partial_eq(c).unwrap_or_default());
}

#[test]
fn should_partial_eq_f32() {
let a: &dyn Reflect = &PI;
let b: &dyn Reflect = &PI;
let c: &dyn Reflect = &TAU;
assert!(a.reflect_partial_eq(b).unwrap_or_default());
assert!(!a.reflect_partial_eq(c).unwrap_or_default());
}

#[test]
fn should_partial_eq_string() {
let a: &dyn Reflect = &String::from("Hello");
let b: &dyn Reflect = &String::from("Hello");
let c: &dyn Reflect = &String::from("World");
assert!(a.reflect_partial_eq(b).unwrap_or_default());
assert!(!a.reflect_partial_eq(c).unwrap_or_default());
}

#[test]
fn should_partial_eq_vec() {
let a: &dyn Reflect = &vec![1, 2, 3];
let b: &dyn Reflect = &vec![1, 2, 3];
let c: &dyn Reflect = &vec![3, 2, 1];
assert!(a.reflect_partial_eq(b).unwrap_or_default());
assert!(!a.reflect_partial_eq(c).unwrap_or_default());
}

#[test]
fn should_partial_eq_hash_map() {
let mut a = HashMap::new();
a.insert(0usize, 1.23_f64);
let b = a.clone();
let mut c = HashMap::new();
c.insert(0usize, 3.21_f64);

let a: &dyn Reflect = &a;
let b: &dyn Reflect = &b;
let c: &dyn Reflect = &c;
assert!(a.reflect_partial_eq(b).unwrap_or_default());
assert!(!a.reflect_partial_eq(c).unwrap_or_default());
}

#[test]
fn should_not_partial_eq_option() {
// Option<T> does not contain a `PartialEq` implementation, so it should return `None`
let a: &dyn Reflect = &Some(123);
let b: &dyn Reflect = &Some(123);
assert_eq!(None, a.reflect_partial_eq(b));
}
}

0 comments on commit fe43eb2

Please sign in to comment.