Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. pub trait GroupIntersection {
  2. type Output;
  3.  
  4. fn intersection(self) -> Option<Self::Output>;
  5. }
  6. macro_rules! impl_reciprocal_intersection {
  7. (take => ($t:ty, $u:ty)) => {
  8. impl GroupIntersection for ($u, $t) {
  9. type Output = <($t, $u) as GroupIntersection>::Output;
  10.  
  11. fn intersection(self) -> Option<Self::Output> {
  12. let (u, t) = self;
  13. (t, u).intersection()
  14. }
  15. }
  16. };
  17. (borrow => ($t:ty, $u:ty)) => {
  18. impl<'a> GroupIntersection for (&'a $u, &'a $t) {
  19. type Output = <(&'a $t, &'a $u) as GroupIntersection>::Output;
  20.  
  21. fn intersection(self) -> Option<Self::Output> {
  22. let (u, t) = self;
  23. (t, u).intersection()
  24. }
  25. }
  26. };
  27. }
  28.  
  29. pub trait Intersection<T> {
  30. type Output;
  31.  
  32. fn intersection(self, _: T) -> Option<Self::Output>;
  33. }
  34.  
  35. impl<T, U> Intersection<U> for T
  36. where
  37. (T, U): GroupIntersection,
  38. {
  39. type Output = <(T, U) as GroupIntersection>::Output;
  40.  
  41. fn intersection(self, other: U) -> Option<Self::Output> {
  42. (self, other).intersection()
  43. }
  44. }
  45.  
  46. impl GroupIntersection for (i32, f32) {
  47. type Output = ();
  48.  
  49. fn intersection(self) -> Option<Self::Output> {
  50. let (a, b) = self;
  51. if a == (b as i32) {
  52. Some(())
  53. } else {
  54. None
  55. }
  56. }
  57. }
  58. impl_reciprocal_intersection!(take => (i32, f32));
  59.  
  60. impl<'a> GroupIntersection for (&'a i32, &'a f32) {
  61. type Output = ();
  62.  
  63. fn intersection(self) -> Option<Self::Output> {
  64. let (a, b) = self;
  65. (*a, *b).intersection()
  66. }
  67. }
  68. impl_reciprocal_intersection!(borrow => (i32, f32));
  69.  
  70. fn main() {
  71. let x: i32 = 0;
  72. let y: f32 = 0.0;
  73. x.intersection(y);
  74. y.intersection(x);
  75. (&x, &y).intersection();
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement