deku icon indicating copy to clipboard operation
deku copied to clipboard

`id_pat` with external `id`

Open shirok1 opened this issue 2 years ago • 2 comments

Hello, it's me again.

When using an Enum type with external id (from context) and an id_pat on any Enum variant, the read method (::deku::DekuRead) implement for that variant will incorrectly try to read the id receiver field again.

#[deku_derive(DekuRead, DekuWrite)]
#[derive(Debug)]
#[deku(ctx = "content_id: u16, content_size: u16", id = "content_id")]
pub enum SomeDataTypeEnum {
    // #[deku(id = "0x0200")]
    // ThisWorks {
    //     #[deku(bytes_read = "content_size")]
    //     content: Vec<u8>,
    // },
    #[deku(id_pat = "0x0201..=0x02FF")]
    ThisWontWork {
        content_id: u16,
        #[deku(bytes_read = "content_size")]
        content: Vec<u8>,
    },
}

...expands to this:

0x0201..=0x02FF => {
    let __deku_id = {
        let (__deku_new_rest, __deku_value) = <u16 as ::deku::DekuRead<
            '_,
            _,
        >>::read(__deku_rest, ())?;
        let __deku_value: u16 = Result::<
            _,
            ::deku::DekuError,
        >::Ok(__deku_value)?;
        __deku_rest = __deku_new_rest;
        __deku_value
    };
    let content_id = &__deku_id;

I suggest adding an explicit attribute for specifying the id receiver.

shirok1 avatar Jan 02 '23 11:01 shirok1

thanks for the report.

In the meantime, can you confirm this is a valid work-around?

#[deku(skip, default = "content_id")]
content_id: u16,

sharksforarms avatar Jan 06 '23 03:01 sharksforarms

Can you confirm this is a valid work-around?

#[deku(skip, default = "content_id")]
content_id: u16,

Yes, it works. :rofl: Pretty strange.

shirok1 avatar Jan 06 '23 07:01 shirok1