How to use deku::input #323
Unanswered
konradmoesch
asked this question in
Q&A
Replies: 1 comment 1 reply
-
The following example might help you: use deku::bitvec::{BitSlice, Msb0};
use deku::prelude::*;
#[derive(DekuRead, Debug, PartialEq, Eq)]
struct Block {
other: u16,
#[deku(assert_eq = "Self::calculate_checksum(deku::rest, 3)")]
checksum: u8,
field_a: u8,
field_b: u8,
field_c: u8,
}
impl Block {
pub fn calculate_checksum(deku_rest: &BitSlice<u8, Msb0>, block_size: usize) -> u8 {
let bytes: &[u8] = deku_rest.domain().region().unwrap().1;
bytes.iter().take(block_size).sum::<u8>()
}
}
fn main() {
let block = Block {
other: 0xffff,
checksum: 0b0000_0111,
field_a: 0b0000_0001,
field_b: 0b0000_0010,
field_c: 0b0000_0100,
};
let bytes = &[
0xff,
0xff,
0b0000_0111,
0b0000_0001,
0b0000_0010,
0b0000_0100,
];
let test_block = Block::from_bytes((bytes, 0)).unwrap().1;
assert_eq!(block, test_block);
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello, first of all thanks for the crate!
I have a file containing data blocks that start with a checksum and then some other fields, like this:
I can successfully read the fields, but now I want to check whether the checksum is valid, maybe using
#[deku(assert_eq = "calculate_checksum(deku::input, 200, 32)")]
where 200 is the offset of the write block and 32 is the block size in bytes.
WriteBlock
is a member of another structDataset
, that is read usingDataset::from_bytes()
However, I get the error
cannot find value '__deku_input' in this scope
. How can I usedeku::input
, and is this the way to go to get the raw bytes? Since the checksum field is at the beginning of the block, I also thought of usingdeku::rest
, but this contains the data bit-wise I think.Thanks for your help!
Beta Was this translation helpful? Give feedback.
All reactions