Skip to content

DekuSize does not include bytes from magic attribute #635

@hogg-sy-wang

Description

@hogg-sy-wang

DekuSize is very useful to me, but I noticed an issue: the bytes occupied by the magic attribute are not being counted in SIZE_BYTES.

In some scenarios, I need to calculate the total number of bytes written to a file, which means the bytes from the magic attribute are always missing.

Here’s an example:

use deku::prelude::*;

#[derive(DekuSize)]
#[deku(magic = b"DEMO")]
struct Demo {
    foo: u32,
    bar: u32,
}

impl Demo {
    const SIZE: usize = Self::SIZE_BYTES.unwrap();
}

fn main() {
    let size = Demo::SIZE;
    assert_eq!(size, 12);
}

Running via cargo run:

thread 'main' panicked at src/main.rs:16:5:
assertion `left == right` failed
  left: 8
 right: 12
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Expected behavior:
SIZE_BYTES should include the 4 bytes from the magic attribute, so the total should be 12 instead of 8.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions