File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change 11//! Definitions of integer that is known not to equal zero.
22
33use crate :: fmt;
4- use crate :: ops:: { BitOr , BitOrAssign , Div } ;
4+ use crate :: ops:: { BitOr , BitOrAssign , Div , Rem } ;
55use crate :: str:: FromStr ;
66
77use super :: from_str_radix;
@@ -279,6 +279,18 @@ macro_rules! nonzero_integers_div {
279279 unsafe { crate :: intrinsics:: unchecked_div( self , other. get( ) ) }
280280 }
281281 }
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+ }
282294 ) +
283295 }
284296}
Original file line number Diff line number Diff line change @@ -320,3 +320,11 @@ fn test_nonzero_uint_div() {
320320 let x: u32 = 42u32 / nz;
321321 assert_eq ! ( x, 42u32 ) ;
322322}
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