modes:pair: implement Eq and Ord for Pair
If we auto-derived Ord it would look at Pair::0 first, when it should look at Pair::1. Hence the custom implementation.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
use crate::card::{Card, PlayingCard};
|
use crate::card::{Card, PlayingCard};
|
||||||
use crate::helper::ordered;
|
use crate::helper::ordered;
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Eq, Debug, Copy, Clone)]
|
||||||
pub struct Pair(Card, Card);
|
pub struct Pair(Card, Card);
|
||||||
|
|
||||||
impl Pair {
|
impl Pair {
|
||||||
@@ -46,3 +46,27 @@ impl Display for Pair {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
|
impl Ord for Pair {
|
||||||
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
|
match (self.0.cmp(&other.0), self.1.cmp(&other.1)) {
|
||||||
|
// If Pair::1 are equal, then it's dependent on Pair::0
|
||||||
|
(x, Ordering::Equal) => x,
|
||||||
|
// Otherwise leave it to Pair::1
|
||||||
|
(_, x) => x,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Pair {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.cmp(other) == Ordering::Equal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialOrd for Pair {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
|
Some(self.cmp(other))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user