From f6bf2b6abdabdf8688132101402323a828359aad Mon Sep 17 00:00:00 2001 From: Hsingai Altaica Date: Thu, 1 Dec 2022 16:43:49 -0800 Subject: [PATCH 1/3] added Ord trait to GenericFraction --- src/fraction/generic_fraction.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/fraction/generic_fraction.rs b/src/fraction/generic_fraction.rs index 768b9017..8ea8d281 100644 --- a/src/fraction/generic_fraction.rs +++ b/src/fraction/generic_fraction.rs @@ -516,6 +516,26 @@ impl PartialOrd for GenericFraction { } } +impl Ord for GenericFraction { + fn cmp(&self, other: &Self) -> Ordering { + match self { + GenericFraction::NaN => { + match other { + GenericFraction::NaN => Ordering::Equal, + _ => Ordering::Less, + } + }, + _ => { + match other { + GenericFraction::NaN => Ordering::Greater, + _ => self.partial_cmp(other).unwrap(), + } + } + } + } +} + + impl Neg for GenericFraction { type Output = GenericFraction; From f69af0684df637c83c199394c8c60f6e8e8d9233 Mon Sep 17 00:00:00 2001 From: Hsingai Altaica Date: Thu, 1 Dec 2022 17:26:00 -0800 Subject: [PATCH 2/3] rewrote GenericFraction::cmp() to be eaiser to read --- src/fraction/generic_fraction.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/fraction/generic_fraction.rs b/src/fraction/generic_fraction.rs index 8ea8d281..33841ff1 100644 --- a/src/fraction/generic_fraction.rs +++ b/src/fraction/generic_fraction.rs @@ -518,18 +518,17 @@ impl PartialOrd for GenericFraction { impl Ord for GenericFraction { fn cmp(&self, other: &Self) -> Ordering { - match self { - GenericFraction::NaN => { - match other { - GenericFraction::NaN => Ordering::Equal, - _ => Ordering::Less, - } - }, - _ => { - match other { - GenericFraction::NaN => Ordering::Greater, - _ => self.partial_cmp(other).unwrap(), - } + if *self == GenericFraction::NaN { + if *other == GenericFraction::NaN { + Ordering::Equal + } else { + Ordering::Less + } + } else { + if *other == GenericFraction::NaN { + Ordering::Greater + } else { + self.partial_cmp(other).unwrap() } } } From 3b0d8d7bd0d8021fe7689c5b0af992e7e23b1f16 Mon Sep 17 00:00:00 2001 From: Hsingai Altaica Date: Thu, 1 Dec 2022 17:38:56 -0800 Subject: [PATCH 3/3] replaces the .unwrap() on GenericFraction::cmp() with .expect() --- src/fraction/generic_fraction.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fraction/generic_fraction.rs b/src/fraction/generic_fraction.rs index 33841ff1..98cd8f5f 100644 --- a/src/fraction/generic_fraction.rs +++ b/src/fraction/generic_fraction.rs @@ -528,7 +528,7 @@ impl Ord for GenericFraction { if *other == GenericFraction::NaN { Ordering::Greater } else { - self.partial_cmp(other).unwrap() + self.partial_cmp(other).expect("Well when I wrote this the only way partial_cmp() would return None was if one of the argument was NaN, which they weren't in this case.") } } }