Skip to content

Instantly share code, notes, and snippets.

@ashiato45
Created February 12, 2021 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashiato45/e9127cf34d782f6969c9de41908bd263 to your computer and use it in GitHub Desktop.
Save ashiato45/e9127cf34d782f6969c9de41908bd263 to your computer and use it in GitHub Desktop.
#[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