Skip to content

Implement From<&Vec> for Binary and HexBinary #2036

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

Closed
wants to merge 1 commit into from
Closed

Conversation

mina86
Copy link
Contributor

@mina86 mina86 commented Mar 3, 2024

To allow users access to the base64 and hex serialisation code used by Binary and HexBinary, allow casting reference to a vector into reference to the wrapper type. This is helpful when users needs some non-trivial serialisation which incorporates base64/hex. For example, consider (perhaps somewhat contrived) example of:

struct Message {
    even_length: Vec<u8>,
}

where we want even_length to use base64 encoding but also be guaranteed to be of even length. Since serialisation function takes the value by referenc, user would have to implement base64 encoding themselves. With this change, it’s possible to reuse Binary’s code:

fn serialize<S: Serializer>(even: &Vec<u8>, ser: S) -> Result<S::Ok, S::Error> {
    <&Binary>::from(even).serialize(ser)
}

This conversion is added via bytemuck’s derive thus is guaranteed to be sound.

While adding it, also replace some manual implementations by use of derive_more’s derive macros to clean up code a little bit.

To allow users access to the base64 and hex serialisation code used by
Binary and HexBinary, allow casting reference to a vector into reference
to the wrapper type.  This is helpful when users needs some non-trivial
serialisation which incorporates base64/hex.  For example, consider
(perhaps somewhat contrived) example of:

    struct Message {
        even_length: Vec<u8>,
    }

where we want `even_length` to use base64 encoding but also be
guaranteed to be of even length.  Since serialisation function takes
the value by referenc, user would have to implement base64 encoding
themselves.  With this change, it’s possible to reuse Binary’s code:

    fn serialize<S: Serializer>(even: &Vec<u8>, ser: S) -> Result<S::Ok, S::Error> {
        <&Binary>::from(even).serialize(ser)
    }

This conversion is added via bytemuck’s derive thus is guaranteed to be
sound.

While adding it, also replace some manual implementations by use of
derive_more’s derive macros to clean up code a little bit.
Copy link
Member

@webmaster128 webmaster128 left a comment

Choose a reason for hiding this comment

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

Thank you for your suggestion. Using Vec<u8>as field types is not something anyone on the CosmWasm ecosystem is doing. The reason is the default encoding of that type in Rust/serde. Using Binary instead solves that for every use case I saw so far.

This PR contains a whole lot of changes, many of which we don't want to do this way. Please break those changes down into minimal PRs such that we can discuss them individually. Then each change should get a test and CHANGELOG entry. In general I'd like to avoid the extra dependencies introduced here.

Implementing From<&Vec> for Binary and HexBinary as the title suggests is fine. However, I don't see a point for mainline cosmwasm-std to support a From implementation from reference to reference. To implement your serializer, just use the base64 crate we use in cosmwasm-std. If you want to ensure to not have a diverging implementation long term, we can implement cosmwasm_std::encoding::{to_base64,to_hex, from_base64,from_hex} and use that in Binary.

@mina86 mina86 closed this Mar 5, 2024
@mina86 mina86 deleted the a branch March 5, 2024 12:53
@webmaster128 webmaster128 mentioned this pull request Mar 5, 2024
10 tasks
@aumetra aumetra mentioned this pull request Mar 13, 2024
10 tasks
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.

2 participants