ff_uint/num/
macros.rs

1#![allow(unused_macros)]
2
3
4
5#[macro_export]
6#[doc(hidden)]
7macro_rules! impl_num_overflowing_binop{
8    (impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> $op: ident for $name: ty, $method:ident, $overflowing_op: ident) => {
9        impl_num_overflowing_binop!(impl <$($imp_l, )*$($imp_i : $imp_p),+> $op<$name> for $name, $method, $overflowing_op);
10    };
11
12	(impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> $op: ident<$other:ty> for $name: ty, $method:ident, $overflowing_op: ident) => {
13		impl <$($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<$other> for $name {
14			type Output = $name;
15
16            #[inline]
17			fn $method(self, other: $other) -> Self::Output {
18                let (res, overflow) = self.0.$overflowing_op(other.0);
19                panic_on_overflow!(overflow);
20                <$name>::new(res)
21            }
22        }
23
24		impl<'macro_lifetime, $($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<&'macro_lifetime $other> for $name {
25			type Output = $name;
26
27            #[inline]
28			fn $method(self, other: &'macro_lifetime $other) -> Self::Output {
29                let (res, overflow) = self.0.$overflowing_op(other.0);
30                panic_on_overflow!(overflow);
31                <$name>::new(res)
32            }
33        }
34
35		impl<'macro_lifetime, $($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<$other> for &'macro_lifetime $name {
36			type Output = $name;
37
38            #[inline]
39			fn $method(self, other: $other) -> Self::Output {
40                let (res, overflow) = self.0.$overflowing_op(other.0);
41                panic_on_overflow!(overflow);
42                <$name>::new(res)
43            }
44        }
45
46		impl<'macro_lifetime_a, 'macro_lifetime_b, $($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<&'macro_lifetime_a $other> for &'macro_lifetime_b $name {
47			type Output = $name;
48
49            #[inline]
50			fn $method(self, other: &'macro_lifetime_a $other) -> Self::Output {
51                let (res, overflow) = self.0.$overflowing_op(other.0);
52                panic_on_overflow!(overflow);
53                <$name>::new(res)
54            }
55        }
56	};
57}
58
59#[macro_export]
60#[doc(hidden)]
61macro_rules! impl_num_overflowing_binop_primitive{
62
63	(impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> $op: ident<$other:ty> for $name: ty, $method:ident, $overflowing_op: ident) => {
64		impl <$($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<$other> for $name {
65			type Output = $name;
66
67            #[inline]
68			fn $method(self, other: $other) -> Self::Output {
69                let (res, overflow) = self.0.$overflowing_op(other);
70                panic_on_overflow!(overflow);
71                <$name>::new(res)
72            }
73        }
74
75		impl<'macro_lifetime, $($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<&'macro_lifetime $other> for $name {
76			type Output = $name;
77
78            #[inline]
79			fn $method(self, other: &'macro_lifetime $other) -> Self::Output {
80                let (res, overflow) = self.0.$overflowing_op(*other);
81                panic_on_overflow!(overflow);
82                <$name>::new(res)
83            }
84        }
85
86		impl<'macro_lifetime, $($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<$other> for &'macro_lifetime $name {
87			type Output = $name;
88
89            #[inline]
90			fn $method(self, other: $other) -> Self::Output {
91                let (res, overflow) = self.0.$overflowing_op(other);
92                panic_on_overflow!(overflow);
93                <$name>::new(res)
94            }
95        }
96
97		impl<'macro_lifetime_a, 'macro_lifetime_b, $($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<&'macro_lifetime_a $other> for &'macro_lifetime_b $name {
98			type Output = $name;
99
100            #[inline]
101			fn $method(self, other: &'macro_lifetime_a $other) -> Self::Output {
102                let (res, overflow) = self.0.$overflowing_op(*other);
103                panic_on_overflow!(overflow);
104                <$name>::new(res)
105            }
106        }
107	};
108}
109
110#[macro_export]
111#[doc(hidden)]
112macro_rules! impl_num_overflowing_unop {
113    (impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> $op: ident for $name: ty, $method:ident, $overflowing_op: ident) => {
114        impl <$($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op for $name {
115            type Output = $name;
116            #[inline]
117            fn $method(self) -> $name {
118                let (res, overflow) = self.0.$overflowing_op();
119                panic_on_overflow!(overflow);
120                <$name>::new(res)
121            }
122        }
123
124        impl <'macro_lifetime, $($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op for &'macro_lifetime $name {
125            type Output = $name;
126            #[inline]
127            fn $method(self) -> $name {
128                let (res, overflow) = self.0.$overflowing_op();
129                panic_on_overflow!(overflow);
130                <$name>::new(res)
131            }
132        }
133    };
134}
135
136#[macro_export]
137#[doc(hidden)]
138macro_rules! impl_num_overflowing_assignop{
139    (impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> $op: ident for $name: ty, $method:ident, $overflowing_op: ident) => {
140        impl_num_overflowing_assignop!(impl <$($imp_l, )*$($imp_i : $imp_p),+> $op<$name> for $name, $method, $overflowing_op);
141    };
142
143	(impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> $op: ident<$other:ty> for $name: ty, $method:ident, $overflowing_op: ident) => {
144		impl<$($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<$other> for $name {
145            #[inline]
146			fn $method(&mut self, other: $other) {
147                let (res, overflow) = self.0.$overflowing_op(other.0);
148                panic_on_overflow!(overflow);
149                self.0 = res;
150            }
151        }
152
153		impl<'macro_lifetime, $($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<&'macro_lifetime $other> for $name {
154            #[inline]
155			fn $method(&mut self, other: &'macro_lifetime $other) {
156                let (res, overflow) = self.0.$overflowing_op(other.0);
157                panic_on_overflow!(overflow);
158                self.0 = res;
159            }
160        }
161    };
162}
163
164#[macro_export]
165#[doc(hidden)]
166macro_rules! impl_num_overflowing_assignop_primitive{
167    (impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> $op: ident for $name: ty, $method:ident, $overflowing_op: ident) => {
168        impl_num_overflowing_assignop!(impl <$($imp_l, )*$($imp_i : $imp_p),+> $op<$name> for $name, $method, $overflowing_op);
169    };
170
171	(impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> $op: ident<$other:ty> for $name: ty, $method:ident, $overflowing_op: ident) => {
172		impl<$($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<$other> for $name {
173            #[inline]
174			fn $method(&mut self, other: $other) {
175                let (res, overflow) = self.0.$overflowing_op(other);
176                panic_on_overflow!(overflow);
177                self.0 = res;
178            }
179        }
180
181		impl<'macro_lifetime, $($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<&'macro_lifetime $other> for $name {
182            #[inline]
183			fn $method(&mut self, other: &'macro_lifetime $other) {
184                let (res, overflow) = self.0.$overflowing_op(*other);
185                panic_on_overflow!(overflow);
186                self.0 = res;
187            }
188        }
189    };
190}
191
192#[macro_export]
193#[doc(hidden)]
194macro_rules! impl_num_map_from {
195    (impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> From<$from:ty> for $to: ty) => {
196		impl<$($imp_l, )*$($imp_i : $imp_p),+> From<$from> for $to {
197			fn from(value: $from) -> $to {
198				<$to>::new(From::from(value))
199			}
200		}
201	};
202}
203
204#[macro_export]
205#[doc(hidden)]
206macro_rules! impl_fnum_map_from {
207    (impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> From<$from:ty> for $to: ty) => {
208		impl<$($imp_l, )*$($imp_i : $imp_p),+> From<$from> for $to {
209			fn from(value: $from) -> $to {
210				<$to>::from_uint_unchecked(From::from(value))
211			}
212		}
213	};
214}
215
216#[macro_export]
217#[doc(hidden)]
218macro_rules! impl_fnum_map_from_signed {
219    (impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> From<$from:ty> for $to: ty) => {
220		impl<$($imp_l, )*$($imp_i : $imp_p),+> From<$from> for $to {
221			fn from(value: $from) -> $to {
222                if value > 0 {
223                    <$to>::from_uint_unchecked(From::from(value))
224                } else {
225                    -<$to>::from_uint_unchecked(From::from(-value))
226                }
227			}
228		}
229	};
230}
231
232#[macro_export]
233#[doc(hidden)]
234macro_rules! impl_num_try_from_for_primitive {
235    (impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> TryFrom<$from:ty> for $to: ty) => {
236		impl<$($imp_l, )*$($imp_i : $imp_p),+> core::convert::TryFrom<$from> for $to {
237			type Error = &'static str;
238
239			#[inline]
240			fn try_from(u: $from) -> core::result::Result<$to, &'static str> {
241                match u.0.try_into() {
242                    Ok(v)=>Ok(v),
243                    _=> Err(concat!("integer overflow when casting to ", stringify!($to)))
244                }
245
246            }
247		}
248	};
249}
250
251#[macro_export]
252#[doc(hidden)]
253macro_rules! impl_fnum_try_from_for_primitive {
254    (impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> TryFrom<$from:ty> for $to: ty) => {
255		impl<$($imp_l, )*$($imp_i : $imp_p),+> core::convert::TryFrom<$from> for $to {
256			type Error = &'static str;
257
258			#[inline]
259			fn try_from(u: $from) -> core::result::Result<$to, &'static str> {
260                match u.to_uint().try_into() {
261                    Ok(v)=>Ok(v),
262                    _=> Err(concat!("integer overflow when casting to ", stringify!($to)))
263                }
264
265            }
266		}
267	};
268}
269
270#[macro_export]
271#[doc(hidden)]
272macro_rules! impl_fnum_try_from_for_primitive_signed {
273    (impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> TryFrom<$from:ty> for $to: ty) => {
274		impl<$($imp_l, )*$($imp_i : $imp_p),+> core::convert::TryFrom<$from> for $to {
275			type Error = &'static str;
276
277			#[inline]
278			fn try_from(u: $from) -> core::result::Result<$to, &'static str> {
279                let u = u.to_uint();
280                match u.try_into() {
281                    Ok(v)=>Ok(v),
282                    _=> match (<$from>::MODULUS-u).try_into() {
283                            Ok(v)=> {let v:$to = v; Ok(-v)}
284                            _=> Err(concat!("integer overflow when casting to ", stringify!($to)))
285                        }
286                }
287
288            }
289		}
290	};
291}
292
293#[macro_export]
294#[doc(hidden)]
295macro_rules! impl_num_wrapping_binop{
296    (impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> $op: ident for $name: ty, $method:ident, $wrapping_op: ident) => {
297        impl_num_wrapping_binop!(impl <$($imp_l, )*$($imp_i : $imp_p),+> $op<$name> for $name, $method, $wrapping_op);
298    };
299
300	(impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> $op: ident<$other:ty> for $name: ty, $method:ident, $wrapping_op: ident) => {
301		impl <$($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<$other> for $name {
302			type Output = $name;
303
304            #[inline]
305			fn $method(self, other: $other) -> Self::Output {
306                <$name>::new(self.0.$wrapping_op(other.0))
307            }
308        }
309
310		impl<'macro_lifetime, $($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<&'macro_lifetime $other> for $name {
311			type Output = $name;
312
313            #[inline]
314			fn $method(self, other: &'macro_lifetime $other) -> Self::Output {
315                <$name>::new(self.0.$wrapping_op(other.0))
316            }
317        }
318
319		impl<'macro_lifetime, $($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<$other> for &'macro_lifetime $name {
320			type Output = $name;
321
322            #[inline]
323			fn $method(self, other: $other) -> Self::Output {
324                <$name>::new(self.0.$wrapping_op(other.0))
325            }
326        }
327
328		impl<'macro_lifetime_a, 'macro_lifetime_b, $($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<&'macro_lifetime_a $other> for &'macro_lifetime_b $name {
329			type Output = $name;
330
331            #[inline]
332			fn $method(self, other: &'macro_lifetime_a $other) -> Self::Output {
333                <$name>::new(self.0.$wrapping_op(other.0))
334            }
335        }
336	};
337}
338
339#[macro_export]
340#[doc(hidden)]
341macro_rules! impl_num_wrapping_unop {
342    (impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> $op: ident for $name: ty, $method:ident, $wrapping_op: ident) => {
343        impl <$($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op for $name {
344            type Output = $name;
345            #[inline]
346            fn $method(self) -> $name {
347                <$name>::new(self.0.$wrapping_op())
348            }
349        }
350
351        impl <'macro_lifetime, $($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op for &'macro_lifetime $name {
352            type Output = $name;
353            #[inline]
354            fn $method(self) -> $name {
355                <$name>::new(self.0.$wrapping_op())
356            }
357        }
358    };
359}
360
361#[macro_export]
362#[doc(hidden)]
363macro_rules! impl_num_wrapping_assignop{
364    (impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> $op: ident for $name: ty, $method:ident, $wrapping_op: ident) => {
365        impl_num_wrapping_assignop!(impl <$($imp_l, )*$($imp_i : $imp_p),+> $op<$name> for $name, $method, $wrapping_op);
366    };
367
368	(impl <$($imp_l:lifetime, )*$($imp_i:ident : $imp_p:path),+> $op: ident<$other:ty> for $name: ty, $method:ident, $wrapping_op: ident) => {
369		impl<$($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<$other> for $name {
370            #[inline]
371			fn $method(&mut self, other: $other) {
372                self.0 = self.0.$wrapping_op(other.0);
373            }
374        }
375
376		impl<'macro_lifetime, $($imp_l, )*$($imp_i : $imp_p),+> core::ops::$op<&'macro_lifetime $other> for $name {
377            #[inline]
378			fn $method(&mut self, other: &'macro_lifetime $other) {
379                self.0 = self.0.$wrapping_op(other.0);
380            }
381        }
382    };
383}