File tree 2 files changed +21
-1
lines changed
2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change 1
1
//! Definitions of integer that is known not to equal zero.
2
2
3
3
use crate :: fmt;
4
- use crate :: ops:: { BitOr , BitOrAssign , Div } ;
4
+ use crate :: ops:: { BitOr , BitOrAssign , Div , Rem } ;
5
5
use crate :: str:: FromStr ;
6
6
7
7
use super :: from_str_radix;
@@ -279,6 +279,18 @@ macro_rules! nonzero_integers_div {
279
279
unsafe { crate :: intrinsics:: unchecked_div( self , other. get( ) ) }
280
280
}
281
281
}
282
+
283
+ #[ stable( feature = "nonzero_div" , since = "1.50.0" ) ]
284
+ impl Rem <$Ty> for $Int {
285
+ type Output = $Int;
286
+ /// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic.
287
+ #[ inline]
288
+ fn rem( self , other: $Ty) -> $Int {
289
+ // SAFETY: rem by zero is checked because `other` is a nonzero,
290
+ // and MIN/-1 is checked because `self` is an unsigned int.
291
+ unsafe { crate :: intrinsics:: unchecked_rem( self , other. get( ) ) }
292
+ }
293
+ }
282
294
) +
283
295
}
284
296
}
Original file line number Diff line number Diff line change @@ -320,3 +320,11 @@ fn test_nonzero_uint_div() {
320
320
let x: u32 = 42u32 / nz;
321
321
assert_eq ! ( x, 42u32 ) ;
322
322
}
323
+
324
+ #[ test]
325
+ fn test_nonzero_uint_rem ( ) {
326
+ let nz = NonZeroU32 :: new ( 10 ) . unwrap ( ) ;
327
+
328
+ let x: u32 = 42u32 % nz;
329
+ assert_eq ! ( x, 2u32 ) ;
330
+ }
You can’t perform that action at this time.
0 commit comments