Skip to content

Packing for nested structs wrong when using partial integer types #99

Open
@paulzhng

Description

@paulzhng

Hi,

I noticed that packing nested structs fails for partial Integer types when used in inner structs. Weirdly enough, this is only applicable for bits smaller than 5.

Following fields packed result in [0] being outputted.

use packed_struct::prelude::*;

#[derive(PackedStruct, PartialEq, Debug)]
#[packed_struct(bit_numbering = "msb0", endian = "msb")]
pub struct Outer {
    #[packed_field(bits = "0")]
    inner_field: Inner,
}

#[derive(PackedStruct, PartialEq, Debug)]
#[packed_struct(bit_numbering = "msb0", endian = "msb")]
pub struct Inner {
    #[packed_field(bits = "0")]
    val: Integer<u8, packed_bits::Bits<1>>,
}

fn main() {
    let outer = Outer {
        inner_field: Inner { val: 1.into() },
    };
    let packed = outer.pack().unwrap();
    let unpacked = Outer::unpack_from_slice(&packed).unwrap();

    println!("unpacked: {:#}", unpacked);
    println!("datetime: {:#}", outer);
    assert_eq!(outer, unpacked);
}

Code example also available here: rustexplorer.com.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions