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