eryon_core/state/impls/
impl_ops.rs

1/*
2    Appellation: impl_ops <state>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use crate::state::{RawState, State};
6
7impl<Q> core::ops::Neg for State<Q>
8where
9    Q: RawState + core::ops::Neg,
10{
11    type Output = State<Q::Output>;
12
13    fn neg(self) -> Self::Output {
14        State(core::ops::Neg::neg(self.0))
15    }
16}
17
18impl<'a, Q> core::ops::Neg for &'a State<Q>
19where
20    &'a Q: RawState + core::ops::Neg,
21{
22    type Output = State<<&'a Q as core::ops::Neg>::Output>;
23
24    fn neg(self) -> Self::Output {
25        State(core::ops::Neg::neg(self.get()))
26    }
27}
28
29impl<'a, Q> core::ops::Neg for &'a mut State<Q>
30where
31    &'a Q: RawState + core::ops::Neg,
32{
33    type Output = State<<&'a Q as core::ops::Neg>::Output>;
34
35    fn neg(self) -> Self::Output {
36        State(core::ops::Neg::neg(self.get()))
37    }
38}
39
40impl<Q> core::ops::Not for State<Q>
41where
42    Q: RawState + core::ops::Not,
43{
44    type Output = State<Q::Output>;
45
46    fn not(self) -> Self::Output {
47        State(core::ops::Not::not(self.0))
48    }
49}
50
51impl<'a, Q> core::ops::Not for &'a State<Q>
52where
53    &'a Q: RawState + core::ops::Not,
54{
55    type Output = State<<&'a Q as core::ops::Not>::Output>;
56
57    fn not(self) -> Self::Output {
58        State(core::ops::Not::not(self.get()))
59    }
60}
61
62impl<'a, Q> core::ops::Not for &'a mut State<Q>
63where
64    &'a Q: RawState + core::ops::Not,
65{
66    type Output = State<<&'a Q as core::ops::Not>::Output>;
67
68    fn not(self) -> Self::Output {
69        State(core::ops::Not::not(self.get()))
70    }
71}
72
73impl<Q> num_traits::Num for State<Q>
74where
75    Q: RawState + num_traits::Num,
76{
77    type FromStrRadixErr = Q::FromStrRadixErr;
78
79    fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
80        Q::from_str_radix(str, radix).map(State)
81    }
82}
83
84impl<Q> num_traits::One for State<Q>
85where
86    Q: RawState + PartialEq + num_traits::One,
87{
88    fn one() -> Self {
89        State(Q::one())
90    }
91
92    fn is_one(&self) -> bool {
93        self.0.is_one()
94    }
95}
96
97impl<Q> num_traits::Zero for State<Q>
98where
99    Q: RawState + num_traits::Zero,
100{
101    fn zero() -> Self {
102        State(Q::zero())
103    }
104
105    fn is_zero(&self) -> bool {
106        self.0.is_zero()
107    }
108}
109
110macro_rules! impl_bin_op {
111    (@impl $wrap:ident($trait:ident::$method:ident)) => {
112        impl<A, B, C> ::core::ops::$trait<$wrap<B>> for $wrap<A>
113        where
114            A: $crate::state::RawState + ::core::ops::$trait<B, Output = C>,
115            B: $crate::state::RawState,
116            C: $crate::state::RawState,
117        {
118            type Output = $wrap<C>;
119
120            fn $method(self, rhs: $wrap<B>) -> Self::Output {
121                $wrap(::core::ops::$trait::$method(self.0, rhs.0))
122            }
123        }
124
125        impl<'a, A, B, C> ::core::ops::$trait<&'a $wrap<B>> for $wrap<A>
126        where
127            A: $crate::state::RawState + ::core::ops::$trait<&'a B, Output = C>,
128            B: $crate::state::RawState,
129            C: $crate::state::RawState,
130        {
131            type Output = $wrap<C>;
132
133            fn $method(self, rhs: &'a $wrap<B>) -> Self::Output {
134                $wrap(::core::ops::$trait::$method(self.value(), rhs.get()))
135            }
136        }
137
138        impl<'a, A, B, C> ::core::ops::$trait<&'a $wrap<B>> for &'a $wrap<A>
139        where
140            &'a A: $crate::state::RawState + ::core::ops::$trait<&'a B, Output = C>,
141            B: $crate::state::RawState,
142            C: $crate::state::RawState,
143        {
144            type Output = $wrap<C>;
145
146            fn $method(self, rhs: &'a $wrap<B>) -> Self::Output {
147                $wrap(::core::ops::$trait::$method(self.get(), rhs.get()))
148            }
149        }
150
151        impl<'a, A, B, C> ::core::ops::$trait<$wrap<B>> for &'a $wrap<A>
152        where
153            &'a A: $crate::state::RawState + ::core::ops::$trait<B, Output = C>,
154            B: $crate::state::RawState,
155            C: $crate::state::RawState,
156        {
157            type Output = $wrap<C>;
158
159            fn $method(self, rhs: $wrap<B>) -> Self::Output {
160                $wrap(::core::ops::$trait::$method(self.get(), rhs.value()))
161            }
162        }
163    };
164
165    ($wrap:ident($($trait:ident::$method:ident),* $(,)?)) => {
166        $(impl_bin_op!(@impl $wrap($trait::$method));)*
167    };
168}
169
170macro_rules! impl_assign_op {
171    (@impl $wrap:ident($trait:ident::$method:ident)) => {
172        impl<A, B> ::core::ops::$trait<B> for $wrap<A>
173        where
174            A: $crate::state::RawState + ::core::ops::$trait<B>,
175            B: $crate::state::RawState,
176        {
177            fn $method(&mut self, rhs: B) {
178                ::core::ops::$trait::$method(self.get_mut(), rhs)
179            }
180        }
181    };
182
183    ($wrap:ident($($trait:ident::$method:ident),* $(,)?)) => {
184        $(impl_assign_op!(@impl $wrap($trait::$method));)*
185    };
186}
187
188impl_assign_op! {
189    State(
190        AddAssign::add_assign,
191        SubAssign::sub_assign,
192        MulAssign::mul_assign,
193        DivAssign::div_assign,
194        RemAssign::rem_assign,
195        BitAndAssign::bitand_assign,
196        BitOrAssign::bitor_assign,
197        BitXorAssign::bitxor_assign,
198        ShlAssign::shl_assign,
199        ShrAssign::shr_assign,
200    )
201}
202
203impl_bin_op! {
204    State(
205        Add::add,
206        Sub::sub,
207        Mul::mul,
208        Div::div,
209        Rem::rem,
210        BitAnd::bitand,
211        BitOr::bitor,
212        BitXor::bitxor,
213        Shl::shl,
214        Shr::shr,
215    )
216}