From 96269d42539b5af8bb288e0610943381d569005f Mon Sep 17 00:00:00 2001 From: James Wilson Date: Thu, 23 Jan 2025 15:55:59 +0000 Subject: [PATCH] Revert #512 and #508 (#663) * Revert "Update MaxEncodedLen derive macro (#512)" This reverts commit 1e3f80c1e047ec67f79f12e6a8067692edb14e9d. * Revert "Fix max_encoded_len for Compact fields (#508)" This reverts commit ddf9439de7253b632210072155dfc5caed0ed18a. --------- Co-authored-by: Serban Iorga --- derive/src/max_encoded_len.rs | 4 +--- src/compact.rs | 33 +++----------------------------- src/max_encoded_len.rs | 13 ++----------- tests/max_encoded_len.rs | 36 ----------------------------------- 4 files changed, 6 insertions(+), 80 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..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,