11use super :: bytes_format:: { self , BytesFormat } ;
2- use derive_more:: { Add , AddAssign , From , Into , Sum } ;
2+ use derive_more:: { Add , AddAssign , From , Into , Sub , SubAssign , Sum } ;
33use std:: {
44 fmt:: { Debug , Display } ,
55 iter:: Sum ,
6- ops:: { Add , AddAssign , Mul , MulAssign } ,
6+ ops:: { Add , AddAssign , Mul , MulAssign , Sub , SubAssign } ,
77} ;
88
99#[ cfg( feature = "json" ) ]
1010use serde:: { Deserialize , Serialize } ;
1111
12+ mod mul_traits {
13+ use std:: ops:: { Mul , MulAssign } ;
14+ pub trait MulAssignEx < Rhs > : Mul < Rhs , Output = Self > + MulAssign < Rhs > + Sized { }
15+ impl < Lhs : Mul < Rhs , Output = Lhs > + MulAssign < Rhs > , Rhs > MulAssignEx < Rhs > for Lhs { }
16+ }
17+ use mul_traits:: MulAssignEx ;
18+
1219/// Types whose values can be used as disk usage statistic.
1320pub trait Size :
1421 Debug
@@ -21,7 +28,13 @@ pub trait Size:
2128 + Ord
2229 + Add < Output = Self >
2330 + AddAssign
31+ + Sub < Output = Self >
32+ + SubAssign
2433 + Sum
34+ + MulAssignEx < u8 >
35+ + MulAssignEx < u16 >
36+ + MulAssignEx < u32 >
37+ + MulAssignEx < u64 >
2538{
2639 /// Underlying type
2740 type Inner : From < Self > + Into < Self > + Mul < Self , Output = Self > ;
@@ -33,14 +46,44 @@ pub trait Size:
3346 fn display ( self , input : Self :: DisplayFormat ) -> Self :: DisplayOutput ;
3447}
3548
49+ macro_rules! impl_mul {
50+ ( $name: ident: $inner: ident *= $( $num_type: ident) +) => {
51+ $(
52+ impl Mul <$num_type> for $name {
53+ type Output = Self ;
54+ fn mul( self , rhs: $num_type) -> Self :: Output {
55+ self . 0 . mul( rhs as $inner) . into( )
56+ }
57+ }
58+
59+ impl Mul <$name> for $num_type {
60+ type Output = $name;
61+ fn mul( self , rhs: $name) -> Self :: Output {
62+ rhs * self
63+ }
64+ }
65+
66+ impl MulAssign <$num_type> for $name {
67+ fn mul_assign( & mut self , rhs: $num_type) {
68+ self . 0 *= rhs as $inner;
69+ }
70+ }
71+ ) +
72+ } ;
73+
74+ ( $name: ident: u64 ) => {
75+ impl_mul!( $name: u64 *= u8 u16 u32 u64 ) ;
76+ } ;
77+ }
78+
3679macro_rules! newtype {
3780 (
3881 $( #[ $attribute: meta] ) *
3982 $name: ident = $inner: ty;
4083 display: ( $display_format: ty) -> $display_output: ty = $display_impl: expr;
4184 ) => {
4285 #[ derive( Debug , Default , Clone , Copy , PartialEq , Eq , PartialOrd , Ord ) ]
43- #[ derive( From , Into , Add , AddAssign , Sum ) ]
86+ #[ derive( From , Into , Add , AddAssign , Sub , SubAssign , Sum ) ]
4487 #[ cfg_attr( feature = "json" , derive( Deserialize , Serialize ) ) ]
4588 $( #[ $attribute] ) *
4689 pub struct $name( $inner) ;
@@ -65,25 +108,7 @@ macro_rules! newtype {
65108 }
66109 }
67110
68- impl Mul <$inner> for $name {
69- type Output = Self ;
70- fn mul( self , rhs: $inner) -> Self :: Output {
71- self . 0 . mul( rhs) . into( )
72- }
73- }
74-
75- impl Mul <$name> for $inner {
76- type Output = $name;
77- fn mul( self , rhs: $name) -> Self :: Output {
78- rhs * self
79- }
80- }
81-
82- impl MulAssign <$inner> for $name {
83- fn mul_assign( & mut self , rhs: $inner) {
84- self . 0 *= rhs;
85- }
86- }
111+ impl_mul!( $name: u64 ) ;
87112 } ;
88113}
89114
0 commit comments