Skip to main content

arrow_buffer/
arith.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5/// Derives `std::ops::$t` for `$ty` calling `$wrapping` or `$checked` variants
6/// based on if debug_assertions enabled
7macro_rules! derive_arith {
8    ($ty:ty, $t:ident, $t_assign:ident, $op:ident, $op_assign:ident, $wrapping:ident, $checked:ident) => {
9        impl std::ops::$t for $ty {
10            type Output = $ty;
11
12            #[cfg(debug_assertions)]
13            fn $op(self, rhs: Self) -> Self::Output {
14                self.$checked(rhs)
15                    .expect(concat!(stringify!($ty), " overflow"))
16            }
17
18            #[cfg(not(debug_assertions))]
19            fn $op(self, rhs: Self) -> Self::Output {
20                self.$wrapping(rhs)
21            }
22        }
23
24        impl std::ops::$t_assign for $ty {
25            #[cfg(debug_assertions)]
26            fn $op_assign(&mut self, rhs: Self) {
27                *self = self
28                    .$checked(rhs)
29                    .expect(concat!(stringify!($ty), " overflow"));
30            }
31
32            #[cfg(not(debug_assertions))]
33            fn $op_assign(&mut self, rhs: Self) {
34                *self = self.$wrapping(rhs);
35            }
36        }
37
38        impl<'a> std::ops::$t<$ty> for &'a $ty {
39            type Output = $ty;
40
41            fn $op(self, rhs: $ty) -> Self::Output {
42                (*self).$op(rhs)
43            }
44        }
45
46        impl<'a> std::ops::$t<&'a $ty> for $ty {
47            type Output = $ty;
48
49            fn $op(self, rhs: &'a $ty) -> Self::Output {
50                self.$op(*rhs)
51            }
52        }
53
54        impl<'a, 'b> std::ops::$t<&'b $ty> for &'a $ty {
55            type Output = $ty;
56
57            fn $op(self, rhs: &'b $ty) -> Self::Output {
58                (*self).$op(*rhs)
59            }
60        }
61    };
62}
63
64pub(crate) use derive_arith;