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

Is it possible to have nested layouts? #17

Closed dheater closed 2 years ago

dheater commented 2 years ago

Is it possible to have nested layouts?

For example, I have a multibyte header that I want to define and reuse as a field in other layouts

Something like this:

define_layout!(header_t, BigEndian, {
    version: u16,
    length: u16,
};

define_layout!(packet, BigEndian {
    header: header_t,
    field1: u16,
    field2: u16,
    data: [u8],
}
smessmer commented 2 years ago

not currently, but I'm working on it. You can take a look at the "feature/nesting" branch and the tests/nested.rs file in there if you want a preview.

smessmer commented 2 years ago

Nested layouts are implemented in https://github.com/smessmer/binary-layout/commit/3b67f467c7d390125376ba5d2430136e47da6232 and will be part of the 3.0 release