sharksforarms / deku

Declarative binary reading and writing: bit-level, symmetric, serialization/deserialization
Apache License 2.0
1.05k stars 54 forks source link

Doc request: update for structs #409

Open sempervictus opened 5 months ago

sempervictus commented 5 months ago
#[deku(update = "another_field.some_calls_returning_new_value()")

doesn't really work for structs with multiple members when we need to perform another_field.some_calls_returning_new_value() for this.member since rust doesn't allow calls up to the parent from the struct member to be updated, a bit confused on the semantic required.

#[deku_derive(DekuRead, DekuWrite)]
#[derive(Debug, PartialEq)]
#[deku(endian = "endian", ctx = "endian: Endian")]
pub(super) struct UbString {
    #[deku(update = "self.hdr.length = self.val.len() as u16")]
    pub(super) hdr: UbHdr,
    #[deku(count = "hdr.length")]
    pub(super) val: Vec<u8>,
}

doesn't work either due to a somewhat opaque

error[E0277]: the trait bound `UbHdr: From<()>` is not satisfied
...
   |
65 | #[deku_derive(DekuRead, DekuWrite)]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<()>` is not implemented for `UbHdr`
   |
   = note: required for `()` to implement `Into<UbHdr>`
   = note: required for `UbHdr` to implement `TryFrom<()>`
   = note: required for `()` to implement `TryInto<UbHdr>`
   = note: this error originates in the attribute macro `deku_derive` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.

error.

Trying to do this by creating a whole new UbHdr struct in the update macro fails with the same From related error even though in neither case is there any sort of type conversion given that i'm providing the correct type to either the struct member or the entire struct replacment.