Skip to content

Commit 5260315

Browse files
committed
Remove histogram while decoding C-style enums.
1 parent c0f99c0 commit 5260315

4 files changed

Lines changed: 22 additions & 17 deletions

File tree

bitcode_derive/src/decode.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,12 @@ impl crate::shared::Item for Item {
127127
.then(|| {
128128
let private = private(crate_name);
129129
let c_style = inners.is_empty();
130-
quote! { variants: #private::VariantDecoder<#de, #variant_index, #variant_count, #c_style>, }
130+
let histogram = if c_style {
131+
0
132+
} else {
133+
variant_count
134+
};
135+
quote! { variants: #private::VariantDecoder<#de, #variant_index, #variant_count, #histogram>, }
131136
})
132137
.unwrap_or_default();
133138
quote! {

src/derive/option.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<T: Encode> Buffer for OptionEncoder<T> {
8686
}
8787

8888
pub struct OptionDecoder<'a, T: Decode<'a>> {
89-
variants: VariantDecoder<'a, u8, 2, false>,
89+
variants: VariantDecoder<'a, u8, 2, 2>,
9090
some: T::Decoder,
9191
}
9292

src/derive/result.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<T: Encode, E: Encode> Buffer for ResultEncoder<T, E> {
5555
}
5656

5757
pub struct ResultDecoder<'a, T: Decode<'a>, E: Decode<'a>> {
58-
variants: VariantDecoder<'a, u8, 2, false>,
58+
variants: VariantDecoder<'a, u8, 2, 2>,
5959
ok: T::Decoder,
6060
err: E::Decoder,
6161
}

src/derive/variant.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)