Skip to content

Commit

Permalink
PartialOrd / Reuse Ord implementation where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
dnsl48 committed Oct 14, 2023
1 parent 4ebedcf commit 89bcf06
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
11 changes: 10 additions & 1 deletion src/decimal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,18 @@ where
}
}

dec_impl!(impl_trait_cmp; PartialOrd; partial_cmp; Option<Ordering>);
dec_impl!(impl_trait_cmp; Ord; cmp; Ordering);

impl<T, P> PartialOrd for GenericDecimal<T, P>
where
T: Clone + GenericInteger + PartialOrd,
P: Copy + GenericInteger + Into<usize>
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl<T, P> PartialEq for GenericDecimal<T, P>
where
T: Clone + GenericInteger + PartialEq,
Expand Down
11 changes: 10 additions & 1 deletion src/dynaint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,6 @@ macro_rules! dyna_impl {
}

dyna_impl! (impl_trait_birefs_customret; PartialEq, eq, bool);
dyna_impl! (impl_trait_birefs_customret; PartialOrd, partial_cmp, Option<Ordering>);
dyna_impl! (impl_trait_birefs_customret; Ord, cmp, Ordering);

dyna_impl! (impl_trait_math_unary; Neg, neg);
Expand Down Expand Up @@ -785,6 +784,16 @@ dyna_impl! (impl_trait_math_checked; CheckedSub, checked_sub);

dyna_impl!(impl_trait_from_uint; u16, u32, u64, u128, usize);

impl<T, G> PartialOrd for DynaInt<T, G>
where
T: Copy + GenericInteger + Into<G> + TryToConvertFrom<G> + From<u8> + PartialOrd,
G: Clone + GenericInteger + PartialOrd
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl<T, G> Eq for DynaInt<T, G>
where
T: Copy + GenericInteger + Into<G> + TryToConvertFrom<G> + From<u8> + Eq,
Expand Down
7 changes: 1 addition & 6 deletions src/fraction/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ impl Neg for Sign {

impl PartialOrd for Sign {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match (self, other) {
(Sign::Minus, Sign::Minus) => Some(Ordering::Equal),
(Sign::Plus, Sign::Minus) => Some(Ordering::Greater),
(Sign::Minus, Sign::Plus) => Some(Ordering::Less),
(Sign::Plus, Sign::Plus) => Some(Ordering::Equal),
}
Some(self.cmp(other))
}
}

Expand Down

0 comments on commit 89bcf06

Please sign in to comment.