From 260ed0fd5c38ec09f815e8da74bcab78a97380c7 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 13 Sep 2024 14:00:13 -0700 Subject: [PATCH] ratio: PartialEq, Eq, PartialOrd, Ord --- ratio_ops/src/ratio.rs | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/ratio_ops/src/ratio.rs b/ratio_ops/src/ratio.rs index fdb5f39..a7f8928 100644 --- a/ratio_ops/src/ratio.rs +++ b/ratio_ops/src/ratio.rs @@ -175,3 +175,48 @@ macro_rules! impl_ratio_assign_operator { impl_ratio_assign_operator!(AddAssign,add_assign); impl_ratio_assign_operator!(SubAssign,sub_assign); impl_ratio_assign_operator!(RemAssign,rem_assign); + +// Only implement PartialEq +// Rust's operators aren't actually that good + +impl PartialEq for Ratio + where + Num:Copy, + Den:Copy, + Num:core::ops::Mul, + T:PartialEq, +{ + #[inline] + fn eq(&self,&other:&Self)->bool{ + (self.num*other.den).eq(&(other.num*self.den)) + } +} +impl Eq for Ratio + where + Ratio:PartialEq, +{} + +impl PartialOrd for Ratio + where + Num:Copy, + Den:Copy, + Num:core::ops::Mul, + T:PartialOrd, +{ + #[inline] + fn partial_cmp(&self,&other:&Self)->Option{ + (self.num*other.den).partial_cmp(&(other.num*self.den)) + } +} +impl Ord for Ratio + where + Num:Copy, + Den:Copy, + Num:core::ops::Mul, + T:Ord, +{ + #[inline] + fn cmp(&self,other:&Self)->std::cmp::Ordering{ + (self.num*other.den).cmp(&(other.num*self.den)) + } +}