Closed mmmfarrell closed 1 year ago
If you do cargo doc
for only the two structs building, you can analyze what functions are generated from deku.
I modified the project structure for doc
:
use deku::prelude::*;
use crate::{DekuBase, DekuTest};
fn main() {
let data: Vec<u8> = vec![0xAB, 0xAC, 0xBE, 0xEF];
let (_rest, mut val) = DekuTest::from_bytes((data.as_ref(), 0)).unwrap();
assert_eq!(DekuTest {
base: DekuBase { field_a: 0xAB, field_b: 0xAC },
field_b: 0xBE,
field_c: 0xEF,
}, val);
val.field_c = 0xFF;
let data_out = val.to_bytes().unwrap();
assert_eq!(vec![0xAB, 0xAC, 0xBE, 0xFF], data_out);
//let data_base: Vec<u8> = vec![0xBE, 0xEF];
//let (_rest, mut val) = DekuBase::from_bytes((data_base.as_ref(), 0)).unwrap();
//assert_eq!(DekuBase {
// field_a: 0xBE,
// field_b: 0xEF,
//}, val);
//println!("pass\n");
}
use deku::prelude::*;
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(endian = "endian", ctx = "endian: deku::ctx::Endian")]
pub struct DekuBase {
field_a: u8,
field_b: u8,
}
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(endian = "little")]
pub struct DekuTest {
base: DekuBase,
field_b: u8,
field_c: u8,
}
Now you can run cargo doc
and see the diff between the two. I would write some code that uses only read() but I'm short on time today
The docs @ https://docs.rs/deku/latest/deku/trait.DekuContainerRead.html give the container context constraints.
Hey @mmmfarrell sorry for not getting back to you sooner.
The error message really isn't great here...
Basically, you're trying to read with DekuBase
which requires a context ctx
argument endian
, which, is normally passed by the parent struct.
But because you're using it on it's own, it doesn't know what endian
is and the proc_macro doesn't generate the code to read. So, we need to specify this default with ctx_default
.
https://docs.rs/deku/latest/deku/attributes/index.html#ctx_default
use deku::prelude::*;
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
- #[deku(endian = "endian", ctx = "endian: deku::ctx::Endian")]
+ #[deku(endian = "endian", ctx = "endian: deku::ctx::Endian", ctx_default = "deku::ctx::Endian::Little")]
struct DekuBase {
field_a: u8,
field_b: u8,
}
Closing for now. Feel free to re-open if your issue isn't solved
I am having trouble getting this example working when I want to be able to serialize / deserialize both the child and parent struct of a nested data type. Any help would be much appreciated!
This is related to #183 , but not identical b/c in that issue, the child type was never serialized independent of the parent type.
Example:
produces the following error:
Or if I change the DekuBase class to have the following macros
I get the following errors: