arrow_buffer/
arith.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18/// Derives `std::ops::$t` for `$ty` calling `$wrapping` or `$checked` variants
19/// based on if debug_assertions enabled
20macro_rules! derive_arith {
21    ($ty:ty, $t:ident, $t_assign:ident, $op:ident, $op_assign:ident, $wrapping:ident, $checked:ident) => {
22        impl std::ops::$t for $ty {
23            type Output = $ty;
24
25            #[cfg(debug_assertions)]
26            fn $op(self, rhs: Self) -> Self::Output {
27                self.$checked(rhs)
28                    .expect(concat!(stringify!($ty), " overflow"))
29            }
30
31            #[cfg(not(debug_assertions))]
32            fn $op(self, rhs: Self) -> Self::Output {
33                self.$wrapping(rhs)
34            }
35        }
36
37        impl std::ops::$t_assign for $ty {
38            #[cfg(debug_assertions)]
39            fn $op_assign(&mut self, rhs: Self) {
40                *self = self
41                    .$checked(rhs)
42                    .expect(concat!(stringify!($ty), " overflow"));
43            }
44
45            #[cfg(not(debug_assertions))]
46            fn $op_assign(&mut self, rhs: Self) {
47                *self = self.$wrapping(rhs);
48            }
49        }
50
51        impl<'a> std::ops::$t<$ty> for &'a $ty {
52            type Output = $ty;
53
54            fn $op(self, rhs: $ty) -> Self::Output {
55                (*self).$op(rhs)
56            }
57        }
58
59        impl<'a> std::ops::$t<&'a $ty> for $ty {
60            type Output = $ty;
61
62            fn $op(self, rhs: &'a $ty) -> Self::Output {
63                self.$op(*rhs)
64            }
65        }
66
67        impl<'a, 'b> std::ops::$t<&'b $ty> for &'a $ty {
68            type Output = $ty;
69
70            fn $op(self, rhs: &'b $ty) -> Self::Output {
71                (*self).$op(*rhs)
72            }
73        }
74    };
75}
76
77pub(crate) use derive_arith;