Skip to content

Commit

Permalink
implement more built-in traits
Browse files Browse the repository at this point in the history
  • Loading branch information
magiclen committed Dec 17, 2023
1 parent 45d00cd commit e22ff8b
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "byte-unit"
version = "5.1.0"
version = "5.1.1"
authors = ["Magic Len <[email protected]>"]
edition = "2021"
rust-version = "1.69"
Expand Down
114 changes: 113 additions & 1 deletion src/bit/built_in_traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::str::FromStr;
use core::{cmp::Ordering, str::FromStr};

use super::Bit;
use crate::{ExceededBoundsError, ParseError, TryFromIntError};
Expand Down Expand Up @@ -201,3 +201,115 @@ impl FromStr for Bit {
Bit::parse_str(s)
}
}

impl PartialEq<u64> for Bit {
#[cfg(feature = "u128")]
#[inline]
fn eq(&self, other: &u64) -> bool {
self.0 == *other as u128
}

#[cfg(not(feature = "u128"))]
#[inline]
fn eq(&self, other: &u64) -> bool {
self.0 == *other
}
}

impl PartialEq<u128> for Bit {
#[cfg(feature = "u128")]
#[inline]
fn eq(&self, other: &u128) -> bool {
self.0 == *other
}

#[cfg(not(feature = "u128"))]
#[inline]
fn eq(&self, other: &u128) -> bool {
self.0 as u128 == *other
}
}

impl PartialEq<Bit> for u64 {
#[cfg(feature = "u128")]
#[inline]
fn eq(&self, other: &Bit) -> bool {
*self as u128 == other.0
}

#[cfg(not(feature = "u128"))]
#[inline]
fn eq(&self, other: &Bit) -> bool {
*self == other.0
}
}

impl PartialEq<Bit> for u128 {
#[cfg(feature = "u128")]
#[inline]
fn eq(&self, other: &Bit) -> bool {
*self == other.0
}

#[cfg(not(feature = "u128"))]
#[inline]
fn eq(&self, other: &Bit) -> bool {
*self == other.0 as u128
}
}

impl PartialOrd<u64> for Bit {
#[cfg(feature = "u128")]
#[inline]
fn partial_cmp(&self, other: &u64) -> Option<Ordering> {
self.0.partial_cmp(&(*other as u128))
}

#[cfg(not(feature = "u128"))]
#[inline]
fn partial_cmp(&self, other: &u64) -> Option<Ordering> {
self.0.partial_cmp(other)
}
}

impl PartialOrd<u128> for Bit {
#[cfg(feature = "u128")]
#[inline]
fn partial_cmp(&self, other: &u128) -> Option<Ordering> {
self.0.partial_cmp(other)
}

#[cfg(not(feature = "u128"))]
#[inline]
fn partial_cmp(&self, other: &u128) -> Option<Ordering> {
(self.0 as u128).partial_cmp(other)
}
}

impl PartialOrd<Bit> for u64 {
#[cfg(feature = "u128")]
#[inline]
fn partial_cmp(&self, other: &Bit) -> Option<Ordering> {
(*self as u128).partial_cmp(&other.0)
}

#[cfg(not(feature = "u128"))]
#[inline]
fn partial_cmp(&self, other: &Bit) -> Option<Ordering> {
self.partial_cmp(&other.0)
}
}

impl PartialOrd<Bit> for u128 {
#[cfg(feature = "u128")]
#[inline]
fn partial_cmp(&self, other: &Bit) -> Option<Ordering> {
self.partial_cmp(&other.0)
}

#[cfg(not(feature = "u128"))]
#[inline]
fn partial_cmp(&self, other: &Bit) -> Option<Ordering> {
self.partial_cmp(&(other.0 as u128))
}
}
114 changes: 113 additions & 1 deletion src/byte/built_in_traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::str::FromStr;
use core::{cmp::Ordering, str::FromStr};

use super::Byte;
use crate::{ExceededBoundsError, ParseError, TryFromIntError};
Expand Down Expand Up @@ -202,3 +202,115 @@ impl FromStr for Byte {
Byte::parse_str(s, false)
}
}

impl PartialEq<u64> for Byte {
#[cfg(feature = "u128")]
#[inline]
fn eq(&self, other: &u64) -> bool {
self.0 == *other as u128
}

#[cfg(not(feature = "u128"))]
#[inline]
fn eq(&self, other: &u64) -> bool {
self.0 == *other
}
}

impl PartialEq<u128> for Byte {
#[cfg(feature = "u128")]
#[inline]
fn eq(&self, other: &u128) -> bool {
self.0 == *other
}

#[cfg(not(feature = "u128"))]
#[inline]
fn eq(&self, other: &u128) -> bool {
self.0 as u128 == *other
}
}

impl PartialEq<Byte> for u64 {
#[cfg(feature = "u128")]
#[inline]
fn eq(&self, other: &Byte) -> bool {
*self as u128 == other.0
}

#[cfg(not(feature = "u128"))]
#[inline]
fn eq(&self, other: &Byte) -> bool {
*self == other.0
}
}

impl PartialEq<Byte> for u128 {
#[cfg(feature = "u128")]
#[inline]
fn eq(&self, other: &Byte) -> bool {
*self == other.0
}

#[cfg(not(feature = "u128"))]
#[inline]
fn eq(&self, other: &Byte) -> bool {
*self == other.0 as u128
}
}

impl PartialOrd<u64> for Byte {
#[cfg(feature = "u128")]
#[inline]
fn partial_cmp(&self, other: &u64) -> Option<Ordering> {
self.0.partial_cmp(&(*other as u128))
}

#[cfg(not(feature = "u128"))]
#[inline]
fn partial_cmp(&self, other: &u64) -> Option<Ordering> {
self.0.partial_cmp(other)
}
}

impl PartialOrd<u128> for Byte {
#[cfg(feature = "u128")]
#[inline]
fn partial_cmp(&self, other: &u128) -> Option<Ordering> {
self.0.partial_cmp(other)
}

#[cfg(not(feature = "u128"))]
#[inline]
fn partial_cmp(&self, other: &u128) -> Option<Ordering> {
(self.0 as u128).partial_cmp(other)
}
}

impl PartialOrd<Byte> for u64 {
#[cfg(feature = "u128")]
#[inline]
fn partial_cmp(&self, other: &Byte) -> Option<Ordering> {
(*self as u128).partial_cmp(&other.0)
}

#[cfg(not(feature = "u128"))]
#[inline]
fn partial_cmp(&self, other: &Byte) -> Option<Ordering> {
self.partial_cmp(&other.0)
}
}

impl PartialOrd<Byte> for u128 {
#[cfg(feature = "u128")]
#[inline]
fn partial_cmp(&self, other: &Byte) -> Option<Ordering> {
self.partial_cmp(&other.0)
}

#[cfg(not(feature = "u128"))]
#[inline]
fn partial_cmp(&self, other: &Byte) -> Option<Ordering> {
self.partial_cmp(&(other.0 as u128))
}
}
2 changes: 1 addition & 1 deletion tests/bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,6 @@ fn tests() {
let bit = Bit::from_f64_with_unit(case.1 .0, case.1 .1).unwrap();

assert_eq!(case.0, serde_json::to_string(&bit).unwrap(), "{i}");
assert_eq!(bit, serde_json::from_str(case.0).unwrap(), "{i}");
assert_eq!(bit, serde_json::from_str::<Bit>(case.0).unwrap(), "{i}");
}
}
2 changes: 1 addition & 1 deletion tests/byte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,6 @@ fn tests() {
let byte = Byte::from_f64_with_unit(case.1 .0, case.1 .1).unwrap();

assert_eq!(case.0, serde_json::to_string(&byte).unwrap(), "{i}");
assert_eq!(byte, serde_json::from_str(case.0).unwrap(), "{i}");
assert_eq!(byte, serde_json::from_str::<Byte>(case.0).unwrap(), "{i}");
}
}

0 comments on commit e22ff8b

Please sign in to comment.