Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. macro_rules! impl_comparisons {
  2. ($ty:ty) => {
  3. impl PartialOrd for $ty {
  4. fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
  5. Some(self.cmp(other))
  6. }
  7. }
  8.  
  9. impl PartialEq for $ty {
  10. fn eq(&self, other: &Self) -> bool {
  11. self.cmp(other) == std::cmp::Ordering::Equal
  12. }
  13. }
  14.  
  15. impl Eq for $ty {}
  16. };
  17. }
  18.  
  19. #[derive(Debug)]
  20. struct Wow(i64);
  21.  
  22. impl Ord for Wow {
  23. fn cmp(&self, other: &Self) -> std::cmp::Ordering {
  24. self.0.cmp(&other.0)
  25. }
  26. }
  27.  
  28. impl_comparisons!(Wow);
  29.  
  30. fn main() {
  31. let x = Wow(1);
  32. let y = Wow(2);
  33. assert_eq!(std::cmp::Ordering::Less, x.cmp(&y));
  34. assert_eq!(x, x);
  35. assert_eq!(y, y);
  36. assert!(x < y);
  37. assert!(y > x);
  38. assert_ne!(x, y);
  39. assert!(!(x == y));
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement