Skip to content

Do not derive encode/decode when discriminant doesn't fit u8. #283

Open
@gui1117

Description

@gui1117

whe the discriminant doesn't fit u8 the derivation is wrong (more precisely: unexpected)

#[derive(Encode, Decode)]
#[repr(u32)]
enum ABC {
	D = 3u32,
}
#[repr(u32)]
enum ABC { D = 3u32, }
const _: () =
    {
        #[allow(unknown_lints)]
        #[allow(rust_2018_idioms)]
        extern crate parity_scale_codec as _parity_scale_codec;
        impl _parity_scale_codec::Encode for ABC {
            fn encode_to<__CodecOutputEdqy: _parity_scale_codec::Output +
                         ?Sized>(&self,
                                 __codec_dest_edqy: &mut __CodecOutputEdqy) {
                match *self {
                    ABC::D => {
                        __codec_dest_edqy.push_byte(3u32 as
                                                        ::core::primitive::u8);
                    }
                    _ => (),
                }
            }
        }
        impl _parity_scale_codec::EncodeLike for ABC { }
    };
const _: () =
    {
        #[allow(unknown_lints)]
        #[allow(rust_2018_idioms)]
        extern crate parity_scale_codec as _parity_scale_codec;
        impl _parity_scale_codec::Decode for ABC {
            fn decode<__CodecInputEdqy: _parity_scale_codec::Input>(__codec_input_edqy:
                                                                        &mut __CodecInputEdqy)
             -> ::core::result::Result<Self, _parity_scale_codec::Error> {
                match __codec_input_edqy.read_byte().map_err(|e|
                                                                 e.chain("Could not decode `ABC`, failed to read variant byte"))?
                    {
                    __codec_x_edqy if
                    __codec_x_edqy == 3u32 as ::core::primitive::u8 => {
                        ::core::result::Result::Ok(ABC::D)
                    }
                    _ =>
                    ::core::result::Result::Err("Could not decode `ABC`, variant doesn\'t exist".into()),
                }
            }
        }
    };

I think we can use From::from instead of as maybe but it is probably a breaking change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions