@@ -30,9 +30,9 @@ impl RealHandle {
3030 }
3131
3232 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
3636 }
3737
3838 /// This function packs the discriminant and data values into a 31-bit space.
@@ -45,10 +45,14 @@ impl RealHandle {
4545 let discriminant = self . discriminant ( ) ;
4646 let data = self . data ( ) ;
4747
48- // these assertions ensure the components avoid overlapping eachother and the sign bit
48+ // make sure the discriminant fits into `disc_size` bits
4949 assert ! ( discriminant < 2u32 . pow( disc_size) ) ;
50+
51+ // make sure the data fits into `data_size` bits
5052 assert ! ( data < 2u32 . pow( data_size) ) ;
5153
54+ // packs the data into the lower `data_size` bits
55+ // and packs the discriminant right above the data
5256 ( discriminant << data_size | data) as i32
5357 }
5458
@@ -69,7 +73,10 @@ impl RealHandle {
6973 // the lower `data_size` bits of this mask are 1
7074 let data_mask = 2u32 . pow ( data_size) - 1 ;
7175
76+ // the discriminant is stored right above the lower `data_size` bits
7277 let discriminant = handle_bits >> data_size;
78+
79+ // the data is stored in the lower `data_size` bits
7380 let data = handle_bits & data_mask;
7481
7582 Self :: new ( discriminant, data)
0 commit comments