Skip to content

Commit

Permalink
Bump virtue 0.0.4 (#463)
Browse files Browse the repository at this point in the history
* Updated breaking virtue changes in preperation of virtue 0.0.4 release

* Added contents of derive.rs test_macro_newtype
  • Loading branch information
VictorKoenders committed Jan 8, 2022
1 parent 8d6dc0a commit db398ec
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion derive/Cargo.toml
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
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
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
@@ -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);
}
}

0 comments on commit db398ec

Please sign in to comment.