reweb3_num/bint/
saturating.rs

1use crate::{doc, ExpType};
2
3macro_rules! saturating {
4    ($BUint: ident, $BInt: ident, $Digit: ident) => {
5        #[doc = doc::saturating::impl_desc!()]
6        impl<const N: usize> $BInt<N> {
7            #[doc = doc::saturating::saturating_add!(I)]
8            #[must_use = doc::must_use_op!()]
9            #[inline]
10            pub const fn saturating_add(self, rhs: Self) -> Self {
11                match self.checked_add(rhs) {
12                    Some(add) => add,
13                    None => {
14                        if self.is_negative() {
15                            Self::MIN
16                        } else {
17                            Self::MAX
18                        }
19                    }
20                }
21            }
22
23            #[doc = doc::saturating::saturating_add_unsigned!(I)]
24            #[must_use = doc::must_use_op!()]
25            #[inline]
26            pub const fn saturating_add_unsigned(self, rhs: $BUint<N>) -> Self {
27                match self.checked_add_unsigned(rhs) {
28                    Some(i) => i,
29                    None => Self::MAX,
30                }
31            }
32
33            #[doc = doc::saturating::saturating_sub!(I)]
34            #[must_use = doc::must_use_op!()]
35            #[inline]
36            pub const fn saturating_sub(self, rhs: Self) -> Self {
37                match self.checked_sub(rhs) {
38                    Some(add) => add,
39                    None => {
40                        if self.is_negative() {
41                            Self::MIN
42                        } else {
43                            Self::MAX
44                        }
45                    }
46                }
47            }
48
49            #[doc = doc::saturating::saturating_sub_unsigned!(I)]
50            #[must_use = doc::must_use_op!()]
51            #[inline]
52            pub const fn saturating_sub_unsigned(self, rhs: $BUint<N>) -> Self {
53                match self.checked_sub_unsigned(rhs) {
54                    Some(i) => i,
55                    None => Self::MIN,
56                }
57            }
58
59            #[doc = doc::saturating::saturating_mul!(I)]
60            #[must_use = doc::must_use_op!()]
61            #[inline]
62            pub const fn saturating_mul(self, rhs: Self) -> Self {
63                match self.checked_mul(rhs) {
64                    Some(mul) => mul,
65                    None => {
66                        if self.is_negative() == rhs.is_negative() {
67                            Self::MAX
68                        } else {
69                            Self::MIN
70                        }
71                    }
72                }
73            }
74
75            #[doc = doc::saturating::saturating_div!(I)]
76            #[must_use = doc::must_use_op!()]
77            #[inline]
78            pub const fn saturating_div(self, rhs: Self) -> Self {
79                let (div, overflow) = self.overflowing_div(rhs);
80                if overflow {
81                    Self::MAX
82                } else {
83                    div
84                }
85            }
86
87            #[doc = doc::saturating::saturating_neg!(I)]
88            #[must_use = doc::must_use_op!()]
89            #[inline]
90            pub const fn saturating_neg(self) -> Self {
91                match self.checked_neg() {
92                    Some(abs) => abs,
93                    None => Self::MAX,
94                }
95            }
96
97            #[doc = doc::saturating::saturating_abs!(I)]
98            #[must_use = doc::must_use_op!()]
99            #[inline]
100            pub const fn saturating_abs(self) -> Self {
101                match self.checked_abs() {
102                    Some(abs) => abs,
103                    None => Self::MAX,
104                }
105            }
106
107            #[doc = doc::saturating::saturating_pow!(I)]
108            #[must_use = doc::must_use_op!()]
109            #[inline]
110            pub const fn saturating_pow(self, exp: ExpType) -> Self {
111                match self.checked_pow(exp) {
112                    Some(pow) => pow,
113                    None => {
114                        if self.is_negative() && exp & 1 != 0 {
115                            Self::MIN
116                        } else {
117                            Self::MAX
118                        }
119                    }
120                }
121            }
122        }
123    };
124}
125
126crate::macro_impl!(saturating);