You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I'm working on a crate that uses Deku for a format like this:
structPacket{header:PacketHeader,#[deku(ctx = "header")]payload:Payload,}implPacket{fnupdate_packet_size(&mutself){// walk through the packet and determine how large// it is, then set `self.packet.packet_size`.}}structPacketHeader{packet_type:u16,packet_size:u16,}#[deku(ctx = "packet_header: &PacketHeader", id = "packet_header.packet_type()")]enumPayload{#[deku(id = "PacketType::Type1")]Type1(Type1),#[deku(id = "PacketType::Type2")]Type2(#[deku(ctx = "packet_header")]Type2),
...
}structType2{#[deku(count = "packet_header.packet_size")]data:Vec<u8>,}implType2{fnset_data(&mutself,bytes:&u8]){// set `self.data`}}
In English, there's a packet header that identifies the type of packet and the packet size. For packet types that require knowledge of the size during parsing, I can pass in the header like I do for Type2.
But I'm trying to sort out how to handle setting the packet size when I make changes to the structure. Say I generate a new packet, then go add payload data to the Type2 payload via set_data().
I could have the user run update_packet_size() after making any modifications to the data, but as I'm providing a public API of getters and setters for any fields, it seems like I should be able to abstract that away by running the update in my setters.
Something like:
implType2{fnset_data(&mutself,bytes:&u8]){// set `self.data`
parent.update_packet_size();}}
This ends up being tricky as set_data() requires a &mut self reference, so I can't just pass in another mutable reference to the parent Packet.
Another approach I thought about was trying to inject some logic into the DekuWrite implementation such that it would run update_packet_size() just before serialization (kind of the opposite of how it's using packet_size to determine how much to read during de-serialization).
Is there some obvious way to do what I'm trying to do and abstract the update_packet_size() (or update()) calls from my crate's users?
Thanks so much for all the work on Deku - it's really a pleasure to use!
The text was updated successfully, but these errors were encountered:
Hi, I'm working on a crate that uses Deku for a format like this:
In English, there's a packet header that identifies the type of packet and the packet size. For packet types that require knowledge of the size during parsing, I can pass in the header like I do for
Type2
.But I'm trying to sort out how to handle setting the packet size when I make changes to the structure. Say I generate a new packet, then go add payload data to the
Type2
payload viaset_data()
.I could have the user run
update_packet_size()
after making any modifications to the data, but as I'm providing a public API of getters and setters for any fields, it seems like I should be able to abstract that away by running the update in my setters.Something like:
This ends up being tricky as
set_data()
requires a&mut self
reference, so I can't just pass in another mutable reference to the parentPacket
.Another approach I thought about was trying to inject some logic into the
DekuWrite
implementation such that it would runupdate_packet_size()
just before serialization (kind of the opposite of how it's usingpacket_size
to determine how much to read during de-serialization).Is there some obvious way to do what I'm trying to do and abstract the
update_packet_size()
(orupdate()
) calls from my crate's users?Thanks so much for all the work on Deku - it's really a pleasure to use!
The text was updated successfully, but these errors were encountered: