haar_lib/macros/
impl_ops.rs

1//! `impl_ops!`
2
3/// [`Add`](std::ops::Add), [`Sub`](std::ops::Sub), [`Mul`](std::ops::Mul), [`Div`](std::ops::Div), [`Rem`](std::ops::Rem), [`AddAssign`](std::ops::AddAssign), [`SubAssign`](std::ops::SubAssign), [`MulAssign`](std::ops::MulAssign), [`DivAssign`](std::ops::DivAssign), [`RemAssign`](std::ops::RemAssign), [`Neg`](std::ops::Neg)を実装する。
4#[macro_export]
5#[doc(hidden)]
6macro_rules! impl_ops {
7    (@inner, $(#[$meta:meta])* [$($bound:tt)*]  $({$($where:tt)*})?; $tr:ty, $rhs:ty, $a:ty, $f:expr, $fn:tt) => {
8        impl <$($bound)*> $tr for $a $(where $($where)*)? {
9            type Output = Self;
10            $(#[$meta])*
11            fn $fn(self, rhs: $rhs) -> Self::Output {
12                $f(self, rhs)
13            }
14        }
15    };
16    (@inner_assign, $(#[$meta:meta])* [$($bound:tt)*] $({$($where:tt)*})?; $tr:ty, $rhs:ty, $a:ty, $f:expr, $fn:tt) => {
17        impl <$($bound)*> $tr for $a $(where $($where)*)? {
18            $(#[$meta])*
19            fn $fn(&mut self, rhs: $rhs) {
20                $f(self, rhs)
21            }
22        }
23    };
24
25    ($(#[$meta:meta])* $({$($bound:tt)*})? $trait:ident for $a:ty $(where {$($where:tt)*})?, $f:expr) => {
26        impl_ops!(@when $(#[$meta])* [$($($bound)*)?] $({$($where)*})?; $trait, Self, $a, $f);
27    };
28    ($(#[$meta:meta])* $({$($bound:tt)*})? $trait:ident <$rhs:ty> for $a:ty $(where {$($where:tt)*})?, $f:expr) => {
29        impl_ops!(@when $(#[$meta])* [$($($bound)*)?] $({$($where)*})?; $trait, $rhs, $a, $f);
30    };
31
32    (@when $(#[$meta:meta])* [$($bound:tt)*] $({$($where:tt)*})?; Add, $rhs:ty, $a:ty, $f:expr) => {
33        impl_ops!(@inner, $(#[$meta])* [$($bound)*] $({$($where)*})?; std::ops::Add<$rhs>, $rhs, $a, $f, add);
34    };
35    (@when $(#[$meta:meta])* [$($bound:tt)*] $({$($where:tt)*})?; Sub, $rhs:ty, $a:ty, $f:expr) => {
36        impl_ops!(@inner, $(#[$meta])* [$($bound)*] $({$($where)*})?; std::ops::Sub<$rhs>, $rhs, $a, $f, sub);
37    };
38    (@when $(#[$meta:meta])* [$($bound:tt)*] $({$($where:tt)*})?; Mul, $rhs:ty, $a:ty, $f:expr) => {
39        impl_ops!(@inner, $(#[$meta])* [$($bound)*] $({$($where)*})?; std::ops::Mul<$rhs>, $rhs, $a, $f, mul);
40    };
41    (@when $(#[$meta:meta])* [$($bound:tt)*] $({$($where:tt)*})?; Div, $rhs:ty, $a:ty, $f:expr) => {
42        impl_ops!(@inner, $(#[$meta])* [$($bound)*] $({$($where)*})?; std::ops::Div<$rhs>, $rhs, $a, $f, div);
43    };
44    (@when $(#[$meta:meta])* [$($bound:tt)*] $({$($where:tt)*})?; Rem, $rhs:ty, $a:ty, $f:expr) => {
45        impl_ops!(@inner, $(#[$meta])* [$($bound)*] $({$($where)*})?; std::ops::Rem<$rhs>, $rhs, $a, $f, rem);
46    };
47
48    (@when $(#[$meta:meta])* [$($bound:tt)*] $({$($where:tt)*})?; AddAssign, $rhs:ty, $a:ty, $f:expr) => {
49        impl_ops!(@inner_assign, $(#[$meta])* [$($bound)*] $({$($where)*})?; std::ops::AddAssign<$rhs>, $rhs, $a, $f, add_assign);
50    };
51    (@when $(#[$meta:meta])* [$($bound:tt)*] $({$($where:tt)*})?; SubAssign, $rhs:ty, $a:ty, $f:expr) => {
52        impl_ops!(@inner_assign, $(#[$meta])* [$($bound)*] $({$($where)*})?; std::ops::SubAssign<$rhs>, $rhs, $a, $f, sub_assign);
53    };
54    (@when $(#[$meta:meta])* [$($bound:tt)*] $({$($where:tt)*})?; MulAssign, $rhs:ty, $a:ty, $f:expr) => {
55        impl_ops!(@inner_assign, $(#[$meta])* [$($bound)*] $({$($where)*})?; std::ops::MulAssign<$rhs>, $rhs, $a, $f, mul_assign);
56    };
57    (@when $(#[$meta:meta])* [$($bound:tt)*] $({$($where:tt)*})?; DivAssign, $rhs:ty, $a:ty, $f:expr) => {
58        impl_ops!(@inner_assign, $(#[$meta])* [$($bound)*] $({$($where)*})?; std::ops::DivAssign<$rhs>, $rhs, $a, $f, div_assign);
59    };
60    (@when $(#[$meta:meta])* [$($bound:tt)*] $({$($where:tt)*})?; RemAssign, $rhs:ty, $a:ty, $f:expr) => {
61        impl_ops!(@inner_assign, $(#[$meta])* [$($bound)*] $({$($where)*})?; std::ops::RemAssign<$rhs>, $rhs, $a, $f, rem_assign);
62    };
63
64    (@when $(#[$meta:meta])* [$($bound:tt)*] $({$($where:tt)*})?; Neg, $rhs:ty, $a:ty, $f:expr) => {
65        impl <$($bound)*> std::ops::Neg for $a $(where $($where)*)? {
66            type Output = Self;
67            $(#[$meta])*
68            fn neg(self) -> Self::Output {
69                $f(self)
70            }
71        }
72    }
73}