overflow_proof/
base_checked_ops.rs

1use super::Checked;
2
3pub trait CheckedAdd<Rhs = Self> {
4    type Output;
5
6    fn checked_add(self, rhs: Rhs) -> Option<Self::Output>;
7}
8
9pub trait CheckedSub<Rhs = Self> {
10    type Output;
11
12    fn checked_sub(self, rhs: Rhs) -> Option<Self::Output>;
13}
14
15pub trait CheckedMul<Rhs = Self> {
16    type Output;
17
18    fn checked_mul(self, rhs: Rhs) -> Option<Self::Output>;
19}
20
21pub trait CheckedDiv<Rhs = Self> {
22    type Output;
23
24    fn checked_div(self, rhs: Rhs) -> Option<Self::Output>;
25}
26
27pub trait CheckedRem<Rhs = Self> {
28    type Output;
29
30    fn checked_rem(self, rhs: Rhs) -> Option<Self::Output>;
31}
32
33pub trait CheckedAbs : Sized {
34    type Output;
35
36    fn checked_abs(self) -> Option<Self>;
37}
38
39pub trait CheckedNeg : Sized {
40    type Output;
41
42    fn checked_neg(self) -> Option<Self>;
43}
44
45
46macro_rules! impl_checked_trait_2_for {
47    ($checked_t:tt, $checked_op:ident, $t:ty) => {
48
49        impl $checked_t for $t {
50            type Output = $t;
51
52            fn $checked_op(self, rhs: Self) -> Option<Self::Output> {
53                self.$checked_op(rhs)
54            }
55        }
56
57        impl<D> $checked_t<Checked<$t, D>> for $t {
58            type Output = $t;
59
60            fn $checked_op(self, rhs: Checked<$t, D>) -> Option<Self::Output> {
61                self.$checked_op(rhs.v)
62            }
63        }
64    }
65}
66
67macro_rules! impl_checked_trait_1_for {
68    ($checked_t:ty, $checked_op:ident, $t:ty) => {
69
70        impl $checked_t for $t {
71            type Output = $t;
72
73            fn $checked_op(self) -> Option<Self::Output> {
74                self.$checked_op()
75            }
76        }
77    }
78}
79
80macro_rules! impl_checked_all {
81    ($t:ty) => {
82        impl_checked_trait_2_for!(CheckedAdd, checked_add, $t);
83        impl_checked_trait_2_for!(CheckedSub, checked_sub, $t);
84        impl_checked_trait_2_for!(CheckedMul, checked_mul, $t);
85        impl_checked_trait_2_for!(CheckedDiv, checked_div, $t);
86        impl_checked_trait_2_for!(CheckedRem, checked_rem, $t);
87        impl_checked_trait_1_for!(CheckedNeg, checked_neg, $t);
88    }
89}
90
91impl_checked_all!(usize);
92impl_checked_all!(isize);
93impl_checked_all!(u8);
94impl_checked_all!(i8);
95impl_checked_all!(u16);
96impl_checked_all!(i16);
97impl_checked_all!(u32);
98impl_checked_all!(i32);
99impl_checked_all!(u64);
100impl_checked_all!(i64);
101impl_checked_all!(u128);
102impl_checked_all!(i128);
103
104// nightly only
105/*
106impl_checked_trait_1_for!(CheckedAbs, checked_abs, std::num::NonZeroIsize);
107impl_checked_trait_1_for!(CheckedAbs, checked_abs, std::num::NonZeroI8);
108impl_checked_trait_1_for!(CheckedAbs, checked_abs, std::num::NonZeroI16);
109impl_checked_trait_1_for!(CheckedAbs, checked_abs, std::num::NonZeroI32);
110impl_checked_trait_1_for!(CheckedAbs, checked_abs, std::num::NonZeroI64);
111impl_checked_trait_1_for!(CheckedAbs, checked_abs, std::num::NonZeroI128);
112*/
113