Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub trait GroupIntersection {
- type Output;
- fn intersection(self) -> Option<Self::Output>;
- }
- macro_rules! impl_reciprocal_intersection {
- (take => ($t:ty, $u:ty)) => {
- impl GroupIntersection for ($u, $t) {
- type Output = <($t, $u) as GroupIntersection>::Output;
- fn intersection(self) -> Option<Self::Output> {
- let (u, t) = self;
- (t, u).intersection()
- }
- }
- };
- (borrow => ($t:ty, $u:ty)) => {
- impl<'a> GroupIntersection for (&'a $u, &'a $t) {
- type Output = <(&'a $t, &'a $u) as GroupIntersection>::Output;
- fn intersection(self) -> Option<Self::Output> {
- let (u, t) = self;
- (t, u).intersection()
- }
- }
- };
- }
- pub trait Intersection<T> {
- type Output;
- fn intersection(self, _: T) -> Option<Self::Output>;
- }
- impl<T, U> Intersection<U> for T
- where
- (T, U): GroupIntersection,
- {
- type Output = <(T, U) as GroupIntersection>::Output;
- fn intersection(self, other: U) -> Option<Self::Output> {
- (self, other).intersection()
- }
- }
- impl GroupIntersection for (i32, f32) {
- type Output = ();
- fn intersection(self) -> Option<Self::Output> {
- let (a, b) = self;
- if a == (b as i32) {
- Some(())
- } else {
- None
- }
- }
- }
- impl_reciprocal_intersection!(take => (i32, f32));
- impl<'a> GroupIntersection for (&'a i32, &'a f32) {
- type Output = ();
- fn intersection(self) -> Option<Self::Output> {
- let (a, b) = self;
- (*a, *b).intersection()
- }
- }
- impl_reciprocal_intersection!(borrow => (i32, f32));
- fn main() {
- let x: i32 = 0;
- let y: f32 = 0.0;
- x.intersection(y);
- y.intersection(x);
- (&x, &y).intersection();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement