Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- macro_rules! impl_comparisons {
- ($ty:ty) => {
- impl PartialOrd for $ty {
- fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
- Some(self.cmp(other))
- }
- }
- impl PartialEq for $ty {
- fn eq(&self, other: &Self) -> bool {
- self.cmp(other) == std::cmp::Ordering::Equal
- }
- }
- impl Eq for $ty {}
- };
- }
- #[derive(Debug)]
- struct Wow(i64);
- impl Ord for Wow {
- fn cmp(&self, other: &Self) -> std::cmp::Ordering {
- self.0.cmp(&other.0)
- }
- }
- impl_comparisons!(Wow);
- fn main() {
- let x = Wow(1);
- let y = Wow(2);
- assert_eq!(std::cmp::Ordering::Less, x.cmp(&y));
- assert_eq!(x, x);
- assert_eq!(y, y);
- assert!(x < y);
- assert!(y > x);
- assert_ne!(x, y);
- assert!(!(x == y));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement