Skip to content

Commit

Permalink
Revert #512 and #508 (#663)
Browse files Browse the repository at this point in the history
* Revert "Update MaxEncodedLen derive macro (#512)"

This reverts commit 1e3f80c.

* Revert "Fix max_encoded_len for Compact fields (#508)"

This reverts commit ddf9439.

---------

Co-authored-by: Serban Iorga <[email protected]>
  • Loading branch information
jsdw and serban300 authored Jan 23, 2025
1 parent ff0b259 commit 96269d4
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 80 deletions.
4 changes: 1 addition & 3 deletions derive/src/max_encoded_len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down
33 changes: 3 additions & 30 deletions src/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<T: MaxEncodedLen> 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<T> 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<Self> + Into<Self> + MaybeMaxEncodedLen;
type Type: for<'a> EncodeAsRef<'a, Self> + Decode + From<Self> + Into<Self>;
}

impl<'a, T: 'a> EncodeAsRef<'a, T> for Compact<T>
Expand All @@ -266,21 +251,9 @@ where
type RefType = CompactRef<'a, T>;
}

#[cfg(feature = "max-encoded-len")]
impl<T> MaxEncodedLen for Compact<T>
where
T: CompactAs,
Compact<T::As>: MaxEncodedLen,
Compact<T>: Encode,
{
fn max_encoded_len() -> usize {
Compact::<T::As>::max_encoded_len()
}
}

impl<T: 'static> HasCompact for T
where
Compact<T>: for<'a> EncodeAsRef<'a, T> + Decode + From<Self> + Into<Self> + MaybeMaxEncodedLen,
Compact<T>: for<'a> EncodeAsRef<'a, T> + Decode + From<Self> + Into<Self>,
{
type Type = Compact<T>;
}
Expand Down
13 changes: 2 additions & 11 deletions src/max_encoded_len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 0 additions & 36 deletions tests/max_encoded_len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,42 +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(), 17);
assert_eq!(
CompactField::max_encoded_len(),
Compact::<u64>::max_encoded_len() + u64::max_encoded_len()
);
}

#[derive(Encode, MaxEncodedLen)]
struct CompactFieldGenerics<T: MaxEncodedLen> {
#[codec(compact)]
t: T,
v: u64,
}

#[test]
fn compact_field_generics_max_length() {
assert_eq!(CompactFieldGenerics::<u64>::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::<u64>::max_encoded_len());
}

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

0 comments on commit 96269d4

Please sign in to comment.