scsys_core/state/impls/
impl_ops.rs

1/*
2    Appellation: impl_ops <module>
3    Contrib: @FL03
4*/
5use crate::state::State;
6
7macro_rules! impl_binary_op {
8    ($s:ident::<[$($op:ident.$call:ident),* $(,)?]>) => {
9        $(
10            impl_binary_op!(@impl $s::$op.$call);
11            impl_binary_op!(@mut $s::$op.$call);
12        )*
13    };
14    (@impl $s:ident::$op:ident.$call:ident) => {
15        impl<A, B, C> ::core::ops::$op<$s<B>> for $s<A>
16        where
17            A: ::core::ops::$op<B, Output = C>,
18        {
19            type Output = $s<C>;
20
21            fn $call(self, rhs: $s<B>) -> Self::Output {
22                $s(::core::ops::$op::$call(self.0, rhs.0))
23            }
24        }
25
26        impl<'a, A, B, C> ::core::ops::$op<$s<B>> for &'a $s<A>
27        where
28            A: Copy + ::core::ops::$op<B, Output = C>,
29        {
30            type Output = $s<C>;
31
32            fn $call(self, rhs: $s<B>) -> Self::Output {
33                $s(::core::ops::$op::$call(self.0, rhs.0))
34            }
35        }
36
37        impl<'a, A, B, C> ::core::ops::$op<&'a $s<B>> for &'a $s<A>
38        where
39            A: Copy + ::core::ops::$op<B, Output = C>,
40            B: Copy
41        {
42            type Output = $s<C>;
43
44            fn $call(self, rhs: &'a $s<B>) -> Self::Output {
45                $s(::core::ops::$op::$call(self.0, rhs.0))
46            }
47        }
48
49        impl<'a, A, B, C> ::core::ops::$op<&'a $s<B>> for $s<A>
50        where
51            A: ::core::ops::$op<B, Output = C>,
52            B: Copy
53        {
54            type Output = $s<C>;
55
56            fn $call(self, rhs: &'a $s<B>) -> Self::Output {
57                $s(::core::ops::$op::$call(self.0, rhs.0))
58            }
59        }
60
61        impl<'a, A, B, C> ::core::ops::$op<$s<B>> for &'a mut $s<A>
62        where
63            A: Copy + ::core::ops::$op<B, Output = C>,
64        {
65            type Output = $s<C>;
66
67            fn $call(self, rhs: $s<B>) -> Self::Output {
68                $s(::core::ops::$op::$call(self.0, rhs.0))
69            }
70        }
71
72        impl<'a, A, B, C> ::core::ops::$op<&'a mut $s<B>> for $s<A>
73        where
74            A: ::core::ops::$op<B, Output = C>,
75            B: Copy
76        {
77            type Output = $s<C>;
78
79            fn $call(self, rhs: &'a mut $s<B>) -> Self::Output {
80                $s(::core::ops::$op::$call(self.0, rhs.0))
81            }
82        }
83
84        impl<'a, A, B, C> ::core::ops::$op<&'a mut $s<B>> for &'a mut $s<A>
85        where
86            A: Copy + ::core::ops::$op<B, Output = C>,
87            B: Copy
88        {
89            type Output = $s<C>;
90
91            fn $call(self, rhs: &'a mut $s<B>) -> Self::Output {
92                $s(::core::ops::$op::$call(self.0, rhs.0))
93            }
94        }
95    };
96    (@mut $s:ident::$op:ident.$call:ident) => {
97        paste::paste! {
98            impl_binary_op_mut!(@impl $s::[<$op Assign>].[<$call _assign>]);
99        }
100    };
101}
102
103macro_rules! impl_binary_op_mut {
104    ($s:ident::<[$($op:ident.$call:ident),* $(,)?]>) => {
105        $(
106            impl_binary_op!(@impl $s::$op.$call);
107        )*
108    };
109    (@impl $s:ident::$op:ident.$call:ident) => {
110        impl<A, B> ::core::ops::$op<$s<B>> for &mut $s<A>
111        where
112            A: ::core::ops::$op<B>,
113        {
114
115            fn $call(&mut self, rhs: $s<B>) {
116                core::ops::$op::$call(&mut self.0, rhs.0)
117            }
118        }
119    };
120}
121
122macro_rules! impl_unary_op {
123    ($s:ident::<[$($op:ident.$call:ident),* $(,)?]>) => {
124        $(
125            impl_unary_op!(@impl $s::$op.$call);
126        )*
127    };
128    (@impl $s:ident::$op:ident.$call:ident) => {
129        impl<A> ::core::ops::$op for &mut $s<A>
130        where
131            A: Clone + ::core::ops::$op,
132        {
133            type Output = $s<A::Output>;
134
135            fn $call(self) -> Self::Output {
136                $s(core::ops::$op::$call(self.0.clone()))
137            }
138        }
139
140        impl<'a, A> ::core::ops::$op for &mut &'a $s<A>
141        where
142            A: Clone + ::core::ops::$op,
143        {
144            type Output = $s<A::Output>;
145
146            fn $call(self) -> Self::Output {
147                $s(core::ops::$op::$call(self.0.clone()))
148            }
149        }
150
151        impl<'a, A> ::core::ops::$op for &mut &'a mut $s<A>
152        where
153            A: Clone + ::core::ops::$op,
154        {
155            type Output = $s<A::Output>;
156
157            fn $call(self) -> Self::Output {
158                $s(core::ops::$op::$call(self.0.clone()))
159            }
160        }
161    };
162}
163
164impl_binary_op! {
165    State::<[
166        Add.add,
167        Sub.sub,
168        Mul.mul,
169        Div.div,
170        Rem.rem,
171        BitAnd.bitand,
172        BitOr.bitor,
173        BitXor.bitxor,
174        Shl.shl,
175        Shr.shr
176    ]>
177}
178
179impl_unary_op! {
180    State::<[
181        Neg.neg,
182        Not.not
183    ]>
184}