Skip to content

Commit 5d57323

Browse files
committed
Add PartialEq<u8> and PartialOrd for Score
This allows for comparisons between `Score` and `u8`. The reverse implementations are also added.
1 parent 7af59fd commit 5d57323

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/scoring.rs

+57
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ impl Display for Score {
4848
}
4949
}
5050

51+
impl PartialEq<u8> for Score {
52+
fn eq(&self, other: &u8) -> bool {
53+
*self as u8 == *other
54+
}
55+
}
56+
57+
impl PartialEq<Score> for u8 {
58+
fn eq(&self, other: &Score) -> bool {
59+
*self == *other as u8
60+
}
61+
}
62+
63+
impl PartialOrd<u8> for Score {
64+
fn partial_cmp(&self, other: &u8) -> Option<cmp::Ordering> {
65+
(*self as u8).partial_cmp(other)
66+
}
67+
}
68+
69+
impl PartialOrd<Score> for u8 {
70+
fn partial_cmp(&self, other: &Score) -> Option<cmp::Ordering> {
71+
self.partial_cmp(&(*other as u8))
72+
}
73+
}
74+
5175
#[derive(Debug, Clone)]
5276
pub struct GuessCalculation {
5377
/// Estimated guesses needed to crack the password
@@ -1162,6 +1186,39 @@ mod tests {
11621186
}
11631187
}
11641188

1189+
#[test]
1190+
fn test_score_partial_eq_u8() {
1191+
assert_eq!(scoring::Score::Zero, 0);
1192+
assert_eq!(scoring::Score::One, 1);
1193+
assert_eq!(scoring::Score::Two, 2);
1194+
assert_eq!(scoring::Score::Three, 3);
1195+
assert_eq!(scoring::Score::Four, 4);
1196+
1197+
assert_eq!(0, scoring::Score::Zero);
1198+
assert_eq!(1, scoring::Score::One);
1199+
assert_eq!(2, scoring::Score::Two);
1200+
assert_eq!(3, scoring::Score::Three);
1201+
assert_eq!(4, scoring::Score::Four);
1202+
}
1203+
1204+
#[test]
1205+
fn test_score_partial_ord_u8() {
1206+
assert!(scoring::Score::Zero < 1);
1207+
assert!(scoring::Score::One > 0);
1208+
assert!(scoring::Score::Two >= 1);
1209+
assert!(scoring::Score::Three <= 4);
1210+
1211+
assert!(0 < scoring::Score::One);
1212+
assert!(1 > scoring::Score::Zero);
1213+
assert!(1 <= scoring::Score::Two);
1214+
assert!(4 >= scoring::Score::Three);
1215+
1216+
assert!(!(scoring::Score::Zero < 0));
1217+
assert!(!(scoring::Score::Four > 4));
1218+
assert!(!(0 > scoring::Score::Zero));
1219+
assert!(!(4 < scoring::Score::Four));
1220+
}
1221+
11651222
#[cfg(feature = "ser")]
11661223
#[test]
11671224
fn serde_score() {

0 commit comments

Comments
 (0)