Created
February 12, 2021 13:55
-
-
Save ashiato45/e9127cf34d782f6969c9de41908bd263 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Eq, PartialEq)] | |
struct Item { | |
date: usize, | |
cost: usize, | |
} | |
macro_rules! easy_ord { | |
($y:ident, $x:expr) => { | |
impl Ord for $y { | |
fn cmp(&self, other: &Self) -> std::cmp::Ordering { | |
$x(self, other) | |
} | |
} | |
impl PartialOrd for $y { | |
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { | |
Some(self.cmp(other)) | |
} | |
} | |
}; | |
} | |
easy_ord!(Item, |x: &Item, other: &Item| { | |
if x.date < other.date { | |
std::cmp::Ordering::Greater | |
} else if x.date > other.date { | |
std::cmp::Ordering::Less | |
} else { | |
usize::cmp(&x.cost, &other.cost) | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment