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

Update MaxEncodedLen derive macro #512

Merged
merged 12 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion derive/src/max_encoded_len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,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(<#crate_path::Compact::<#ty> as #crate_path::MaxEncodedLen>::max_encoded_len())
ty.span() => .saturating_add(<#ty as #crate_path::MaxEncodedLen>::max_compact_encoded_len())
}
} else {
quote_spanned! {
Expand Down
1 change: 1 addition & 0 deletions derive/src/trait_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ pub fn add<N>(

codec_types
.into_iter()
// .take(1)
pgherveou marked this conversation as resolved.
Show resolved Hide resolved
.for_each(|ty| where_clause.predicates.push(parse_quote!(#ty : #codec_bound)));

let has_compact_bound: syn::Path = parse_quote!(#crate_path::HasCompact);
Expand Down
17 changes: 15 additions & 2 deletions src/max_encoded_len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ use crate::alloc::sync::Arc;
pub trait MaxEncodedLen: Encode {
/// Upper bound, in bytes, of the maximum encoded size of this item.
fn max_encoded_len() -> usize;
/// Upper bound, in bytes, of the maximum encoded size of the compact encoding of this item.
fn max_compact_encoded_len() -> usize {
Self::max_encoded_len()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... personally I'd probably prefer to keep this as a #[no_doc] and make it an internal implementation detail, instead of making it public.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes good point

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by internal you mean simply adding the #[doc(hidden)] attribute or is there other things you can do to truly keep it internal?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by internal you mean simply adding the #[doc(hidden)] attribute or is there other things you can do to truly keep it internal?

Just hide it. I guess you could also prefix its name, e.g. prepend scale_internal_ to the name (which I did when I was adding scale_internal_decode_bytes on Input)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get why we need this at all.

The first thing should also be that there is a test added that fails with the old derive macro.

}

macro_rules! impl_primitives {
Expand All @@ -46,16 +50,25 @@ macro_rules! impl_primitives {
};
}

impl_primitives!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, bool);

impl_primitives!(
i8, i16, i32, i64, i128, bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know that you skipped all unsigned types?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the unsigned types the impl_compact macro is used now, so they're not skipped. (:

NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroI8, NonZeroI16, NonZeroI32,
NonZeroI64, NonZeroI128
);

macro_rules! impl_compact {
($( $t:ty => $e:expr; )*) => {
$(
impl MaxEncodedLen for $t {
fn max_encoded_len() -> usize {
mem::size_of::<$t>()
}

fn max_compact_encoded_len() -> usize {
$e
}
}

impl MaxEncodedLen for Compact<$t> {
fn max_encoded_len() -> usize {
$e
Expand Down
11 changes: 5 additions & 6 deletions tests/max_encoded_len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,17 @@ fn generic_max_length() {
}

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

#[test]
fn compact_field_max_length() {
assert_eq!(
CompactField::max_encoded_len(),
Compact::<u64>::max_encoded_len() + u64::max_encoded_len()
);
let max_len = CompactField::<u64>::max_encoded_len();
assert_eq!(max_len, Compact::<u64>::max_encoded_len() + u64::max_encoded_len());
assert_eq!(max_len, 17);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd prefer to have a separate test for this, instead of modifying an existing one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, this test would fail with

  Compiling parity-scale-codec v3.6.6 (/Users/pg/github/parity-scale-codec)                                                                                                                                                                                            
error[E0277]: the trait bound `Compact<T>: MaxEncodedLen` is not satisfied                                                                                                                                                                                              
  --> tests/max_encoded_len.rs:87:5                                                                                                                                                                                                                                     
   |                                                                                                                                                                                                                                                                    
87 |     t: T,                                                                                                                                                                                                                                                          
   |        ^ the trait `MaxEncodedLen` is not implemented for `Compact<T>`                                                                                                                                                                                             
   |                                                                                                                                                                                                                                                                    
help: consider extending the `where` clause, but there might be an alternative better way to express this requirement                                                                                                                                                   
   |                                                                                                                                                                                                                                                                    
84 | #[derive(Encode, MaxEncodedLen, Compact<T>: MaxEncodedLen)]                                                                    
   |                               +++++++++++++++++++++++++++


#[derive(Encode, MaxEncodedLen)]
Expand Down