Skip to content

Custom readers in the variant scope && pad_bits_after for unit-like enum variants #653

@Eclypsed

Description

@Eclypsed

I'm trying to create a model to represent these 1-bit and 3-bit fields within a byte:

  7   6   5   4   3   2   1   0
+---+---+---+---+---+---+---+---+
| - | D |  Subtype  | -   -   - |
+---+---+---+---+---+---+---+---+

If the D bit is 1, the Subtype bits should be parsed. If the D bit is 0, the Subtype bits hold no meaning and should be skipped over. Currently I have the following enums:

#[derive(Debug, Clone, Copy, PartialEq, Eq, DekuRead)]
#[deku(id_type = "u8", bits = 3)]
pub enum CdrwSubtype {
    #[deku(id = "0b000")]
    Standard,
    #[deku(id = "0b001")]
    HighSpeed,
    #[deku(id = "0b010")]
    UltraSpeed,
    #[deku(id = "0b011")]
    UltraSpeedPlus,
    #[deku(id_pat = "_")]
    Reserved(u8),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, DekuRead)]
#[deku(id_type = "u8", bits = 1)]
pub enum AtipDiscType {
    #[deku(id = "0")]
    Cdr(#[deku(bits = 3)] u8), // Ideally this would be a unit-like variant
    #[deku(id = "1")]
    Cdrw(CdrwSubtype),
}

This works, but in order to make sure that deku consumes all 4 bits when parsing I have to include a meaningless 3-bit field for the Cdr variant of AtipDiscType. Ideally, the Cdr variant would be able to be written as unit-like. pad_bits_after seems like a logical attribute to enable skipping bits after parsing the id for unit-like variants. Something like:

pub enum AtipDiscType {
    #[deku(id = "0", pad_bits_after = "3")]
    Cdr,
    #[deku(id = "1")]
    Cdrw(CdrwSubtype),
}

I imagine pad_bits_before might not be possible at the variant-level because it would interfere with matching the id, but maybe pad_bits_after is? I'm interested in anyway of skipping bits after after an id to ensure each variant consumes the same amount of bits, or any alternative way of structuring this that is semantically similar.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions