haar_lib/macros/
impl_algebra.rs

1//! `impl_algebra!`
2
3/// [`Set`](crate::algebra::traits::Set), [`BinaryOp`](crate::algebra::traits::BinaryOp),
4/// [`Identity`](crate::algebra::traits::Identity), [`Inverse`](crate::algebra::traits::Inverse),
5/// [`Commutative`](crate::algebra::traits::Commutative), [`Associative`](crate::algebra::traits::Associative),
6/// [`Idempotence`](crate::algebra::traits::Idempotence)を実装する。
7#[macro_export]
8#[doc(hidden)]
9macro_rules! impl_algebra {
10    (@inner {$($bound:tt)*} $t:ty $({$($where:tt)*})?;) => {};
11    (@inner {$($bound:tt)*} $t:ty $({$($where:tt)*})?; set: $f:ty; $($rest:tt)*) => {
12        impl <$($bound)*> Set for $t $(where $($where)*)? {
13            type Element = $f;
14        }
15        impl_algebra!(@inner {$($bound)*} $t $({$($where)*})?; $($rest)*);
16    };
17    (@inner {$($bound:tt)*} $t:ty $({$($where:tt)*})?; op: $f:expr; $($rest:tt)*) => {
18        impl <$($bound)*> BinaryOp for $t $(where $($where)*)? {
19            fn op(&self, a: Self::Element, b: Self::Element) -> Self::Element {
20                $f(self, a, b)
21            }
22        }
23        impl_algebra!(@inner {$($bound)*} $t $({$($where)*})?; $($rest)*);
24    };
25    (@inner {$($bound:tt)*} $t:ty $({$($where:tt)*})?; id: $f:expr; $($rest:tt)*) => {
26        impl <$($bound)*> Identity for $t $(where $($where)*)? {
27            fn id(&self) -> Self::Element {
28                $f(self)
29            }
30        }
31        impl_algebra!(@inner {$($bound)*} $t $({$($where)*})?; $($rest)*);
32    };
33    (@inner {$($bound:tt)*} $t:ty $({$($where:tt)*})?; inv: $f:expr; $($rest:tt)*) => {
34        impl <$($bound)*> Inverse for $t $(where $($where)*)? {
35            fn inv(&self, a: Self::Element) -> Self::Element {
36                $f(self, a)
37            }
38        }
39        impl_algebra!(@inner {$($bound)*} $t $({$($where)*})?; $($rest)*);
40    };
41    (@inner {$($bound:tt)*} $t:ty $({$($where:tt)*})?; commu; $($rest:tt)*) => {
42        impl <$($bound)*> Commutative for $t $(where $($where)*)? {}
43        impl_algebra!(@inner {$($bound)*} $t $({$($where)*})?; $($rest)*);
44    };
45    (@inner {$($bound:tt)*} $t:ty $({$($where:tt)*})?; assoc; $($rest:tt)*) => {
46        impl <$($bound)*> Associative for $t $(where $($where)*)? {}
47        impl_algebra!(@inner {$($bound)*} $t $({$($where)*})?; $($rest)*);
48    };
49    (@inner {$($bound:tt)*} $t:ty $({$($where:tt)*})?; idem; $($rest:tt)*) => {
50        impl <$($bound)*> Idempotence for $t $(where $($where)*)? {}
51        impl_algebra!(@inner {$($bound)*} $t $({$($where)*})?; $($rest)*);
52    };
53
54    ($({$($bound:tt)*})? $t:ty $(where {$($where:tt)*})?; $($rest:tt)*) => {
55        impl_algebra!(@inner {$($($bound)*)?} $t $({$($where)*})?; $($rest)*);
56    };
57}