From 01e68e3e7b0b5735f8a85a7b547fe14d50e9be7e Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Mon, 26 Aug 2024 18:31:01 +0200 Subject: fix partial_cmp return value for equal items --- src/gamedata.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/gamedata.rs b/src/gamedata.rs index a956478..a608346 100644 --- a/src/gamedata.rs +++ b/src/gamedata.rs @@ -2,6 +2,7 @@ use num_derive::FromPrimitive; use num_traits::FromPrimitive; use std::{ + cmp::Ordering, fmt::{self, Display, Formatter}, str::FromStr, }; @@ -372,11 +373,17 @@ macro_rules! ordering_group { /// Future versions might extend this order to introduce inter-wing comparisons or a full [`Ord`] /// instance. impl PartialOrd for Encounter { - fn partial_cmp(&self, other: &Self) -> Option { + fn partial_cmp(&self, other: &Self) -> Option { // Keep in mind that this is a partial order, not a weak order! In particular, we cannot // have bosses compare as equal if they are not the same (even if that would be nice for // Wing 5/Wing 7). use Encounter::*; + + // Ensure that a == b <=> a.partial_cmp(b) == Equal holds + if *self == *other { + return Some(Ordering::Equal); + } + ordering_group!(*self, *other; // Raids [ValeGuardian, Gorseval, Sabetha]; @@ -391,6 +398,7 @@ impl PartialOrd for Encounter { // Nightmare CM [MAMA, Siax, Ensolyss]; ); + None } } @@ -1204,6 +1212,10 @@ mod tests { assert!(Qadim > TwinLargos); assert!(Ankka.partial_cmp(&Dragonvoid).is_none()); + + assert_eq!(ValeGuardian.partial_cmp(&ValeGuardian), Some(Ordering::Equal)); + assert_eq!(Slothasor.partial_cmp(&Slothasor), Some(Ordering::Equal)); + assert_eq!(Ai.partial_cmp(&Ai), Some(Ordering::Equal)); } #[test] -- cgit v1.2.3