Skip to content

Commit

Permalink
objectionary#29: improve equality traits for Hex
Browse files Browse the repository at this point in the history
Implemented `Eq` trait, designating that every two `Hex`es can be compared.
Removed a string equality hack from `PartialEq` implementation, removing unnecessary allocations.
  • Loading branch information
UARTman committed Nov 23, 2022
1 parent aa2da93 commit 1633cb2
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ impl Debug for Hex {

impl PartialEq for Hex {
fn eq(&self, other: &Self) -> bool {
self.print() == other.print()
self.bytes() == other.bytes()
}
}

impl Eq for Hex {}

impl Display for Hex {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(self.print().as_str())
Expand Down Expand Up @@ -480,3 +482,14 @@ fn measures_length() -> Result<()> {
assert_eq!(7, d.len());
Ok(())
}

#[test]
fn correct_equality() -> Result<()> {
let d = Hex::from_str("DE-AD-BE-EF")?;
let d1 = Hex::from_str("AA-BB")?;
let d2 = Hex::from_str("DE-AD-BE-EF")?;
assert_eq!(d, d);
assert_ne!(d, d1);
assert_eq!(d, d2);
Ok(())
}

0 comments on commit 1633cb2

Please sign in to comment.