mirror of https://git.sr.ht/~stygianentity/bincode
Bump virtue 0.0.4 (#463)
* Updated breaking virtue changes in preperation of virtue 0.0.4 release * Added contents of derive.rs test_macro_newtype
This commit is contained in:
parent
5e9186ea8f
commit
030905e7d5
|
|
@ -16,4 +16,4 @@ description = "Implementation of #[derive(Encode, Decode)] for bincode"
|
||||||
proc-macro = true
|
proc-macro = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
virtue = "0.0.3"
|
virtue = "0.0.4"
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ impl DeriveEnum {
|
||||||
body.punct(';');
|
body.punct(';');
|
||||||
// If we have any fields, encode them all one by one
|
// If we have any fields, encode them all one by one
|
||||||
for field_name in variant.fields.names() {
|
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!(
|
body.push_parsed(format!(
|
||||||
"bincode::enc::Encode::encode(&bincode::serde::Compat({}), &mut encoder)?;",
|
"bincode::enc::Encode::encode(&bincode::serde::Compat({}), &mut encoder)?;",
|
||||||
field_name.to_string_with_prefix(TUPLE_FIELD_PREFIX),
|
field_name.to_string_with_prefix(TUPLE_FIELD_PREFIX),
|
||||||
|
|
@ -228,7 +228,7 @@ impl DeriveEnum {
|
||||||
variant_body.ident(field.unwrap_ident().clone());
|
variant_body.ident(field.unwrap_ident().clone());
|
||||||
}
|
}
|
||||||
variant_body.punct(':');
|
variant_body.punct(':');
|
||||||
if field.has_attribute(FieldAttribute::WithSerde)? {
|
if field.attributes().has_attribute(FieldAttribute::WithSerde)? {
|
||||||
variant_body
|
variant_body
|
||||||
.push_parsed("<bincode::serde::Compat<_> as bincode::Decode>::decode(&mut decoder)?.0,")?;
|
.push_parsed("<bincode::serde::Compat<_> as bincode::Decode>::decode(&mut decoder)?.0,")?;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -298,7 +298,7 @@ impl DeriveEnum {
|
||||||
variant_body.ident(field.unwrap_ident().clone());
|
variant_body.ident(field.unwrap_ident().clone());
|
||||||
}
|
}
|
||||||
variant_body.punct(':');
|
variant_body.punct(':');
|
||||||
if field.has_attribute(FieldAttribute::WithSerde)? {
|
if field.attributes().has_attribute(FieldAttribute::WithSerde)? {
|
||||||
variant_body
|
variant_body
|
||||||
.push_parsed("<bincode::serde::BorrowCompat<_> as bincode::BorrowDecode>::borrow_decode(&mut decoder)?.0,")?;
|
.push_parsed("<bincode::serde::BorrowCompat<_> as bincode::BorrowDecode>::borrow_decode(&mut decoder)?.0,")?;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ impl DeriveStruct {
|
||||||
.with_return_type("core::result::Result<(), bincode::error::EncodeError>")
|
.with_return_type("core::result::Result<(), bincode::error::EncodeError>")
|
||||||
.body(|fn_body| {
|
.body(|fn_body| {
|
||||||
for field in fields.names() {
|
for field in fields.names() {
|
||||||
if field.has_attribute(FieldAttribute::WithSerde)? {
|
if field.attributes().has_attribute(FieldAttribute::WithSerde)? {
|
||||||
fn_body
|
fn_body
|
||||||
.push_parsed(format!(
|
.push_parsed(format!(
|
||||||
"bincode::Encode::encode(&bincode::serde::Compat(&self.{}), &mut encoder)?;",
|
"bincode::Encode::encode(&bincode::serde::Compat(&self.{}), &mut encoder)?;",
|
||||||
|
|
@ -73,7 +73,7 @@ impl DeriveStruct {
|
||||||
// ...
|
// ...
|
||||||
// }
|
// }
|
||||||
for field in fields.names() {
|
for field in fields.names() {
|
||||||
if field.has_attribute(FieldAttribute::WithSerde)? {
|
if field.attributes().has_attribute(FieldAttribute::WithSerde)? {
|
||||||
struct_body
|
struct_body
|
||||||
.push_parsed(format!(
|
.push_parsed(format!(
|
||||||
"{}: (<bincode::serde::Compat<_> as bincode::Decode>::decode(&mut decoder)?).0,",
|
"{}: (<bincode::serde::Compat<_> as bincode::Decode>::decode(&mut decoder)?).0,",
|
||||||
|
|
@ -118,7 +118,7 @@ impl DeriveStruct {
|
||||||
ok_group.ident_str("Self");
|
ok_group.ident_str("Self");
|
||||||
ok_group.group(Delimiter::Brace, |struct_body| {
|
ok_group.group(Delimiter::Brace, |struct_body| {
|
||||||
for field in fields.names() {
|
for field in fields.names() {
|
||||||
if field.has_attribute(FieldAttribute::WithSerde)? {
|
if field.attributes().has_attribute(FieldAttribute::WithSerde)? {
|
||||||
struct_body
|
struct_body
|
||||||
.push_parsed(format!(
|
.push_parsed(format!(
|
||||||
"{}: (<bincode::serde::BorrowCompat<_> as bincode::de::BorrowDecode>::borrow_decode(&mut decoder)?).0,",
|
"{}: (<bincode::serde::BorrowCompat<_> as bincode::de::BorrowDecode>::borrow_decode(&mut decoder)?).0,",
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#![cfg(feature = "derive")]
|
#![cfg(feature = "derive")]
|
||||||
|
|
||||||
use bincode::config::Configuration;
|
use bincode::config::Configuration;
|
||||||
|
|
||||||
#[derive(bincode::Encode, PartialEq, Debug)]
|
#[derive(bincode::Encode, PartialEq, Debug)]
|
||||||
pub(crate) struct Test<T> {
|
pub(crate) struct Test<T> {
|
||||||
a: T,
|
a: T,
|
||||||
|
|
@ -214,3 +215,39 @@ fn test_c_style_enum() {
|
||||||
assert_eq!(de(6).unwrap(), CStyleEnum::E);
|
assert_eq!(de(6).unwrap(), CStyleEnum::E);
|
||||||
assert_eq!(de(7), expected_err(7));
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue