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..8e64d7cf 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,9 @@ 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, + 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..8a044329 100644 --- a/src/max_encoded_len.rs +++ b/src/max_encoded_len.rs @@ -51,18 +51,9 @@ 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, diff --git a/tests/max_encoded_len.rs b/tests/max_encoded_len.rs index ceb2810c..ea34a862 100644 --- a/tests/max_encoded_len.rs +++ b/tests/max_encoded_len.rs @@ -64,42 +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(), 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); - -#[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`