Closed vext01 closed 2 years ago
Here is the documentation around enums: https://docs.rs/deku/latest/deku/index.html#enums
Without knowing your full data format, here's an example:
#[derive(Debug, DekuRead)]
struct IPBytes {
#[deku(bits = "3")]
val: u8
}
#[derive(Debug, DekuRead)]
#[deku(id = "ip_bytes", ctx = "ip_bytes: u8")]
enum TargetIP {
#[deku(id = "0b001")]
Ip16(u16),
#[deku(id = "0b010")]
Ip32(u32),
#[deku(id_pat = "0b011 | 0b100")]
Ip48(#[deku(bits = "48")] u64),
#[deku(id = "0b110")]
Ip64(u64),
}
#[derive(Debug, DekuRead)]
struct Container {
ip_bytes: IPBytes,
#[deku(ctx = "ip_bytes.val")]
target_ip: TargetIP,
}
Closing for now, feel free to re-open
Thanks! That was exactly what I was after.
Hi again,
Was just implementing a struct like this:
The idea being that only one of the fields is
Some
.And then though: "Wait a gosh-darned minute.... This is an enum!"
But reading the docs, it looks like I couldn't select an enum discriminant based on
cond
derived fromctx
?Imagine:
It would be a neat feature.