Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive PartialEq, Eq, PartialOrd, Ord, Hash #68

Merged
merged 2 commits into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?)"),
_ => (),
}
}
}
Loading