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

Bump virtue 0.0.4 #463

Merged
merged 2 commits into from
Jan 8, 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 @@ -16,4 +16,4 @@ description = "Implementation of #[derive(Encode, Decode)] for bincode"
proc-macro = true

[dependencies]
virtue = "0.0.3"
virtue = "0.0.4"
6 changes: 3 additions & 3 deletions derive/src/derive_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl DeriveEnum {
body.punct(';');
// If we have any fields, encode them all one by one
for field_name in variant.fields.names() {
if field_name.has_attribute(FieldAttribute::WithSerde)? {
if field_name.attributes().has_attribute(FieldAttribute::WithSerde)? {
body.push_parsed(format!(
"bincode::enc::Encode::encode(&bincode::serde::Compat({}), &mut encoder)?;",
field_name.to_string_with_prefix(TUPLE_FIELD_PREFIX),
Expand Down Expand Up @@ -228,7 +228,7 @@ impl DeriveEnum {
variant_body.ident(field.unwrap_ident().clone());
}
variant_body.punct(':');
if field.has_attribute(FieldAttribute::WithSerde)? {
if field.attributes().has_attribute(FieldAttribute::WithSerde)? {
variant_body
.push_parsed("<bincode::serde::Compat<_> as bincode::Decode>::decode(&mut decoder)?.0,")?;
} else {
Expand Down Expand Up @@ -298,7 +298,7 @@ impl DeriveEnum {
variant_body.ident(field.unwrap_ident().clone());
}
variant_body.punct(':');
if field.has_attribute(FieldAttribute::WithSerde)? {
if field.attributes().has_attribute(FieldAttribute::WithSerde)? {
variant_body
.push_parsed("<bincode::serde::BorrowCompat<_> as bincode::BorrowDecode>::borrow_decode(&mut decoder)?.0,")?;
} else {
Expand Down
6 changes: 3 additions & 3 deletions derive/src/derive_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl DeriveStruct {
.with_return_type("core::result::Result<(), bincode::error::EncodeError>")
.body(|fn_body| {
for field in fields.names() {
if field.has_attribute(FieldAttribute::WithSerde)? {
if field.attributes().has_attribute(FieldAttribute::WithSerde)? {
fn_body
.push_parsed(format!(
"bincode::Encode::encode(&bincode::serde::Compat(&self.{}), &mut encoder)?;",
Expand Down Expand Up @@ -73,7 +73,7 @@ impl DeriveStruct {
// ...
// }
for field in fields.names() {
if field.has_attribute(FieldAttribute::WithSerde)? {
if field.attributes().has_attribute(FieldAttribute::WithSerde)? {
struct_body
.push_parsed(format!(
"{}: (<bincode::serde::Compat<_> as bincode::Decode>::decode(&mut decoder)?).0,",
Expand Down Expand Up @@ -118,7 +118,7 @@ impl DeriveStruct {
ok_group.ident_str("Self");
ok_group.group(Delimiter::Brace, |struct_body| {
for field in fields.names() {
if field.has_attribute(FieldAttribute::WithSerde)? {
if field.attributes().has_attribute(FieldAttribute::WithSerde)? {
struct_body
.push_parsed(format!(
"{}: (<bincode::serde::BorrowCompat<_> as bincode::de::BorrowDecode>::borrow_decode(&mut decoder)?).0,",
Expand Down
37 changes: 37 additions & 0 deletions tests/derive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(feature = "derive")]

use bincode::config::Configuration;

#[derive(bincode::Encode, PartialEq, Debug)]
pub(crate) struct Test<T> {
a: T,
Expand Down Expand Up @@ -214,3 +215,39 @@ fn test_c_style_enum() {
assert_eq!(de(6).unwrap(), CStyleEnum::E);
assert_eq!(de(7), expected_err(7));
}

macro_rules! macro_newtype {
($name:ident) => {
#[derive(bincode::Encode, bincode::Decode, PartialEq, Eq, Debug)]
pub struct $name(pub usize);
};
}
macro_newtype!(MacroNewType);

#[test]
fn test_macro_newtype() {
for val in [0, 100, usize::MAX] {
let mut usize_slice = [0u8; 10];
let usize_len =
bincode::encode_into_slice(val, &mut usize_slice, Configuration::standard()).unwrap();

let mut newtype_slice = [0u8; 10];
let newtype_len = bincode::encode_into_slice(
MacroNewType(val),
&mut newtype_slice,
Configuration::standard(),
)
.unwrap();

assert_eq!(usize_len, newtype_len);
assert_eq!(usize_slice, newtype_slice);

let (newtype, len) = bincode::decode_from_slice::<MacroNewType, _>(
&newtype_slice,
Configuration::standard(),
)
.unwrap();
assert_eq!(newtype, MacroNewType(val));
assert_eq!(len, newtype_len);
}
}