Skip to content

How to use deku::input #323

Answered by wcampbell0x2a
konradmoesch asked this question in Q&A
Discussion options

You must be logged in to vote

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,
        fi…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@konradmoesch
Comment options

Answer selected by wcampbell0x2a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants