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

Fix max_encoded_len for Compact fields #508

Merged
merged 6 commits into from
Sep 4, 2023
Merged

Fix max_encoded_len for Compact fields #508

merged 6 commits into from
Sep 4, 2023

Conversation

pgherveou
Copy link
Contributor

@pgherveou pgherveou commented Sep 1, 2023

Noticed that when using compact fields the max_encoded_len macro expand to the same as if we were not using the compact attribute.

Screenshot 2023-09-01 at 21 44 55

#[derive(Encode, Decode, MaxEncodedLen)]
pub struct Weight {
    #[codec(compact)]
    ref_time: u64,
    #[codec(compact)]
    proof_size: u64,
}

Will expand to

    impl ::codec::MaxEncodedLen for Weight {
        fn max_encoded_len() -> ::core::primitive::usize {
            0_usize
                .saturating_add(<u64>::max_encoded_len())
                .saturating_add(<u64>::max_encoded_len())
        }
    }

This PR fix it to

    impl ::codec::MaxEncodedLen for Weight {
        fn max_encoded_len() -> ::core::primitive::usize {
            0_usize
                .saturating_add(::parity_scale_codec::Compact<u64>::max_encoded_len())
                .saturating_add(::parity_scale_codec::Compact<u64>::max_encoded_len())
        }
    }

@pgherveou pgherveou marked this pull request as ready for review September 1, 2023 21:21
@pgherveou
Copy link
Contributor Author

also a few side thoughts:
Do we really need saturating_add for implementing this trait?
Could this be a candidate for const fn?

@bkchr
Copy link
Member

bkchr commented Sep 4, 2023

also a few side thoughts: Do we really need saturating_add for implementing this trait?

I mean we probably never use more than usize, but there is also no harm in using saturating_add.

Could this be a candidate for const fn?

const fn trait functions are not supported right now.

Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

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

Some nitpicks, otherwise looks good 👍

Good work :)

tests/max_encoded_len.rs Outdated Show resolved Hide resolved
derive/src/max_encoded_len.rs Outdated Show resolved Hide resolved
derive/src/max_encoded_len.rs Outdated Show resolved Hide resolved
pgherveou and others added 2 commits September 4, 2023 07:28
Co-authored-by: Bastian Köcher <git@kchr.de>
@bkchr bkchr merged commit ddf9439 into master Sep 4, 2023
@bkchr bkchr deleted the pg/max-encoded-len branch September 4, 2023 07:16
@bkchr
Copy link
Member

bkchr commented Sep 4, 2023

@pgherveou could you release v3.6.5?

@pgherveou
Copy link
Contributor Author

isnt 3.6.5 the current version, you mean 3.6.6?

ascjones added a commit that referenced this pull request Nov 29, 2023
ascjones added a commit that referenced this pull request Nov 29, 2023
* Revert "Update MaxEncodedLen derive macro (#512)"

This reverts commit 1e3f80c.

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

This reverts commit ddf9439

* Bump version to 3.6.9

* Cargo.lock

* ui test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants