From 3b09ac4e737f6d6723fb5a72a14a3b2c47769d0a Mon Sep 17 00:00:00 2001 From: Kotauskas Date: Sun, 5 Apr 2020 16:09:06 +0300 Subject: [PATCH] Added LBNumRef comparison operators --- src/ops/linkedbytes/mod.rs | 81 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 4 deletions(-) diff --git a/src/ops/linkedbytes/mod.rs b/src/ops/linkedbytes/mod.rs index 57f4387..9fa8b4c 100644 --- a/src/ops/linkedbytes/mod.rs +++ b/src/ops/linkedbytes/mod.rs @@ -1,4 +1,4 @@ -use crate::{LBNum}; +use crate::{LBNum, LBNumRef}; use core::cmp::{self, Ordering}; mod add; mod sub; mod mul; mod div; mod from; mod tryinto; mod fmt; @@ -19,9 +19,28 @@ impl cmp::PartialOrd for LBNum { } } impl cmp::Ord for LBNum { + #[inline(always)] + fn cmp(&self, rhs: &Self) -> Ordering { + LBNumRef::from(self).cmp(&LBNumRef::from(rhs)) + } +} +impl PartialEq for LBNumRef<'_> { + #[inline(always)] + fn eq(&self, rhs: &Self) -> bool { + self.cmp(rhs) == Ordering::Equal + } +} +impl Eq for LBNumRef<'_> {} +impl cmp::PartialOrd for LBNumRef<'_> { + #[inline(always)] + fn partial_cmp(&self, rhs: &Self) -> Option { + Some(self.cmp(rhs)) + } +} +impl<'a> cmp::Ord for LBNumRef<'a> { #[inline] fn cmp(&self, rhs: &Self) -> Ordering { - match self.0.len().cmp(&rhs.0.len()) { + match self.inner().len().cmp(&rhs.inner().len()) { Ordering::Greater => Ordering::Greater, Ordering::Less => Ordering::Less, Ordering::Equal => { @@ -37,19 +56,55 @@ impl cmp::Ord for LBNum { } } } +impl PartialEq> for LBNum { + #[inline(always)] + fn eq(&self, rhs: &LBNumRef<'_>) -> bool { + LBNumRef::from(self).cmp(rhs) == Ordering::Equal + } +} +impl PartialOrd> for LBNum { + #[inline(always)] + fn partial_cmp(&self, rhs: &LBNumRef<'_>) -> Option { + Some(LBNumRef::from(self).cmp(rhs)) + } +} +impl PartialEq for LBNumRef<'_> { + #[inline(always)] + fn eq(&self, rhs: &LBNum) -> bool { + self.cmp(&LBNumRef::from(rhs)) == Ordering::Equal + } +} +impl PartialOrd for LBNumRef<'_> { + #[inline(always)] + fn partial_cmp(&self, rhs: &LBNum) -> Option { + Some(self.cmp(&LBNumRef::from(rhs))) + } +} macro_rules! impl_partial_eq_ord_to_primitive { ($ty:ident) => { impl PartialEq<$ty> for LBNum { #[inline(always)] fn eq(&self, rhs: &$ty) -> bool { - self == &Self::from(*rhs) + *self == Self::from(*rhs) + } + } + impl PartialEq<$ty> for LBNumRef<'_> { + #[inline(always)] + fn eq(&self, rhs:&$ty) -> bool { + *self == LBNum::from(*rhs) } } impl PartialEq for $ty { #[inline(always)] fn eq(&self, rhs: &LBNum) -> bool { - &LBNum::from(*self) == rhs + LBNum::from(*self) == *rhs + } + } + impl PartialEq> for $ty { + #[inline(always)] + fn eq(&self, rhs: &LBNumRef<'_>) -> bool { + LBNum::from(*self) == *rhs } } impl PartialOrd<$ty> for LBNum { @@ -61,6 +116,15 @@ macro_rules! impl_partial_eq_ord_to_primitive { Some(self.cmp(&Self::from(*rhs))) } } + impl PartialOrd<$ty> for LBNumRef<'_> { + /// Compares `self` and `rhs`. + /// + /// Never fails, a return value of `Some` can be relied upon. + #[inline(always)] + fn partial_cmp(&self, rhs: &$ty) -> Option { + self.partial_cmp(&LBNum::from(*rhs)) // Why doesn't Ord have a type parameter? + } + } impl PartialOrd for $ty { /// Compares `self` and `rhs`. /// @@ -70,6 +134,15 @@ macro_rules! impl_partial_eq_ord_to_primitive { Some(LBNum::from(*self).cmp(rhs)) } } + impl PartialOrd> for $ty { + /// Compares `self` and `rhs`. + /// + /// Never fails, a return value of `Some` can be relied upon. + #[inline(always)] + fn partial_cmp(&self, rhs: &LBNumRef<'_>) -> Option { + LBNum::from(*self).partial_cmp(rhs) + } + } }; }