smessmer / binary-layout

The binary-layout library allows type-safe, inplace, zero-copy access to structured binary data. You define a custom data layout and give it a slice of binary data, and it will allow you to read and write the fields defined in the layout from the binary data without having to copy any of the data. It's similar to transmuting to/from a #[repr(packed)] struct, but much safer.
Apache License 2.0
66 stars 9 forks source link

Nested structure as an array? #24

Open adam900710 opened 11 months ago

adam900710 commented 11 months ago

Hi,

The project is really awesome for me to parsing on-disk filesystem data, it's not too complex compared to nom, but still supported nested structures.

However I am hitting a case where I have something like this in C:

struct btrfs_root_backup {
    /* Some u8/u16/u32/u64 etc. */
};

struct btrfs_super_block {
    /* Some other u8/u16/u32/u64 */
    struct btrfs_root_backup backup_roots[4];
}

In above case, how can I specify the nested rust structure for btrfs_super_block::backup_roots[4]?

Tried something like the following:

define_layout!(btrfs_super_block, LittleEndian, {
    csum: [u8; BTRFS_CSUM_SIZE],
    ... 
    super_roots: [btrfs_root_backup::NestedView; 4],
});

But of course it failed to compile.

For now I can easily workaround it by just defining super_roots into 4 members, but I'd like to know if there is any better solution.

Thanks, Qu

smessmer commented 8 months ago

Hi, thanks for reaching out. The only array type currently supported is [u8]. Any other array types aren't supported yet, see the README.

Nested views in arrays sound like the natural follow-up once general array types are supported. But as mentioned in the README, I likely won't have time to add this feature. But I'm willing to review and accept PRs.

lvella commented 4 months ago

Do you have a rough idea on how to implement arrays of primitives?

Turns out I need both: arrays of primitives and arrays of nested views.