Skip to content

Fix max_encoded_len for Compact fields#508

Merged
bkchr merged 6 commits intomasterfrom
pg/max-encoded-len
Sep 4, 2023
Merged

Fix max_encoded_len for Compact fields#508
bkchr merged 6 commits intomasterfrom
pg/max-encoded-len

Conversation

@pgherveou
Copy link
Copy Markdown
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 pgherveou requested a review from KiChjang September 1, 2023 21:24
@pgherveou pgherveou requested a review from KiChjang September 2, 2023 04:44
@pgherveou
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 :)

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
Copy Markdown
Member

bkchr commented Sep 4, 2023

@pgherveou could you release v3.6.5?

@pgherveou
Copy link
Copy Markdown
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
jsdw added a commit that referenced this pull request Dec 2, 2024
serban300 added a commit that referenced this pull request Jan 23, 2025
* 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 <serban@parity.io>
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