Skip to content

Commit

Permalink
Revert "Fix max_encoded_len for Compact fields (#508)"
Browse files Browse the repository at this point in the history
This reverts commit ddf9439
  • Loading branch information
ascjones committed Nov 29, 2023
1 parent 5bbda86 commit 39058e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 53 deletions.
51 changes: 22 additions & 29 deletions derive/src/max_encoded_len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

use crate::{
trait_bounds,
utils::{self, codec_crate_path, custom_mel_trait_bound, has_dumb_trait_bound, should_skip},
utils::{codec_crate_path, custom_mel_trait_bound, has_dumb_trait_bound, should_skip},
};
use quote::{quote, quote_spanned};
use syn::{parse_quote, spanned::Spanned, Data, DeriveInput, Field, Fields};
use syn::{parse_quote, spanned::Spanned, Data, DeriveInput, Fields, Type};

/// impl for `#[derive(MaxEncodedLen)]`
pub fn derive_max_encoded_len(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
Expand All @@ -43,13 +43,13 @@ pub fn derive_max_encoded_len(input: proc_macro::TokenStream) -> proc_macro::Tok
parse_quote!(#crate_path::MaxEncodedLen),
None,
has_dumb_trait_bound(&input.attrs),
&crate_path,
&crate_path
) {
return e.to_compile_error().into()
}
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

let data_expr = data_length_expr(&input.data, &crate_path);
let data_expr = data_length_expr(&input.data);

quote::quote!(
const _: () = {
Expand All @@ -64,22 +64,22 @@ pub fn derive_max_encoded_len(input: proc_macro::TokenStream) -> proc_macro::Tok
}

/// generate an expression to sum up the max encoded length from several fields
fn fields_length_expr(fields: &Fields, crate_path: &syn::Path) -> proc_macro2::TokenStream {
let fields_iter: Box<dyn Iterator<Item = &Field>> = match fields {
Fields::Named(ref fields) => Box::new(fields.named.iter().filter_map(|field| {
if should_skip(&field.attrs) {
fn fields_length_expr(fields: &Fields) -> proc_macro2::TokenStream {
let type_iter: Box<dyn Iterator<Item = &Type>> = match fields {
Fields::Named(ref fields) => Box::new(
fields.named.iter().filter_map(|field| if should_skip(&field.attrs) {
None
} else {
Some(field)
}
})),
Fields::Unnamed(ref fields) => Box::new(fields.unnamed.iter().filter_map(|field| {
if should_skip(&field.attrs) {
Some(&field.ty)
})
),
Fields::Unnamed(ref fields) => Box::new(
fields.unnamed.iter().filter_map(|field| if should_skip(&field.attrs) {
None
} else {
Some(field)
}
})),
Some(&field.ty)
})
),
Fields::Unit => Box::new(std::iter::empty()),
};
// expands to an expression like
Expand All @@ -92,16 +92,9 @@ fn fields_length_expr(fields: &Fields, crate_path: &syn::Path) -> proc_macro2::T
// `max_encoded_len` call. This way, if one field's type doesn't implement
// `MaxEncodedLen`, the compiler's error message will underline which field
// caused the issue.
let expansion = fields_iter.map(|field| {
let ty = &field.ty;
if utils::is_compact(&field) {
quote_spanned! {
ty.span() => .saturating_add(<#crate_path::Compact::<#ty> as #crate_path::MaxEncodedLen>::max_encoded_len())
}
} else {
quote_spanned! {
ty.span() => .saturating_add(<#ty as #crate_path::MaxEncodedLen>::max_encoded_len())
}
let expansion = type_iter.map(|ty| {
quote_spanned! {
ty.span() => .saturating_add(<#ty>::max_encoded_len())
}
});
quote! {
Expand All @@ -110,9 +103,9 @@ fn fields_length_expr(fields: &Fields, crate_path: &syn::Path) -> proc_macro2::T
}

// generate an expression to sum up the max encoded length of each field
fn data_length_expr(data: &Data, crate_path: &syn::Path) -> proc_macro2::TokenStream {
fn data_length_expr(data: &Data) -> proc_macro2::TokenStream {
match *data {
Data::Struct(ref data) => fields_length_expr(&data.fields, crate_path),
Data::Struct(ref data) => fields_length_expr(&data.fields),
Data::Enum(ref data) => {
// We need an expression expanded for each variant like
//
Expand All @@ -128,7 +121,7 @@ fn data_length_expr(data: &Data, crate_path: &syn::Path) -> proc_macro2::TokenSt
// Each variant expression's sum is computed the way an equivalent struct's would be.

let expansion = data.variants.iter().map(|variant| {
let variant_expression = fields_length_expr(&variant.fields, crate_path);
let variant_expression = fields_length_expr(&variant.fields);
quote! {
.max(#variant_expression)
}
Expand Down
25 changes: 1 addition & 24 deletions tests/max_encoded_len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! Tests for MaxEncodedLen derive macro
#![cfg(all(feature = "derive", feature = "max-encoded-len"))]

use parity_scale_codec::{Compact, Decode, Encode, MaxEncodedLen};
use parity_scale_codec::{MaxEncodedLen, Compact, Decode, Encode};

#[derive(Encode, MaxEncodedLen)]
struct Primitives {
Expand Down Expand Up @@ -64,29 +64,6 @@ fn generic_max_length() {
assert_eq!(Generic::<u32>::max_encoded_len(), u32::max_encoded_len() * 2);
}

#[derive(Encode, MaxEncodedLen)]
struct CompactField {
#[codec(compact)]
t: u64,
v: u64,
}

#[test]
fn compact_field_max_length() {
assert_eq!(
CompactField::max_encoded_len(),
Compact::<u64>::max_encoded_len() + u64::max_encoded_len()
);
}

#[derive(Encode, MaxEncodedLen)]
struct CompactStruct(#[codec(compact)] u64);

#[test]
fn compact_struct_max_length() {
assert_eq!(CompactStruct::max_encoded_len(), Compact::<u64>::max_encoded_len());
}

#[derive(Encode, MaxEncodedLen)]
struct TwoGenerics<T, U> {
t: T,
Expand Down

0 comments on commit 39058e4

Please sign in to comment.