Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert #512 and #508 #663

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 8 additions & 12 deletions tests/max_encoded_len_ui/unsupported_variant.stderr
Original file line number Diff line number Diff line change
@@ -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`
Loading