Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Alexander
    @anti-social

    Hello
    I created my own string struct that stores string's size. Now I want to handle case when size is stored inside enum's variant:

    #[derive(DekuRead, DekuWrite, Debug, PartialEq)]
    #[deku(endian = "big", type = "u8")]
    enum Message {
        #[deku(id = "0")]
        Unknown,
        #[deku(id_pat = "_")]
        Data(
            #[deku(ctx = "variant_id")]
            RfbString<u8>
        ),
    }

    I've implemented corresponding DekuRead<'a, (Endian, u8)> and it works fine. But DekuWrite<(Endian, u8)> fails with:

    #[deku(ctx = "variant_id")]
                 ^^^^^^^^^^^^^ not found in this scope
    4 replies
    Marcos Alfredo Núñez
    @marnunez
    Hi!
    Quick question: how can we use an enum as a top-level id?
    Like, with this example:
    #[derive(PartialEq, Debug, DekuRead, DekuWrite)]
    struct DekuTest {
        my_id: u8,
        data: u8,
        #[deku(ctx = "*my_id")]
        enum_from_id: MyEnum,
    }
    #[derive(PartialEq, Debug, DekuRead, DekuWrite)]
    #[deku(ctx = "my_id: u8", id = "my_id")]
    enum MyEnum {
        #[deku(id = "1")]
        VariantA(u8),
        #[deku(id = "2")]
        VariantB,
    }
    Marcos Alfredo Núñez
    @marnunez
    If my_id there was an enum instead of a u8
    I'm getting
    error[E0507]: cannot move out of `*block_type` which is behind a shared reference
      --> src\lib.rs:50:18
       |
    50 |     #[deku(ctx = "*block_type")]
       |                  ^^^^^^^^^^^^^ move occurs because `*block_type` has type `TdfBlockType`, which does not implement the `Copy` trait
    
    For more information about this error, try `rustc --explain E0507`.
    error: could not compile `rust-tdf` due to previous error
    Where TdfBlockType is
    #[derive(Debug, DekuRead, DekuWrite)]
    #[deku(type = "u32")]
    enum TdfBlockType {
        #[deku(id = "0")]
        UnusedSlot,
        #[deku(id = "1")]
        NotDefined,
        #[deku(id = "2")]
        CalibrationData,
        #[deku(id = "3")]
        CalibrationData2d,
        #[deku(id = "4")]
        Data2d,
        #[deku(id = "5")]
        Data3d,
        #[deku(id = "6")]
        OpticalSystemConfiguration,
        #[deku(id = "7")]
        ForcePlatformsCalibrationData,
        #[deku(id = "8")]
        ForcePlatformsCalibrationData2d,
        #[deku(id = "9")]
        ForcePlatformsData,
        #[deku(id = "10")]
        AnthropometricData,
        #[deku(id = "11")]
        ElectromyographicData,
        #[deku(id = "12")]
        ForceAndTorqueData,
        #[deku(id = "13")]
        VolumetricData,
        #[deku(id = "14")]
        AnalogData,
        #[deku(id = "15")]
        GeneralCalibrationData,
        #[deku(id = "16")]
        TemporalEventsData,
    }
    And I'm trying to include it here:
    #[derive(Debug, DekuRead, DekuWrite)]
    pub struct TdfJumptableEntry {
        block_type: TdfBlockType,
        #[deku(ctx = "*block_type")]
        format: TdfFormat,
        offset: i32,
        size: i32,
        creation_date: TimeT,
        last_modification_date: TimeT,
        #[deku(pad_bytes_after = "4")]
        last_access_date: TimeT,
        comment: [u8; 256],
    }
    Marcos Alfredo Núñez
    @marnunez
    I'm new to the rust game, but TdfBlockType is an enum type, and all its variants are u32, right? So why wouldn't it implement Copy?
    @sharksforarms, just tagging you in case you don't get this
    Marcos Alfredo Núñez
    @marnunez
    Ok, I solved it by deriving the Copy and Clone traits in TdfBlockType. Not sure if required, or the most efficient way to do it
    Emmanuel Thompson
    @sharksforarms
    Hey @marnunez sorry, I don't think I have gitter setup properly for notifications completely missed this... do you still need help?
    are you able to link me to an example I could compile?
    Anders Kiel Hovgaard
    @ahovgaard
    Hi. Is there a way to skip a field when decoding, but set it to some default value when encoding? Essentially the opposite behavior of the current skip attribute. In other words, I would like to define a protocol message with some field that exists in the binary representation, but I don't want the field to be present in the struct.
    Anders Kiel Hovgaard
    @ahovgaard
    The temp attribute does what I want when it comes to decoding, but it doesn't include the field in the binary on encoding, which I need.