@@ -246,6 +246,8 @@ impl NumSize {
246
246
pub ( super ) const fn from_code ( id : u8 ) -> Self {
247
247
let code = id & 0x1F ;
248
248
NumSize ( match ( id & 0x20 ) / 0x20 {
249
+ 0 if id == Primitive :: BYTE . 0 => NumSizeInner :: Bytes ( 1 ) ,
250
+ 0 if id == Primitive :: F16B . 0 => NumSizeInner :: Bytes ( 2 ) ,
249
251
0 => NumSizeInner :: Bytes ( code) ,
250
252
1 => NumSizeInner :: Factored ( code) ,
251
253
_ => unreachable ! ( ) ,
@@ -262,7 +264,7 @@ impl NumSize {
262
264
pub const fn byte_size ( self ) -> u16 {
263
265
match self . 0 {
264
266
NumSizeInner :: Bytes ( bytes) => bytes as u16 ,
265
- NumSizeInner :: Factored ( factor) => 2 * ( factor as u16 + 1 ) ,
267
+ NumSizeInner :: Factored ( factor) => 16 * ( factor as u16 + 2 ) ,
266
268
}
267
269
}
268
270
}
@@ -307,8 +309,63 @@ mod test {
307
309
use crate :: Primitive ;
308
310
309
311
#[ test]
310
- fn u8 ( ) {
311
- let prim = Primitive :: U8 ;
312
- assert_eq ! ( prim. byte_size( ) , 1 ) ;
312
+ fn unsigned_byte_size ( ) {
313
+ assert_eq ! ( Primitive :: U8 . byte_size( ) , 1 ) ;
314
+ assert_eq ! ( Primitive :: U16 . byte_size( ) , 2 ) ;
315
+ assert_eq ! ( Primitive :: U24 . byte_size( ) , 3 ) ;
316
+ assert_eq ! ( Primitive :: U32 . byte_size( ) , 4 ) ;
317
+ assert_eq ! ( Primitive :: U40 . byte_size( ) , 5 ) ;
318
+ assert_eq ! ( Primitive :: U48 . byte_size( ) , 6 ) ;
319
+ assert_eq ! ( Primitive :: U56 . byte_size( ) , 7 ) ;
320
+ assert_eq ! ( Primitive :: U64 . byte_size( ) , 8 ) ;
321
+ assert_eq ! ( Primitive :: U128 . byte_size( ) , 16 ) ;
322
+ assert_eq ! ( Primitive :: U160 . byte_size( ) , 20 ) ;
323
+ assert_eq ! ( Primitive :: U256 . byte_size( ) , 32 ) ;
324
+ assert_eq ! ( Primitive :: U512 . byte_size( ) , 64 ) ;
325
+ assert_eq ! ( Primitive :: U1024 . byte_size( ) , 128 ) ;
326
+ }
327
+
328
+ #[ test]
329
+ fn signed_byte_size ( ) {
330
+ assert_eq ! ( Primitive :: I8 . byte_size( ) , 1 ) ;
331
+ assert_eq ! ( Primitive :: I16 . byte_size( ) , 2 ) ;
332
+ assert_eq ! ( Primitive :: I24 . byte_size( ) , 3 ) ;
333
+ assert_eq ! ( Primitive :: I32 . byte_size( ) , 4 ) ;
334
+ assert_eq ! ( Primitive :: I40 . byte_size( ) , 5 ) ;
335
+ assert_eq ! ( Primitive :: I48 . byte_size( ) , 6 ) ;
336
+ assert_eq ! ( Primitive :: I56 . byte_size( ) , 7 ) ;
337
+ assert_eq ! ( Primitive :: I64 . byte_size( ) , 8 ) ;
338
+ assert_eq ! ( Primitive :: I128 . byte_size( ) , 16 ) ;
339
+ assert_eq ! ( Primitive :: I256 . byte_size( ) , 32 ) ;
340
+ assert_eq ! ( Primitive :: I512 . byte_size( ) , 64 ) ;
341
+ assert_eq ! ( Primitive :: I1024 . byte_size( ) , 128 ) ;
342
+ }
343
+
344
+ #[ test]
345
+ fn nonzero_byte_size ( ) {
346
+ assert_eq ! ( Primitive :: N8 . byte_size( ) , 1 ) ;
347
+ assert_eq ! ( Primitive :: N16 . byte_size( ) , 2 ) ;
348
+ assert_eq ! ( Primitive :: N24 . byte_size( ) , 3 ) ;
349
+ assert_eq ! ( Primitive :: N32 . byte_size( ) , 4 ) ;
350
+ assert_eq ! ( Primitive :: N48 . byte_size( ) , 6 ) ;
351
+ assert_eq ! ( Primitive :: N64 . byte_size( ) , 8 ) ;
352
+ assert_eq ! ( Primitive :: N128 . byte_size( ) , 16 ) ;
353
+ }
354
+
355
+ #[ test]
356
+ fn float_byte_size ( ) {
357
+ assert_eq ! ( Primitive :: F16 . byte_size( ) , 2 ) ;
358
+ assert_eq ! ( Primitive :: F16B . byte_size( ) , 2 ) ;
359
+ assert_eq ! ( Primitive :: F32 . byte_size( ) , 4 ) ;
360
+ assert_eq ! ( Primitive :: F64 . byte_size( ) , 8 ) ;
361
+ assert_eq ! ( Primitive :: F80 . byte_size( ) , 10 ) ;
362
+ assert_eq ! ( Primitive :: F128 . byte_size( ) , 16 ) ;
363
+ assert_eq ! ( Primitive :: F256 . byte_size( ) , 32 ) ;
364
+ }
365
+
366
+ #[ test]
367
+ fn spec_byte_size ( ) {
368
+ assert_eq ! ( Primitive :: UNIT . byte_size( ) , 0 ) ;
369
+ assert_eq ! ( Primitive :: BYTE . byte_size( ) , 1 ) ;
313
370
}
314
371
}
0 commit comments