Closed whati001 closed 5 months ago
Deku does not have a skip_serializing_if
attribute, but could add this.
@whati001 I believe by combining skip
and cond
you can achieve what you're looking for?
You're right there isn't something like this for 'write side only' or 'read side only'
You can interpret it as "skip
field (read or write) if cond
"
Here's an example.
use deku::prelude::*;
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct MyStruct {
should_next: bool,
#[deku(skip, cond = "!*should_next")]
// on read: if cond == true, we skip and the value is set to Default::default()
// on write: if cond == true, we skip and the writing of the value
optional: Option<u8>,
other_field: u8,
}
fn main() {
env_logger::init();
{
let input = &[0x01, 0x02, 0x03];
let (_rest, r) = MyStruct::from_bytes((input, 0)).unwrap();
assert_eq!(
r,
MyStruct {
should_next: true,
optional: Some(0x02),
other_field: 0x03,
}
);
assert_eq!(vec![0x01, 0x02, 0x03], r.to_bytes().unwrap())
}
{
let input = &[0x00, 0x03];
let (_rest, r) = MyStruct::from_bytes((input, 0)).unwrap();
assert_eq!(
r,
MyStruct {
should_next: false,
optional: None,
other_field: 0x03,
}
);
assert_eq!(vec![0x00, 0x03], r.to_bytes().unwrap())
}
}
Hi @sharksforarms
Thank you very much for the quick feedback. You are right, seems like I have overseen to add the skip
attribute.
Using the following deku #[deku(skip, cond = '...']
works now for me.
Hi all,
would it be possible to add support for conditional write? For example, I would like to skip the serialization of specific fields based on there value as explained in the example below:
Struct to serialize into binary:
And now, I would like to skip the serialization based on the
serialize
flag.I have tried to use
#[serde(skip_serializing_if = ..
, but this has not worked for me.