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]
8macro_rules! impl_algebra {
9    (@inner [$($bound:tt)*]; $t:ty;) => {
10        impl <$($bound)*> Set for $t {}
11    };
12    (@inner [$($bound:tt)*]; $t:ty; op: $f:expr; $($rest:tt)*) => {
13        impl <$($bound)*> BinaryOp for $t {
14            fn op(self, b: Self) -> Self {
15                $f(self, b)
16            }
17        }
18        impl_algebra!(@inner [$($bound)*]; $t; $($rest)*);
19    };
20    (@inner [$($bound:tt)*]; $t:ty; id: $f:expr; $($rest:tt)*) => {
21        impl <$($bound)*> Identity for $t {
22            fn id() -> Self {
23                $f
24            }
25        }
26        impl_algebra!(@inner [$($bound)*]; $t; $($rest)*);
27    };
28    (@inner [$($bound:tt)*]; $t:ty; inv: $f:expr; $($rest:tt)*) => {
29        impl <$($bound)*> Inverse for $t {
30            fn inv(self) -> Self {
31                $f(self)
32            }
33        }
34        impl_algebra!(@inner [$($bound)*]; $t; $($rest)*);
35    };
36    (@inner [$($bound:tt)*]; $t:ty; commu; $($rest:tt)*) => {
37        impl <$($bound)*> Commutative for $t {}
38        impl_algebra!(@inner [$($bound)*]; $t; $($rest)*);
39    };
40    (@inner [$($bound:tt)*]; $t:ty; assoc; $($rest:tt)*) => {
41        impl <$($bound)*> Associative for $t {}
42        impl_algebra!(@inner [$($bound)*]; $t; $($rest)*);
43    };
44    (@inner [$($bound:tt)*]; $t:ty; idem; $($rest:tt)*) => {
45        impl <$($bound)*> Idempotence for $t {}
46        impl_algebra!(@inner [$($bound)*]; $t; $($rest)*);
47    };
48
49    ([$($bound:tt)*]; $t:ty; $($rest:tt)*) => {
50        impl_algebra!(@inner [$($bound)*]; $t; $($rest)*);
51    };
52    ($t:ty; $($rest:tt)*) => {
53        impl_algebra!(@inner []; $t; $($rest)*);
54    };
55}