Skip to content

Commit

Permalink
Added LBNumRef comparison operators
Browse files Browse the repository at this point in the history
  • Loading branch information
kotauskas committed Apr 5, 2020
1 parent 188ef21 commit 3b09ac4
Showing 1 changed file with 77 additions and 4 deletions.
81 changes: 77 additions & 4 deletions src/ops/linkedbytes/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Ordering> {
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 => {
Expand All @@ -37,19 +56,55 @@ impl cmp::Ord for LBNum {
}
}
}
impl PartialEq<LBNumRef<'_>> for LBNum {
#[inline(always)]
fn eq(&self, rhs: &LBNumRef<'_>) -> bool {
LBNumRef::from(self).cmp(rhs) == Ordering::Equal
}
}
impl PartialOrd<LBNumRef<'_>> for LBNum {
#[inline(always)]
fn partial_cmp(&self, rhs: &LBNumRef<'_>) -> Option<Ordering> {
Some(LBNumRef::from(self).cmp(rhs))
}
}
impl PartialEq<LBNum> for LBNumRef<'_> {
#[inline(always)]
fn eq(&self, rhs: &LBNum) -> bool {
self.cmp(&LBNumRef::from(rhs)) == Ordering::Equal
}
}
impl PartialOrd<LBNum> for LBNumRef<'_> {
#[inline(always)]
fn partial_cmp(&self, rhs: &LBNum) -> Option<Ordering> {
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<LBNum> for $ty {
#[inline(always)]
fn eq(&self, rhs: &LBNum) -> bool {
&LBNum::from(*self) == rhs
LBNum::from(*self) == *rhs
}
}
impl PartialEq<LBNumRef<'_>> for $ty {
#[inline(always)]
fn eq(&self, rhs: &LBNumRef<'_>) -> bool {
LBNum::from(*self) == *rhs
}
}
impl PartialOrd<$ty> for LBNum {
Expand All @@ -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<Ordering> {
self.partial_cmp(&LBNum::from(*rhs)) // Why doesn't Ord have a type parameter?
}
}
impl PartialOrd<LBNum> for $ty {
/// Compares `self` and `rhs`.
///
Expand All @@ -70,6 +134,15 @@ macro_rules! impl_partial_eq_ord_to_primitive {
Some(LBNum::from(*self).cmp(rhs))
}
}
impl PartialOrd<LBNumRef<'_>> 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<Ordering> {
LBNum::from(*self).partial_cmp(rhs)
}
}
};
}

Expand Down

0 comments on commit 3b09ac4

Please sign in to comment.