@@ -30,9 +30,9 @@ impl RealHandle {
30
30
}
31
31
32
32
fn packed_disc_size ( ) -> u32 {
33
- ( variant_count :: < Self > ( ) . log2 ( ) + 1 )
34
- . try_into ( )
35
- . expect ( "this would require more than 2^4294967294 variants to overflow" )
33
+ // log2(x ) + 1 is how many bits it takes to store x
34
+ // because the discriminants start at 1, the variant count is equal to the highest discriminant
35
+ variant_count :: < Self > ( ) . log2 ( ) + 1
36
36
}
37
37
38
38
/// This function packs the discriminant and data values into a 31-bit space.
@@ -45,10 +45,14 @@ impl RealHandle {
45
45
let discriminant = self . discriminant ( ) ;
46
46
let data = self . data ( ) ;
47
47
48
- // these assertions ensure the components avoid overlapping eachother and the sign bit
48
+ // make sure the discriminant fits into `disc_size` bits
49
49
assert ! ( discriminant < 2u32 . pow( disc_size) ) ;
50
+
51
+ // make sure the data fits into `data_size` bits
50
52
assert ! ( data < 2u32 . pow( data_size) ) ;
51
53
54
+ // packs the data into the lower `data_size` bits
55
+ // and packs the discriminant right above the data
52
56
( discriminant << data_size | data) as i32
53
57
}
54
58
@@ -69,7 +73,10 @@ impl RealHandle {
69
73
// the lower `data_size` bits of this mask are 1
70
74
let data_mask = 2u32 . pow ( data_size) - 1 ;
71
75
76
+ // the discriminant is stored right above the lower `data_size` bits
72
77
let discriminant = handle_bits >> data_size;
78
+
79
+ // the data is stored in the lower `data_size` bits
73
80
let data = handle_bits & data_mask;
74
81
75
82
Self :: new ( discriminant, data)
0 commit comments