|
1 | 1 | use core::num::Wrapping; |
2 | 2 | use core::ops::{Add,Mul}; |
3 | 3 |
|
| 4 | +#[cfg(has_num_saturating)] |
| 5 | +use core::num::Saturating; |
| 6 | + |
4 | 7 | /// Defines an additive identity element for `Self`. |
5 | 8 | /// |
6 | 9 | /// # Laws |
|
95 | 98 | constZERO:Self =Wrapping(T::ZERO); |
96 | 99 | } |
97 | 100 |
|
| 101 | +#[cfg(has_num_saturating)] |
| 102 | +impl<T:Zero>ZeroforSaturating<T> |
| 103 | +where |
| 104 | +Saturating<T>:Add<Output =Saturating<T>>, |
| 105 | +{ |
| 106 | +fnis_zero(&self) ->bool{ |
| 107 | +self.0.is_zero() |
| 108 | +} |
| 109 | + |
| 110 | +fnset_zero(&mutself){ |
| 111 | +self.0.set_zero(); |
| 112 | +} |
| 113 | + |
| 114 | +fnzero() ->Self{ |
| 115 | +Saturating(T::zero()) |
| 116 | +} |
| 117 | +} |
| 118 | + |
| 119 | +#[cfg(has_num_saturating)] |
| 120 | +impl<T:ConstZero>ConstZeroforSaturating<T> |
| 121 | +where |
| 122 | +Saturating<T>:Add<Output =Saturating<T>>, |
| 123 | +{ |
| 124 | +constZERO:Self =Saturating(T::ZERO); |
| 125 | +} |
| 126 | + |
98 | 127 | /// Defines a multiplicative identity element for `Self`. |
99 | 128 | /// |
100 | 129 | /// # Laws |
@@ -196,6 +225,28 @@ where |
196 | 225 | constONE:Self =Wrapping(T::ONE); |
197 | 226 | } |
198 | 227 |
|
| 228 | +#[cfg(has_num_saturating)] |
| 229 | +impl<T:One>OneforSaturating<T> |
| 230 | +where |
| 231 | +Saturating<T>:Mul<Output =Saturating<T>>, |
| 232 | +{ |
| 233 | +fnset_one(&mutself){ |
| 234 | +self.0.set_one(); |
| 235 | +} |
| 236 | + |
| 237 | +fnone() ->Self{ |
| 238 | +Saturating(T::one()) |
| 239 | +} |
| 240 | +} |
| 241 | + |
| 242 | +#[cfg(has_num_saturating)] |
| 243 | +impl<T:ConstOne>ConstOneforSaturating<T> |
| 244 | +where |
| 245 | +Saturating<T>:Mul<Output =Saturating<T>>, |
| 246 | +{ |
| 247 | +constONE:Self =Saturating(T::ONE); |
| 248 | +} |
| 249 | + |
199 | 250 | // Some helper functions provided for backwards compatibility. |
200 | 251 |
|
201 | 252 | /// Returns the additive identity, `0`. |
@@ -236,3 +287,33 @@ fn wrapping_is_one() { |
236 | 287 | fnrequire_one<T:One>(_:&T){} |
237 | 288 | require_one(&Wrapping(42)); |
238 | 289 | } |
| 290 | + |
| 291 | +#[test] |
| 292 | +#[cfg(has_num_saturating)] |
| 293 | +fnsaturating_identities(){ |
| 294 | +macro_rules! test_saturating_identities{ |
| 295 | +($($t:ty)+) =>{ |
| 296 | + $( |
| 297 | + assert_eq!(zero::<$t>(), zero::<Saturating<$t>>().0); |
| 298 | + assert_eq!(one::<$t>(), one::<Saturating<$t>>().0); |
| 299 | + assert_eq!((0as $t).is_zero(),Saturating(0as $t).is_zero()); |
| 300 | + assert_eq!((1as $t).is_zero(),Saturating(1as $t).is_zero()); |
| 301 | +)+ |
| 302 | +}; |
| 303 | +} |
| 304 | + |
| 305 | +test_saturating_identities!(isizei8i16i32i64usizeu8u16u32u64); |
| 306 | +} |
| 307 | + |
| 308 | +#[test] |
| 309 | +#[cfg(has_num_saturating)] |
| 310 | +fnsaturating_is_zero(){ |
| 311 | +fnrequire_zero<T:Zero>(_:&T){} |
| 312 | +require_zero(&Saturating(42)); |
| 313 | +} |
| 314 | +#[test] |
| 315 | +#[cfg(has_num_saturating)] |
| 316 | +fnsaturating_is_one(){ |
| 317 | +fnrequire_one<T:One>(_:&T){} |
| 318 | +require_one(&Saturating(42)); |
| 319 | +} |