@@ -33,13 +33,16 @@ impl<T: Int, const N: usize> Buffer for VariantEncoder<T, N> {
3333 }
3434}
3535
36- pub struct VariantDecoder < ' a , T : Int , const N : usize , const C_STYLE : bool > {
36+ pub struct VariantDecoder < ' a , T : Int , const N : usize , const HISTOGRAM : usize > {
3737 variants : CowSlice < ' a , T :: Une > ,
38- histogram : [ usize ; N ] , // Not required if C_STYLE. TODO don't reserve space for it.
38+ // `HISTOGRAM` is 0 for C style (fieldless) enums.
39+ histogram : [ usize ; HISTOGRAM ] ,
3940}
4041
4142// [(); N] doesn't implement Default.
42- impl < T : Int , const N : usize , const C_STYLE : bool > Default for VariantDecoder < ' _ , T , N , C_STYLE > {
43+ impl < T : Int , const N : usize , const HISTOGRAM : usize > Default
44+ for VariantDecoder < ' _ , T , N , HISTOGRAM >
45+ {
4346 fn default ( ) -> Self {
4447 Self {
4548 variants : Default :: default ( ) ,
@@ -48,15 +51,16 @@ impl<T: Int, const N: usize, const C_STYLE: bool> Default for VariantDecoder<'_,
4851 }
4952}
5053
51- // C style enums don't require length, so we can skip making a histogram for them.
52- impl < ' a , T : Int , const N : usize > VariantDecoder < ' a , T , N , false > {
54+ // C style enums (`HISTOGRAM` = 0) don't require length, so we
55+ // can skip making a histogram for them.
56+ impl < ' a , T : Int , const N : usize > VariantDecoder < ' a , T , N , N > {
5357 pub fn length ( & self , variant_index : u8 ) -> usize {
5458 self . histogram [ variant_index as usize ]
5559 }
5660}
5761
58- impl < ' a , T : Int + Into < usize > , const N : usize , const C_STYLE : bool > View < ' a >
59- for VariantDecoder < ' a , T , N , C_STYLE >
62+ impl < ' a , T : Int + Into < usize > , const N : usize , const HISTOGRAM : usize > View < ' a >
63+ for VariantDecoder < ' a , T , N , HISTOGRAM >
6064{
6165 fn populate ( & mut self , input : & mut & ' a [ u8 ] , length : usize ) -> Result < ( ) > {
6266 assert ! ( N >= 2 ) ;
@@ -86,18 +90,14 @@ impl<'a, T: Int + Into<usize>, const N: usize, const C_STYLE: bool> View<'a>
8690 check_less_than :: < T , N > ( unsafe { self . variants . as_slice ( length) } ) ?;
8791 } else {
8892 let out = self . variants . cast_mut :: < u8 > ( ) ;
89- if C_STYLE {
90- unpack_bytes_less_than :: < N , 0 > ( input, length, out) ?;
91- } else {
92- self . histogram = unpack_bytes_less_than :: < N , N > ( input, length, out) ?;
93- }
93+ self . histogram = unpack_bytes_less_than :: < N , HISTOGRAM > ( input, length, out) ?;
9494 }
9595 Ok ( ( ) )
9696 }
9797}
9898
99- impl < ' a , T : Int + Into < usize > , const N : usize , const C_STYLE : bool > Decoder < ' a , T >
100- for VariantDecoder < ' a , T , N , C_STYLE >
99+ impl < ' a , T : Int + Into < usize > , const N : usize , const HISTOGRAM : usize > Decoder < ' a , T >
100+ for VariantDecoder < ' a , T , N , HISTOGRAM >
101101{
102102 // Guaranteed to output numbers less than N.
103103 #[ inline( always) ]
0 commit comments