Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode variant index instead of variant value #593

Merged
merged 1 commit into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ description = "Implementation of #[derive(Encode, Decode)] for bincode"
proc-macro = true

[dependencies]
virtue = "0.0.9"
virtue = "0.0.10"
17 changes: 1 addition & 16 deletions derive/src/derive_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ impl DeriveEnum {
fn iter_fields(&self) -> EnumVariantIterator {
EnumVariantIterator {
idx: 0,
last_val: None,
variants: &self.variants,
}
}
Expand Down Expand Up @@ -401,7 +400,6 @@ impl DeriveEnum {
struct EnumVariantIterator<'a> {
variants: &'a [EnumVariant],
idx: usize,
last_val: Option<(Literal, u32)>,
}

impl<'a> Iterator for EnumVariantIterator<'a> {
Expand All @@ -412,20 +410,7 @@ impl<'a> Iterator for EnumVariantIterator<'a> {
let variant = self.variants.get(self.idx)?;
self.idx += 1;

let tokens = if let Fields::Integer(lit) = &variant.fields {
let tree = TokenTree::Literal(lit.clone());
self.last_val = Some((lit.clone(), 0));
vec![tree]
} else if let Some((lit, add)) = self.last_val.as_mut() {
*add += 1;
vec![
TokenTree::Literal(lit.clone()),
TokenTree::Punct(Punct::new('+', Spacing::Alone)),
TokenTree::Literal(Literal::u32_suffixed(*add)),
]
} else {
vec![TokenTree::Literal(Literal::u32_suffixed(idx as u32))]
};
let tokens = vec![TokenTree::Literal(Literal::u32_suffixed(idx as u32))];

Some((tokens, variant))
}
Expand Down
28 changes: 13 additions & 15 deletions tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ fn test_decode_borrowed_enum_tuple_variant() {

#[derive(bincode::Decode, bincode::Encode, PartialEq, Eq, Debug)]
enum CStyleEnum {
A = 1,
A = -1,
B = 2,
C,
D = 5,
Expand All @@ -272,11 +272,11 @@ fn test_c_style_enum() {
slice[0]
}

assert_eq!(ser(CStyleEnum::A), 1);
assert_eq!(ser(CStyleEnum::B), 2);
assert_eq!(ser(CStyleEnum::C), 3);
assert_eq!(ser(CStyleEnum::D), 5);
assert_eq!(ser(CStyleEnum::E), 6);
assert_eq!(ser(CStyleEnum::A), 0);
assert_eq!(ser(CStyleEnum::B), 1);
assert_eq!(ser(CStyleEnum::C), 2);
assert_eq!(ser(CStyleEnum::D), 3);
assert_eq!(ser(CStyleEnum::E), 4);

fn assert_de_successfully(num: u8, expected: CStyleEnum) {
match bincode::decode_from_slice::<CStyleEnum, _>(&[num], bincode::config::standard()) {
Expand All @@ -295,21 +295,19 @@ fn test_c_style_enum() {
}
Err(DecodeError::UnexpectedVariant {
type_name: "CStyleEnum",
allowed: &bincode::error::AllowedEnumVariants::Allowed(&[1, 2, 3, 5, 6]),
allowed: &bincode::error::AllowedEnumVariants::Allowed(&[0, 1, 2, 3, 4]),
found,
}) if found == num as u32 => {}
Err(e) => panic!("Expected DecodeError::UnexpectedVariant, got {e:?}"),
}
}

assert_de_fails(0);
assert_de_successfully(1, CStyleEnum::A);
assert_de_successfully(2, CStyleEnum::B);
assert_de_successfully(3, CStyleEnum::C);
assert_de_fails(4);
assert_de_successfully(5, CStyleEnum::D);
assert_de_successfully(6, CStyleEnum::E);
assert_de_fails(7);
assert_de_successfully(0, CStyleEnum::A);
assert_de_successfully(1, CStyleEnum::B);
assert_de_successfully(2, CStyleEnum::C);
assert_de_successfully(3, CStyleEnum::D);
assert_de_successfully(4, CStyleEnum::E);
assert_de_fails(5);
}

macro_rules! macro_newtype {
Expand Down
3 changes: 3 additions & 0 deletions tests/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ mod issue_547;

#[path = "issues/issue_570.rs"]
mod issue_570;

#[path = "issues/issue_592.rs"]
mod issue_592;
8 changes: 8 additions & 0 deletions tests/issues/issue_592.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![cfg(all(feature = "derive", feature = "std"))]

use bincode::{Decode, Encode};

#[derive(Encode, Decode)]
pub enum TypeOfFile {
Unknown = -1,
}