@@ -103,7 +103,7 @@ pub enum AssetState {
103103}
104104
105105/// Capacity of an asset, which may be continuous or a discrete number of indivisible units
106- #[ derive( Clone , PartialEq , Copy ) ]
106+ #[ derive( Clone , PartialEq , Copy , Debug ) ]
107107pub enum AssetCapacity {
108108 /// Continuous capacity
109109 Continuous ( Capacity ) ,
@@ -115,6 +115,7 @@ pub enum AssetCapacity {
115115impl Add for AssetCapacity {
116116 type Output = Self ;
117117
118+ // Add two AssetCapacity values together
118119 fn add ( self , rhs : AssetCapacity ) -> Self {
119120 match ( self , rhs) {
120121 ( AssetCapacity :: Continuous ( cap1) , AssetCapacity :: Continuous ( cap2) ) => {
@@ -124,24 +125,25 @@ impl Add for AssetCapacity {
124125 Self :: check_same_unit_size ( size1, size2) ;
125126 AssetCapacity :: Discrete ( units1 + units2, size1)
126127 }
127- _ => panic ! ( "Cannot add different types of AssetCapacity" ) ,
128+ _ => panic ! ( "Cannot add different types of AssetCapacity ({self:?} and {rhs:?}) " ) ,
128129 }
129130 }
130131}
131132
132133impl Sub for AssetCapacity {
133134 type Output = Self ;
134135
136+ // Subtract rhs from self, ensuring that the result is non-negative
135137 fn sub ( self , rhs : AssetCapacity ) -> Self {
136138 match ( self , rhs) {
137139 ( AssetCapacity :: Continuous ( cap1) , AssetCapacity :: Continuous ( cap2) ) => {
138- AssetCapacity :: Continuous ( cap1 - cap2)
140+ AssetCapacity :: Continuous ( ( cap1 - cap2) . max ( Capacity ( 0.0 ) ) )
139141 }
140142 ( AssetCapacity :: Discrete ( units1, size1) , AssetCapacity :: Discrete ( units2, size2) ) => {
141143 Self :: check_same_unit_size ( size1, size2) ;
142- AssetCapacity :: Discrete ( units1 - units2, size1)
144+ AssetCapacity :: Discrete ( units1 - units2. min ( units1 ) , size1)
143145 }
144- _ => panic ! ( "Cannot subtract different types of AssetCapacity" ) ,
146+ _ => panic ! ( "Cannot subtract different types of AssetCapacity ({self:?} and {rhs:?}) " ) ,
145147 }
146148 }
147149}
@@ -221,7 +223,7 @@ impl AssetCapacity {
221223 Self :: check_same_unit_size ( size1, size2) ;
222224 AssetCapacity :: Discrete ( min ( units1, units2) , size1)
223225 }
224- _ => panic ! ( "Cannot compare different types of AssetCapacity" ) ,
226+ _ => panic ! ( "Cannot compare different types of AssetCapacity ({self:?} and {rhs:?}) " ) ,
225227 }
226228 }
227229
@@ -235,7 +237,7 @@ impl AssetCapacity {
235237 Self :: check_same_unit_size ( size1, size2) ;
236238 AssetCapacity :: Discrete ( max ( units1, units2) , size1)
237239 }
238- _ => panic ! ( "Cannot compare different types of AssetCapacity" ) ,
240+ _ => panic ! ( "Cannot compare different types of AssetCapacity ({self:?} and {rhs:?}) " ) ,
239241 }
240242 }
241243}
@@ -888,7 +890,7 @@ impl Asset {
888890 "increase_capacity can only be called on Candidate assets"
889891 ) ;
890892 assert ! (
891- capacity. total_capacity( ) > Capacity :: EPSILON ,
893+ capacity. total_capacity( ) > Capacity ( 0.0 ) ,
892894 "Capacity increase must be positive"
893895 ) ;
894896 self . capacity = self . capacity + capacity;
0 commit comments