From 24d8d97d5628e5a02b2d649b5a056fcd22b3e91f Mon Sep 17 00:00:00 2001 From: James Wilson Date: Mon, 2 Dec 2024 10:02:05 +0000 Subject: [PATCH 1/3] Revert "Update MaxEncodedLen derive macro (#512)" This reverts commit 1e3f80c1e047ec67f79f12e6a8067692edb14e9d. --- derive/src/max_encoded_len.rs | 4 +--- src/compact.rs | 36 ++++------------------------------- src/max_encoded_len.rs | 25 ++++-------------------- tests/max_encoded_len.rs | 13 ------------- 4 files changed, 9 insertions(+), 69 deletions(-) diff --git a/derive/src/max_encoded_len.rs b/derive/src/max_encoded_len.rs index b9bcf580..baf738bd 100644 --- a/derive/src/max_encoded_len.rs +++ b/derive/src/max_encoded_len.rs @@ -86,9 +86,7 @@ fn fields_length_expr(fields: &Fields, crate_path: &syn::Path) -> proc_macro2::T let ty = &field.ty; if utils::is_compact(field) { quote_spanned! { - ty.span() => .saturating_add( - <<#ty as #crate_path::HasCompact>::Type as #crate_path::MaxEncodedLen>::max_encoded_len() - ) + ty.span() => .saturating_add(<#crate_path::Compact::<#ty> as #crate_path::MaxEncodedLen>::max_encoded_len()) } } else { quote_spanned! { diff --git a/src/compact.rs b/src/compact.rs index ffcd2ae4..e02b660b 100644 --- a/src/compact.rs +++ b/src/compact.rs @@ -16,14 +16,13 @@ use arrayvec::ArrayVec; -#[cfg(feature = "max-encoded-len")] -use crate::MaxEncodedLen; use crate::{ alloc::vec::Vec, codec::{Decode, Encode, EncodeAsRef, Input, Output}, encode_like::EncodeLike, DecodeWithMemTracking, Error, }; + #[cfg(feature = "fuzz")] use arbitrary::Arbitrary; @@ -239,24 +238,10 @@ where } } -/// Requires the presence of `MaxEncodedLen` when the `max-encoded-len` feature is active. -// Remove this trait when the feature is removed. -#[cfg(feature = "max-encoded-len")] -pub trait MaybeMaxEncodedLen: MaxEncodedLen {} -#[cfg(feature = "max-encoded-len")] -impl MaybeMaxEncodedLen for T {} - -/// Requires the presence of `MaxEncodedLen` when the `max-encoded-len` feature is active. -// Remove this trait when the feature is removed. -#[cfg(not(feature = "max-encoded-len"))] -pub trait MaybeMaxEncodedLen {} -#[cfg(not(feature = "max-encoded-len"))] -impl MaybeMaxEncodedLen for T {} - /// Trait that tells you if a given type can be encoded/decoded in a compact way. pub trait HasCompact: Sized { /// The compact type; this can be - type Type: for<'a> EncodeAsRef<'a, Self> + Decode + From + Into + MaybeMaxEncodedLen; + type Type: for<'a> EncodeAsRef<'a, Self> + Decode + From + Into; } impl<'a, T: 'a> EncodeAsRef<'a, T> for Compact @@ -266,21 +251,8 @@ where type RefType = CompactRef<'a, T>; } -#[cfg(feature = "max-encoded-len")] -impl MaxEncodedLen for Compact -where - T: CompactAs, - Compact: MaxEncodedLen, - Compact: Encode, -{ - fn max_encoded_len() -> usize { - Compact::::max_encoded_len() - } -} - -impl HasCompact for T -where - Compact: for<'a> EncodeAsRef<'a, T> + Decode + From + Into + MaybeMaxEncodedLen, +impl HasCompact for T where + Compact: for<'a> EncodeAsRef<'a, T> + Decode + From + Into { type Type = Compact; } diff --git a/src/max_encoded_len.rs b/src/max_encoded_len.rs index 2c08dc81..b1a55965 100644 --- a/src/max_encoded_len.rs +++ b/src/max_encoded_len.rs @@ -51,28 +51,11 @@ macro_rules! impl_primitives { }; } +impl_primitives!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, bool); + impl_primitives!( - u8, - i8, - u16, - i16, - u32, - i32, - u64, - i64, - u128, - i128, - bool, - NonZeroU8, - NonZeroU16, - NonZeroU32, - NonZeroU64, - NonZeroU128, - NonZeroI8, - NonZeroI16, - NonZeroI32, - NonZeroI64, - NonZeroI128 + NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroI8, NonZeroI16, NonZeroI32, + NonZeroI64, NonZeroI128 ); macro_rules! impl_compact { diff --git a/tests/max_encoded_len.rs b/tests/max_encoded_len.rs index ceb2810c..ad6483e2 100644 --- a/tests/max_encoded_len.rs +++ b/tests/max_encoded_len.rs @@ -73,25 +73,12 @@ struct CompactField { #[test] fn compact_field_max_length() { - assert_eq!(CompactField::max_encoded_len(), 17); assert_eq!( CompactField::max_encoded_len(), Compact::::max_encoded_len() + u64::max_encoded_len() ); } -#[derive(Encode, MaxEncodedLen)] -struct CompactFieldGenerics { - #[codec(compact)] - t: T, - v: u64, -} - -#[test] -fn compact_field_generics_max_length() { - assert_eq!(CompactFieldGenerics::::max_encoded_len(), CompactField::max_encoded_len()); -} - #[derive(Encode, MaxEncodedLen)] struct CompactStruct(#[codec(compact)] u64); From d4c75ae4efadb679bacfe564631b491d5874e9d4 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Mon, 2 Dec 2024 10:08:17 +0000 Subject: [PATCH 2/3] Revert "Fix max_encoded_len for Compact fields (#508)" This reverts commit ddf9439de7253b632210072155dfc5caed0ed18a. --- derive/src/max_encoded_len.rs | 2 +- tests/max_encoded_len.rs | 25 +------------------ .../unsupported_variant.stderr | 20 ++++++--------- 3 files changed, 10 insertions(+), 37 deletions(-) diff --git a/derive/src/max_encoded_len.rs b/derive/src/max_encoded_len.rs index baf738bd..086ccf69 100644 --- a/derive/src/max_encoded_len.rs +++ b/derive/src/max_encoded_len.rs @@ -43,7 +43,7 @@ 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(); } diff --git a/tests/max_encoded_len.rs b/tests/max_encoded_len.rs index ad6483e2..6cdd5fc6 100644 --- a/tests/max_encoded_len.rs +++ b/tests/max_encoded_len.rs @@ -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 { @@ -64,29 +64,6 @@ fn generic_max_length() { assert_eq!(Generic::::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::::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::::max_encoded_len()); -} - #[derive(Encode, MaxEncodedLen)] struct TwoGenerics { t: T, diff --git a/tests/max_encoded_len_ui/unsupported_variant.stderr b/tests/max_encoded_len_ui/unsupported_variant.stderr index c4a48c60..bc4acacc 100644 --- a/tests/max_encoded_len_ui/unsupported_variant.stderr +++ b/tests/max_encoded_len_ui/unsupported_variant.stderr @@ -1,16 +1,12 @@ -error[E0277]: the trait bound `NotMel: MaxEncodedLen` is not satisfied +error[E0599]: no function or associated item named `max_encoded_len` found for struct `NotMel` in the current scope --> tests/max_encoded_len_ui/unsupported_variant.rs:8:9 | +4 | struct NotMel; + | ------------- function or associated item `max_encoded_len` not found for this struct +... 8 | NotMel(NotMel), - | ^^^^^^ the trait `MaxEncodedLen` is not implemented for `NotMel` + | ^^^^^^ function or associated item not found in `NotMel` | - = help: the following other types implement trait `MaxEncodedLen`: - () - (TupleElement0, TupleElement1) - (TupleElement0, TupleElement1, TupleElement2) - (TupleElement0, TupleElement1, TupleElement2, TupleElement3) - (TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4) - (TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5) - (TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6) - (TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6, TupleElement7) - and $N others + = help: items from traits can only be used if the trait is implemented and in scope + = note: the following trait defines an item `max_encoded_len`, perhaps you need to implement it: + candidate #1: `MaxEncodedLen` From 9e4361f92ae96e363255a744880f8c8249b9f945 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Mon, 2 Dec 2024 10:12:37 +0000 Subject: [PATCH 3/3] fmt --- derive/src/max_encoded_len.rs | 2 +- src/compact.rs | 5 +++-- src/max_encoded_len.rs | 12 ++++++++++-- tests/max_encoded_len.rs | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/derive/src/max_encoded_len.rs b/derive/src/max_encoded_len.rs index 086ccf69..baf738bd 100644 --- a/derive/src/max_encoded_len.rs +++ b/derive/src/max_encoded_len.rs @@ -43,7 +43,7 @@ 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(); } diff --git a/src/compact.rs b/src/compact.rs index e02b660b..8e64d7cf 100644 --- a/src/compact.rs +++ b/src/compact.rs @@ -251,8 +251,9 @@ where type RefType = CompactRef<'a, T>; } -impl HasCompact for T where - Compact: for<'a> EncodeAsRef<'a, T> + Decode + From + Into +impl HasCompact for T +where + Compact: for<'a> EncodeAsRef<'a, T> + Decode + From + Into, { type Type = Compact; } diff --git a/src/max_encoded_len.rs b/src/max_encoded_len.rs index b1a55965..8a044329 100644 --- a/src/max_encoded_len.rs +++ b/src/max_encoded_len.rs @@ -54,8 +54,16 @@ macro_rules! impl_primitives { impl_primitives!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, bool); impl_primitives!( - NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroI8, NonZeroI16, NonZeroI32, - NonZeroI64, NonZeroI128 + NonZeroU8, + NonZeroU16, + NonZeroU32, + NonZeroU64, + NonZeroU128, + NonZeroI8, + NonZeroI16, + NonZeroI32, + NonZeroI64, + NonZeroI128 ); macro_rules! impl_compact { diff --git a/tests/max_encoded_len.rs b/tests/max_encoded_len.rs index 6cdd5fc6..ea34a862 100644 --- a/tests/max_encoded_len.rs +++ b/tests/max_encoded_len.rs @@ -16,7 +16,7 @@ //! Tests for MaxEncodedLen derive macro #![cfg(all(feature = "derive", feature = "max-encoded-len"))] -use parity_scale_codec::{MaxEncodedLen, Compact, Decode, Encode}; +use parity_scale_codec::{Compact, Decode, Encode, MaxEncodedLen}; #[derive(Encode, MaxEncodedLen)] struct Primitives {