Best way to convert between Option<T> and sentinel value for reading AND writing. #421
-
Very similar to a previous question, but not quite answered by it. I have a field in a binary struct that is a |
Beta Was this translation helpful? Give feedback.
Answered by
wcampbell0x2a
Feb 24, 2024
Replies: 1 comment
-
I think I would just use my own enum: use deku::prelude::*;
use dbg_hex::dbg_hex;
#[derive(Debug, DekuWrite, DekuRead)]
#[deku(type = "u32")]
enum Value {
#[deku(id = 0xffffffff)]
None,
#[deku(id_pat = "_")]
Some(u32)
}
fn main() {
let data = [0xff, 0xff, 0xff, 0xff];
let v = Value::from_bytes((&data, 0)).unwrap().1;
assert_eq!(data, &*v.to_bytes().unwrap());
dbg_hex!(v);
let data = [0x01, 0x02, 0x03, 0x04];
let v = Value::from_bytes((&data, 0)).unwrap().1;
assert_eq!(data, &*v.to_bytes().unwrap());
dbg_hex!(v);
}
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
wcampbell0x2a
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think I would just use my own enum: