@@ -232,10 +232,7 @@ impl Size {
232232 Size :: from_bytes ( bits / 8 + ( ( bits % 8 ) + 7 ) / 8 )
233233 }
234234
235- pub fn from_bytes ( bytes : u64 ) -> Size {
236- if bytes >= ( 1 << 61 ) {
237- panic ! ( "Size::from_bytes: {} bytes in bits doesn't fit in u64" , bytes)
238- }
235+ pub const fn from_bytes ( bytes : u64 ) -> Size {
239236 Size {
240237 raw : bytes
241238 }
@@ -246,7 +243,9 @@ impl Size {
246243 }
247244
248245 pub fn bits ( self ) -> u64 {
249- self . bytes ( ) * 8
246+ self . bytes ( ) . checked_mul ( 8 ) . unwrap_or_else ( || {
247+ panic ! ( "Size::bits: {} bytes in bits doesn't fit in u64" , self . bytes( ) )
248+ } )
250249 }
251250
252251 pub fn abi_align ( self , align : Align ) -> Size {
@@ -262,9 +261,7 @@ impl Size {
262261 pub fn checked_add < C : HasDataLayout > ( self , offset : Size , cx : C ) -> Option < Size > {
263262 let dl = cx. data_layout ( ) ;
264263
265- // Each Size is less than dl.obj_size_bound(), so the sum is
266- // also less than 1 << 62 (and therefore can't overflow).
267- let bytes = self . bytes ( ) + offset. bytes ( ) ;
264+ let bytes = self . bytes ( ) . checked_add ( offset. bytes ( ) ) ?;
268265
269266 if bytes < dl. obj_size_bound ( ) {
270267 Some ( Size :: from_bytes ( bytes) )
@@ -276,11 +273,11 @@ impl Size {
276273 pub fn checked_mul < C : HasDataLayout > ( self , count : u64 , cx : C ) -> Option < Size > {
277274 let dl = cx. data_layout ( ) ;
278275
279- match self . bytes ( ) . checked_mul ( count) {
280- Some ( bytes ) if bytes < dl. obj_size_bound ( ) => {
281- Some ( Size :: from_bytes ( bytes) )
282- }
283- _ => None
276+ let bytes = self . bytes ( ) . checked_mul ( count) ? ;
277+ if bytes < dl. obj_size_bound ( ) {
278+ Some ( Size :: from_bytes ( bytes) )
279+ } else {
280+ None
284281 }
285282 }
286283}
@@ -291,19 +288,25 @@ impl Size {
291288impl Add for Size {
292289 type Output = Size ;
293290 fn add ( self , other : Size ) -> Size {
294- // Each Size is less than 1 << 61, so the sum is
295- // less than 1 << 62 (and therefore can 't overflow).
296- Size :: from_bytes ( self . bytes ( ) + other . bytes ( ) )
291+ Size :: from_bytes ( self . bytes ( ) . checked_add ( other . bytes ( ) ) . unwrap_or_else ( || {
292+ panic ! ( "Size::add: {} + {} doesn 't fit in u64" , self . bytes ( ) , other . bytes ( ) )
293+ } ) )
297294 }
298295}
299296
300297impl Sub for Size {
301298 type Output = Size ;
302299 fn sub ( self , other : Size ) -> Size {
303- // Each Size is less than 1 << 61, so an underflow
304- // would result in a value larger than 1 << 61,
305- // which Size::from_bytes will catch for us.
306- Size :: from_bytes ( self . bytes ( ) - other. bytes ( ) )
300+ Size :: from_bytes ( self . bytes ( ) . checked_sub ( other. bytes ( ) ) . unwrap_or_else ( || {
301+ panic ! ( "Size::sub: {} - {} would result in negative size" , self . bytes( ) , other. bytes( ) )
302+ } ) )
303+ }
304+ }
305+
306+ impl Mul < Size > for u64 {
307+ type Output = Size ;
308+ fn mul ( self , size : Size ) -> Size {
309+ size * self
307310 }
308311}
309312
0 commit comments