Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions crates/bevy_reflect/src/enums/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,11 @@ pub fn enum_partial_cmp<TEnum: Enum + ?Sized>(

// Same variant name?
if a.variant_name() != b.variant_name() {
// Different variant names, determining ordering by variant index
return Some(a.variant_index().cmp(&b.variant_index()));
// Different variant names.
// Ordering by variant index here can result in inconsistencies with
// partial_eq when comparing between two different concrete enums,
// so we simply return None here
return None;
}

// Same variant type?
Expand Down
37 changes: 11 additions & 26 deletions crates/bevy_reflect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1677,7 +1677,7 @@ mod tests {
}

#[test]
fn enum_variant_index_ordering() {
fn enum_variant_ordering() {
use core::cmp::Ordering;

#[derive(PartialEq, PartialOrd, Reflect, Debug)]
Expand All @@ -1691,19 +1691,10 @@ mod tests {
let b = MyEnum::Center;
let c = MyEnum::Bottom;

// Variant ordering should follow variant index
assert_eq!(
PartialReflect::reflect_partial_cmp(&a, &b),
Some(Ordering::Less)
);
assert_eq!(
PartialReflect::reflect_partial_cmp(&b, &a),
Some(Ordering::Greater)
);
assert_eq!(
PartialReflect::reflect_partial_cmp(&b, &c),
Some(Ordering::Less)
);
// Variant ordering of different variant name cannot be compared.
assert_eq!(PartialReflect::reflect_partial_cmp(&a, &b), None);
assert_eq!(PartialReflect::reflect_partial_cmp(&b, &a), None);
assert_eq!(PartialReflect::reflect_partial_cmp(&b, &c), None);
assert_eq!(
PartialReflect::reflect_partial_cmp(&a, &a),
Some(Ordering::Equal)
Expand All @@ -1713,23 +1704,17 @@ mod tests {
enum MyEnum2 {
A,
B,
C,
Center,
}
let a1 = MyEnum2::A;
let c1 = MyEnum2::C;
let c1 = MyEnum2::Center;

// Unfortunately, it means that enums that have different types can also be compared
assert_eq!(
PartialReflect::reflect_partial_cmp(&a1, &a),
Some(Ordering::Equal)
);
assert_eq!(
PartialReflect::reflect_partial_cmp(&a1, &b),
Some(Ordering::Less)
);
assert_eq!(PartialReflect::reflect_partial_cmp(&a1, &a), None);
assert_eq!(PartialReflect::reflect_partial_cmp(&a1, &b), None);
// Two enums with the same variant name across different types are currently comparable
assert_eq!(
PartialReflect::reflect_partial_cmp(&c1, &b),
Some(Ordering::Greater)
Some(Ordering::Equal)
);
}

Expand Down