@@ -1482,9 +1482,19 @@ impl SubAssign<Duration> for StdDuration {
14821482 }
14831483}
14841484
1485- /// Implement `Mul` (reflexively) and `Div` for `Duration` for various types.
1485+ /// Given a value and whether it is signed, cast it to the signed version.
1486+ macro_rules! cast_signed {
1487+ ( @signed $val: ident) => {
1488+ $val
1489+ } ;
1490+ ( @unsigned $val: ident) => {
1491+ $val. cast_signed( )
1492+ } ;
1493+ }
1494+
1495+ /// Implement `Mul` (reflexively) and `Div` for `Duration` for various signed types.
14861496macro_rules! duration_mul_div_int {
1487- ( $( $ type: ty) ,+) => { $(
1497+ ( $( @$signedness : ident $ type: ty) ,+ $ ( , ) ? ) => { $(
14881498 impl Mul <$type> for Duration {
14891499 type Output = Self ;
14901500
@@ -1493,7 +1503,7 @@ macro_rules! duration_mul_div_int {
14931503 fn mul( self , rhs: $type) -> Self :: Output {
14941504 Self :: nanoseconds_i128(
14951505 self . whole_nanoseconds( )
1496- . checked_mul( rhs . cast_signed( ) . extend:: <i128 >( ) )
1506+ . checked_mul( cast_signed! ( @$signedness rhs ) . extend:: <i128 >( ) )
14971507 . expect( "overflow when multiplying duration" )
14981508 )
14991509 }
@@ -1516,13 +1526,21 @@ macro_rules! duration_mul_div_int {
15161526 #[ track_caller]
15171527 fn div( self , rhs: $type) -> Self :: Output {
15181528 Self :: nanoseconds_i128(
1519- self . whole_nanoseconds( ) / rhs . cast_signed( ) . extend:: <i128 >( )
1529+ self . whole_nanoseconds( ) / cast_signed! ( @$signedness rhs ) . extend:: <i128 >( )
15201530 )
15211531 }
15221532 }
15231533 ) +} ;
15241534}
1525- duration_mul_div_int ! [ i8 , i16 , i32 , u8 , u16 , u32 ] ;
1535+
1536+ duration_mul_div_int ! {
1537+ @signed i8 ,
1538+ @signed i16 ,
1539+ @signed i32 ,
1540+ @unsigned u8 ,
1541+ @unsigned u16 ,
1542+ @unsigned u32 ,
1543+ }
15261544
15271545impl Mul < f32 > for Duration {
15281546 type Output = Self ;
0 commit comments