Skip to content

Commit

Permalink
Derive PartialEq, Eq, PartialOrd, Ord, Hash
Browse files Browse the repository at this point in the history
The derived implementations work now that the interior value is always
fully masked[1].

As a side effect, this makes consts eligible for pattern matching.

[1]: rust-ux#65

Closes: rust-ux#66
  • Loading branch information
chrysn committed May 30, 2024
1 parent ab2f367 commit c1f82d9
Showing 1 changed file with 2 additions and 32 deletions.
34 changes: 2 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

0 comments on commit c1f82d9

Please sign in to comment.