@@ -563,6 +563,29 @@ macro_rules! impl_integer_for_isize {
563
563
fn div_rem( & self , other: & Self ) -> ( Self , Self ) {
564
564
( * self / * other, * self % * other)
565
565
}
566
+
567
+ /// Rounds up to nearest multiple of argument.
568
+ #[ inline]
569
+ fn next_multiple_of( & self , other: & Self ) -> Self {
570
+ // Avoid the overflow of `MIN % -1`
571
+ if * other == -1 {
572
+ return * self ;
573
+ }
574
+
575
+ let m = Integer :: mod_floor( self , other) ;
576
+ * self + if m == 0 { 0 } else { other - m }
577
+ }
578
+
579
+ /// Rounds down to nearest multiple of argument.
580
+ #[ inline]
581
+ fn prev_multiple_of( & self , other: & Self ) -> Self {
582
+ // Avoid the overflow of `MIN % -1`
583
+ if * other == -1 {
584
+ return * self ;
585
+ }
586
+
587
+ * self - Integer :: mod_floor( self , other)
588
+ }
566
589
}
567
590
568
591
#[ cfg( test) ]
@@ -785,6 +808,16 @@ macro_rules! impl_integer_for_isize {
785
808
assert_eq!( ( 3 as $T) . is_odd( ) , true ) ;
786
809
assert_eq!( ( 4 as $T) . is_odd( ) , false ) ;
787
810
}
811
+
812
+ #[ test]
813
+ fn test_multiple_of_one_limits( ) {
814
+ for x in & [ <$T>:: min_value( ) , <$T>:: max_value( ) ] {
815
+ for one in & [ 1 , -1 ] {
816
+ assert_eq!( Integer :: next_multiple_of( x, one) , * x) ;
817
+ assert_eq!( Integer :: prev_multiple_of( x, one) , * x) ;
818
+ }
819
+ }
820
+ }
788
821
}
789
822
} ;
790
823
}
0 commit comments