Skip to content

Commit

Permalink
Derive PartialEq, Eq, PartialOrd, Ord, Hash
Browse files Browse the repository at this point in the history
Merges: #68
  • Loading branch information
chrysn committed May 30, 2024
2 parents ab2f367 + 81871d0 commit 9df9b3a
Showing 1 changed file with 15 additions and 32 deletions.
47 changes: 15 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ use lib::core::ops::{
ShrAssign,
};

use lib::core::hash::{Hash, Hasher};

use lib::core::cmp::{Ord, Ordering, PartialOrd};

use lib::core::fmt::{Binary, Display, Formatter, LowerHex, Octal, UpperHex};

macro_rules! define_unsigned {
Expand All @@ -37,7 +33,7 @@ macro_rules! define_unsigned {

#[$doc]
#[allow(non_camel_case_types)]
#[derive(Default, Clone, Copy, Debug)]
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $name($type);

impl $name {
Expand All @@ -61,7 +57,7 @@ macro_rules! define_signed {

#[$doc]
#[allow(non_camel_case_types)]
#[derive(Default, Clone, Copy, Debug)]
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $name($type);

#[$doc]
Expand Down Expand Up @@ -159,32 +155,6 @@ macro_rules! implement_common {
}
}

impl PartialEq for $name {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}

impl Eq for $name {}

impl PartialOrd for $name {
fn partial_cmp(&self, other: &$name) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for $name {
fn cmp(&self, other: &$name) -> Ordering {
self.0.cmp(&other.0)
}
}

impl Hash for $name {
fn hash<H: Hasher>(&self, h: &mut H) {
self.0.hash(h)
}
}

// Implement formating functions
impl Display for $name {
fn fmt(&self, f: &mut Formatter) -> Result<(), lib::core::fmt::Error> {
Expand Down Expand Up @@ -983,4 +953,17 @@ mod tests {
assert_eq!(!u7(0), u7(0x7F));
assert_eq!(!u7(56), u7(71));
}

#[test]
fn test_match() {
const SEVEN: u7 = u7::new(7);
match u7(7) {
SEVEN => (),
_ => panic!("Pattern matching failed (7 != 7?)"),
}
match u7(42) {
SEVEN => panic!("Pattern matching failed (7 == 42?)"),
_ => (),
}
}
}

0 comments on commit 9df9b3a

Please sign in to comment.