Skip to content

Commit

Permalink
Bumped virtue to 0.0.13 (#626)
Browse files Browse the repository at this point in the history
Co-authored-by: Victor Koenders <victor.koenders@qrtech.se>
  • Loading branch information
VictorKoenders and Victor Koenders committed Mar 30, 2023
1 parent c623d81 commit 09d256b
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 105 deletions.
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.11"
virtue = "0.0.13"
116 changes: 62 additions & 54 deletions derive/src/derive_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ impl DeriveEnum {

// if we have any fields, declare them here
// Self::Variant { a, b, c }
if let Some(delimiter) = variant.fields.delimiter() {
if let Some(fields) = variant.fields.as_ref() {
let delimiter = fields.delimiter();
match_body.group(delimiter, |field_body| {
for (idx, field_name) in
variant.fields.names().into_iter().enumerate()
{
for (idx, field_name) in fields.names().into_iter().enumerate() {
if idx != 0 {
field_body.punct(',');
}
Expand Down Expand Up @@ -104,23 +103,25 @@ impl DeriveEnum {
body.punct('?');
body.punct(';');
// If we have any fields, encode them all one by one
for field_name in variant.fields.names() {
let attributes = field_name
.attributes()
.get_attribute::<FieldAttributes>()?
.unwrap_or_default();
if attributes.with_serde {
body.push_parsed(format!(
if let Some(fields) = variant.fields.as_ref() {
for field_name in fields.names() {
let attributes = field_name
.attributes()
.get_attribute::<FieldAttributes>()?
.unwrap_or_default();
if attributes.with_serde {
body.push_parsed(format!(
"{0}::Encode::encode(&{0}::serde::Compat({1}), encoder)?;",
crate_name,
field_name.to_string_with_prefix(TUPLE_FIELD_PREFIX),
))?;
} else {
body.push_parsed(format!(
"{0}::Encode::encode({1}, encoder)?;",
crate_name,
field_name.to_string_with_prefix(TUPLE_FIELD_PREFIX),
))?;
} else {
body.push_parsed(format!(
"{0}::Encode::encode({1}, encoder)?;",
crate_name,
field_name.to_string_with_prefix(TUPLE_FIELD_PREFIX),
))?;
}
}
}
body.push_parsed("Ok(())")?;
Expand Down Expand Up @@ -181,7 +182,7 @@ impl DeriveEnum {
variant_inner.ident_str("allowed");
variant_inner.punct(':');

if self.variants.iter().any(|i| i.has_fixed_value()) {
if self.variants.iter().any(|i| i.value.is_some()) {
// we have fixed values, implement AllowedEnumVariants::Allowed
variant_inner.push_parsed(format!(
"&{}::error::AllowedEnumVariants::Allowed",
Expand Down Expand Up @@ -230,7 +231,7 @@ impl DeriveEnum {
where_constraints.push_parsed_constraint(bounds).map_err(|e| e.with_span(lit.span()))?;
} else {
for g in generics.iter_generics() {
where_constraints.push_constraint(g, format!("{}::Decode", crate_name)).unwrap();
where_constraints.push_constraint(g, format!("{}::Decode", crate_name))?;
}
}
Ok(())
Expand Down Expand Up @@ -272,27 +273,29 @@ impl DeriveEnum {
variant_case_body.ident(variant.name.clone());

variant_case_body.group(Delimiter::Brace, |variant_body| {
let is_tuple = matches!(variant.fields, Fields::Tuple(_));
for (idx, field) in variant.fields.names().into_iter().enumerate() {
if is_tuple {
variant_body.lit_usize(idx);
} else {
variant_body.ident(field.unwrap_ident().clone());
}
variant_body.punct(':');
let attributes = field.attributes().get_attribute::<FieldAttributes>()?.unwrap_or_default();
if attributes.with_serde {
variant_body
.push_parsed(format!(
"<{0}::serde::Compat<_> as {0}::Decode>::decode(decoder)?.0,",
crate_name
))?;
} else {
variant_body
.push_parsed(format!(
"{}::Decode::decode(decoder)?,",
crate_name
))?;
if let Some(fields) = variant.fields.as_ref() {
let is_tuple = matches!(fields, Fields::Tuple(_));
for (idx, field) in fields.names().into_iter().enumerate() {
if is_tuple {
variant_body.lit_usize(idx);
} else {
variant_body.ident(field.unwrap_ident().clone());
}
variant_body.punct(':');
let attributes = field.attributes().get_attribute::<FieldAttributes>()?.unwrap_or_default();
if attributes.with_serde {
variant_body
.push_parsed(format!(
"<{0}::serde::Compat<_> as {0}::Decode>::decode(decoder)?.0,",
crate_name
))?;
} else {
variant_body
.push_parsed(format!(
"{}::Decode::decode(decoder)?,",
crate_name
))?;
}
}
}
Ok(())
Expand Down Expand Up @@ -327,6 +330,9 @@ impl DeriveEnum {
for g in generics.iter_generics() {
where_constraints.push_constraint(g, format!("{}::de::BorrowDecode<'__de>", crate_name)).unwrap();
}
for lt in generics.iter_lifetimes() {
where_constraints.push_parsed_constraint(format!("'__de: '{}", lt.ident))?;
}
}
Ok(())
})?
Expand Down Expand Up @@ -364,20 +370,22 @@ impl DeriveEnum {
variant_case_body.ident(variant.name.clone());

variant_case_body.group(Delimiter::Brace, |variant_body| {
let is_tuple = matches!(variant.fields, Fields::Tuple(_));
for (idx, field) in variant.fields.names().into_iter().enumerate() {
if is_tuple {
variant_body.lit_usize(idx);
} else {
variant_body.ident(field.unwrap_ident().clone());
}
variant_body.punct(':');
let attributes = field.attributes().get_attribute::<FieldAttributes>()?.unwrap_or_default();
if attributes.with_serde {
variant_body
.push_parsed(format!("<{0}::serde::BorrowCompat<_> as {0}::BorrowDecode>::borrow_decode(decoder)?.0,", crate_name))?;
} else {
variant_body.push_parsed(format!("{}::BorrowDecode::borrow_decode(decoder)?,", crate_name))?;
if let Some(fields) = variant.fields.as_ref() {
let is_tuple = matches!(fields, Fields::Tuple(_));
for (idx, field) in fields.names().into_iter().enumerate() {
if is_tuple {
variant_body.lit_usize(idx);
} else {
variant_body.ident(field.unwrap_ident().clone());
}
variant_body.punct(':');
let attributes = field.attributes().get_attribute::<FieldAttributes>()?.unwrap_or_default();
if attributes.with_serde {
variant_body
.push_parsed(format!("<{0}::serde::BorrowCompat<_> as {0}::BorrowDecode>::borrow_decode(decoder)?.0,", crate_name))?;
} else {
variant_body.push_parsed(format!("{}::BorrowDecode::borrow_decode(decoder)?,", crate_name))?;
}
}
}
Ok(())
Expand Down
105 changes: 57 additions & 48 deletions derive/src/derive_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use virtue::parse::Fields;
use virtue::prelude::*;

pub(crate) struct DeriveStruct {
pub fields: Fields,
pub fields: Option<Fields>,
pub attributes: ContainerAttributes,
}

Expand Down Expand Up @@ -39,21 +39,23 @@ impl DeriveStruct {
crate_name
))
.body(|fn_body| {
for field in self.fields.names() {
let attributes = field
.attributes()
.get_attribute::<FieldAttributes>()?
.unwrap_or_default();
if attributes.with_serde {
fn_body.push_parsed(format!(
"{0}::Encode::encode(&{0}::serde::Compat(&self.{1}), encoder)?;",
crate_name, field
))?;
} else {
fn_body.push_parsed(format!(
"{}::Encode::encode(&self.{}, encoder)?;",
crate_name, field
))?;
if let Some(fields) = self.fields.as_ref() {
for field in fields.names() {
let attributes = field
.attributes()
.get_attribute::<FieldAttributes>()?
.unwrap_or_default();
if attributes.with_serde {
fn_body.push_parsed(format!(
"{0}::Encode::encode(&{0}::serde::Compat(&self.{1}), encoder)?;",
crate_name, field
))?;
} else {
fn_body.push_parsed(format!(
"{}::Encode::encode(&self.{}, encoder)?;",
crate_name, field
))?;
}
}
}
fn_body.push_parsed("Ok(())")?;
Expand Down Expand Up @@ -95,22 +97,24 @@ impl DeriveStruct {
// b: bincode::Decode::decode(decoder)?,
// ...
// }
for field in &self.fields.names() {
let attributes = field.attributes().get_attribute::<FieldAttributes>()?.unwrap_or_default();
if attributes.with_serde {
struct_body
.push_parsed(format!(
"{1}: (<{0}::serde::Compat<_> as {0}::Decode>::decode(decoder)?).0,",
crate_name,
field
))?;
} else {
struct_body
.push_parsed(format!(
"{1}: {0}::Decode::decode(decoder)?,",
crate_name,
field
))?;
if let Some(fields) = self.fields.as_ref() {
for field in fields.names() {
let attributes = field.attributes().get_attribute::<FieldAttributes>()?.unwrap_or_default();
if attributes.with_serde {
struct_body
.push_parsed(format!(
"{1}: (<{0}::serde::Compat<_> as {0}::Decode>::decode(decoder)?).0,",
crate_name,
field
))?;
} else {
struct_body
.push_parsed(format!(
"{1}: {0}::Decode::decode(decoder)?,",
crate_name,
field
))?;
}
}
}
Ok(())
Expand All @@ -137,6 +141,9 @@ impl DeriveStruct {
for g in generics.iter_generics() {
where_constraints.push_constraint(g, format!("{}::de::BorrowDecode<'__de>", crate_name)).unwrap();
}
for lt in generics.iter_lifetimes() {
where_constraints.push_parsed_constraint(format!("'__de: '{}", lt.ident))?;
}
}
Ok(())
})?
Expand All @@ -150,22 +157,24 @@ impl DeriveStruct {
fn_body.group(Delimiter::Parenthesis, |ok_group| {
ok_group.ident_str("Self");
ok_group.group(Delimiter::Brace, |struct_body| {
for field in self.fields.names() {
let attributes = field.attributes().get_attribute::<FieldAttributes>()?.unwrap_or_default();
if attributes.with_serde {
struct_body
.push_parsed(format!(
"{1}: (<{0}::serde::BorrowCompat<_> as {0}::BorrowDecode>::borrow_decode(decoder)?).0,",
crate_name,
field
))?;
} else {
struct_body
.push_parsed(format!(
"{1}: {0}::BorrowDecode::borrow_decode(decoder)?,",
crate_name,
field
))?;
if let Some(fields) = self.fields.as_ref() {
for field in fields.names() {
let attributes = field.attributes().get_attribute::<FieldAttributes>()?.unwrap_or_default();
if attributes.with_serde {
struct_body
.push_parsed(format!(
"{1}: (<{0}::serde::BorrowCompat<_> as {0}::BorrowDecode>::borrow_decode(decoder)?).0,",
crate_name,
field
))?;
} else {
struct_body
.push_parsed(format!(
"{1}: {0}::BorrowDecode::borrow_decode(decoder)?,",
crate_name,
field
))?;
}
}
}
Ok(())
Expand Down
3 changes: 3 additions & 0 deletions tests/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ mod issue_570;

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

#[path = "issues/issue_614.rs"]
mod issue_614;
6 changes: 4 additions & 2 deletions tests/issues/issue_431.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ use std::borrow::Cow;
use std::string::String;

#[derive(Decode, Encode, PartialEq, Debug)]
#[bincode(borrow_decode_bounds = "&'__de U<'a, A>: ::bincode::de::BorrowDecode<'__de> + '__de")]
#[bincode(
borrow_decode_bounds = "&'__de U<'a, A>: ::bincode::de::BorrowDecode<'__de> + '__de, '__de: 'a"
)]
struct T<'a, A: Clone + Encode + Decode> {
t: Cow<'a, U<'a, A>>,
}

#[derive(Clone, Decode, Encode, PartialEq, Debug)]
#[bincode(borrow_decode_bounds = "&'__de A: ::bincode::de::BorrowDecode<'__de> + '__de")]
#[bincode(borrow_decode_bounds = "&'__de A: ::bincode::de::BorrowDecode<'__de> + '__de, '__de: 'a")]
struct U<'a, A: Clone + Encode + Decode> {
u: Cow<'a, A>,
}
Expand Down
22 changes: 22 additions & 0 deletions tests/issues/issue_614.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![cfg(feature = "derive")]

use bincode::{Decode, Encode};

#[derive(Encode, Decode, Clone)]
pub struct A;
#[derive(Encode, Decode, Clone)]
pub struct B<T>
where
T: Clone + Encode + Decode,
{
pub t: T,
}

#[derive(Encode, Decode)]
pub struct MyStruct<T>
where
T: Clone + Encode + Decode,
{
pub a: A,
pub b: B<T>,
}

0 comments on commit 09d256b

Please sign in to comment.