1 | use core::ops::{Add, Mul, Sub}; |
2 | |
3 | /// Saturating math operations. Deprecated, use `SaturatingAdd`, `SaturatingSub` and |
4 | /// `SaturatingMul` instead. |
5 | pub trait Saturating { |
6 | /// Saturating addition operator. |
7 | /// Returns a+b, saturating at the numeric bounds instead of overflowing. |
8 | fn saturating_add(self, v: Self) -> Self; |
9 | |
10 | /// Saturating subtraction operator. |
11 | /// Returns a-b, saturating at the numeric bounds instead of overflowing. |
12 | fn saturating_sub(self, v: Self) -> Self; |
13 | } |
14 | |
15 | macro_rules! deprecated_saturating_impl { |
16 | ($trait_name:ident for $($t:ty)*) => {$( |
17 | impl $trait_name for $t { |
18 | #[inline] |
19 | fn saturating_add(self, v: Self) -> Self { |
20 | Self::saturating_add(self, v) |
21 | } |
22 | |
23 | #[inline] |
24 | fn saturating_sub(self, v: Self) -> Self { |
25 | Self::saturating_sub(self, v) |
26 | } |
27 | } |
28 | )*} |
29 | } |
30 | |
31 | deprecated_saturating_impl!(Saturating for isize i8 i16 i32 i64 i128); |
32 | deprecated_saturating_impl!(Saturating for usize u8 u16 u32 u64 u128); |
33 | |
34 | macro_rules! saturating_impl { |
35 | ($trait_name:ident, $method:ident, $t:ty) => { |
36 | impl $trait_name for $t { |
37 | #[inline] |
38 | fn $method(&self, v: &Self) -> Self { |
39 | <$t>::$method(*self, *v) |
40 | } |
41 | } |
42 | }; |
43 | } |
44 | |
45 | /// Performs addition that saturates at the numeric bounds instead of overflowing. |
46 | pub trait SaturatingAdd: Sized + Add<Self, Output = Self> { |
47 | /// Saturating addition. Computes `self + other`, saturating at the relevant high or low boundary of |
48 | /// the type. |
49 | fn saturating_add(&self, v: &Self) -> Self; |
50 | } |
51 | |
52 | saturating_impl!(SaturatingAdd, saturating_add, u8); |
53 | saturating_impl!(SaturatingAdd, saturating_add, u16); |
54 | saturating_impl!(SaturatingAdd, saturating_add, u32); |
55 | saturating_impl!(SaturatingAdd, saturating_add, u64); |
56 | saturating_impl!(SaturatingAdd, saturating_add, usize); |
57 | saturating_impl!(SaturatingAdd, saturating_add, u128); |
58 | |
59 | saturating_impl!(SaturatingAdd, saturating_add, i8); |
60 | saturating_impl!(SaturatingAdd, saturating_add, i16); |
61 | saturating_impl!(SaturatingAdd, saturating_add, i32); |
62 | saturating_impl!(SaturatingAdd, saturating_add, i64); |
63 | saturating_impl!(SaturatingAdd, saturating_add, isize); |
64 | saturating_impl!(SaturatingAdd, saturating_add, i128); |
65 | |
66 | /// Performs subtraction that saturates at the numeric bounds instead of overflowing. |
67 | pub trait SaturatingSub: Sized + Sub<Self, Output = Self> { |
68 | /// Saturating subtraction. Computes `self - other`, saturating at the relevant high or low boundary of |
69 | /// the type. |
70 | fn saturating_sub(&self, v: &Self) -> Self; |
71 | } |
72 | |
73 | saturating_impl!(SaturatingSub, saturating_sub, u8); |
74 | saturating_impl!(SaturatingSub, saturating_sub, u16); |
75 | saturating_impl!(SaturatingSub, saturating_sub, u32); |
76 | saturating_impl!(SaturatingSub, saturating_sub, u64); |
77 | saturating_impl!(SaturatingSub, saturating_sub, usize); |
78 | saturating_impl!(SaturatingSub, saturating_sub, u128); |
79 | |
80 | saturating_impl!(SaturatingSub, saturating_sub, i8); |
81 | saturating_impl!(SaturatingSub, saturating_sub, i16); |
82 | saturating_impl!(SaturatingSub, saturating_sub, i32); |
83 | saturating_impl!(SaturatingSub, saturating_sub, i64); |
84 | saturating_impl!(SaturatingSub, saturating_sub, isize); |
85 | saturating_impl!(SaturatingSub, saturating_sub, i128); |
86 | |
87 | /// Performs multiplication that saturates at the numeric bounds instead of overflowing. |
88 | pub trait SaturatingMul: Sized + Mul<Self, Output = Self> { |
89 | /// Saturating multiplication. Computes `self * other`, saturating at the relevant high or low boundary of |
90 | /// the type. |
91 | fn saturating_mul(&self, v: &Self) -> Self; |
92 | } |
93 | |
94 | saturating_impl!(SaturatingMul, saturating_mul, u8); |
95 | saturating_impl!(SaturatingMul, saturating_mul, u16); |
96 | saturating_impl!(SaturatingMul, saturating_mul, u32); |
97 | saturating_impl!(SaturatingMul, saturating_mul, u64); |
98 | saturating_impl!(SaturatingMul, saturating_mul, usize); |
99 | saturating_impl!(SaturatingMul, saturating_mul, u128); |
100 | |
101 | saturating_impl!(SaturatingMul, saturating_mul, i8); |
102 | saturating_impl!(SaturatingMul, saturating_mul, i16); |
103 | saturating_impl!(SaturatingMul, saturating_mul, i32); |
104 | saturating_impl!(SaturatingMul, saturating_mul, i64); |
105 | saturating_impl!(SaturatingMul, saturating_mul, isize); |
106 | saturating_impl!(SaturatingMul, saturating_mul, i128); |
107 | |
108 | // TODO: add SaturatingNeg for signed integer primitives once the saturating_neg() API is stable. |
109 | |
110 | #[test ] |
111 | fn test_saturating_traits() { |
112 | fn saturating_add<T: SaturatingAdd>(a: T, b: T) -> T { |
113 | a.saturating_add(&b) |
114 | } |
115 | fn saturating_sub<T: SaturatingSub>(a: T, b: T) -> T { |
116 | a.saturating_sub(&b) |
117 | } |
118 | fn saturating_mul<T: SaturatingMul>(a: T, b: T) -> T { |
119 | a.saturating_mul(&b) |
120 | } |
121 | assert_eq!(saturating_add(255, 1), 255u8); |
122 | assert_eq!(saturating_add(127, 1), 127i8); |
123 | assert_eq!(saturating_add(-128, -1), -128i8); |
124 | assert_eq!(saturating_sub(0, 1), 0u8); |
125 | assert_eq!(saturating_sub(-128, 1), -128i8); |
126 | assert_eq!(saturating_sub(127, -1), 127i8); |
127 | assert_eq!(saturating_mul(255, 2), 255u8); |
128 | assert_eq!(saturating_mul(127, 2), 127i8); |
129 | assert_eq!(saturating_mul(-128, 2), -128i8); |
130 | } |
131 | |