Is there a built in way to read/write a vector of bits/bool?
yanshay opened this issue · comments
In the structure I read/write there's a section that represent bits. It begins with a u8 that include the number of bits and then as many u8 as needed (rounded up) that include the bits:
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct Container {
qty: u8,
#[deku(bytes_read = "((qty+7)/8)")]
bits_data: Vec<u8>,
}
So for example if qty is 12, there are two bytes with the 12 bits of data (and 4 empty).
I am able to read/write it into Vec as shown above but it's inconvenient later to deal with the bits.
- I couldn't find a way to read/write into a u32 for example (assuming I know it may be at most 32 bits)
- I couldn't find a way to read/write into a Vec, it tried to read the u8 value into the bool rather than read the bits one by one into the bools (which looks like a bug to me)
- I would even prefer to read/write into BitVec for easier access and optimal memory footprint.
I could probably do it with reader/writer, but is there a built in way to do either of the two options I mentioned?
Away from my computer, but this should work:
use deku::prelude::*;
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct Bit(#[deku(bits = "1")] u8);
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct Container {
qty: u8,
#[deku(count = "qty")]
bits_data: Vec<Bit>,
}
I would even prefer to read/write into BitVec for easier access and optimal memory footprint.
Interesting idea, I'd support a MR for this.
Thanks, that works. I also added padding as in the code below to align on bytes boundary. Its also required for try_form
not to fail on too much data. See below.
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct BitContainer {
qty: u8,
#[deku(count = "qty", pad_bits_after="(qty+7)/8*8-qty")]
bits_data: Vec<Bit>,
field_after: u8
}
What's still missing is automatic filling of qty on write, so I won't need to fill it in the code.
- I can't do it using writer, because the
pad_bits_after
would not work. On read I need padding to refer to the readqty
but on write I need it to refer to the length ofbits_data
. (Interestingly, from my tests, on write deku ignores the 'qty' and decides how many bits to save based on theVec<Bit>
size, I think this is worth explaining in the documentation) - I can solve this using
update
on theqty
field. But that's error prone since it requires client code to remember to update prior to save. I think there would be value in having an attributeupdate_on_write
that executes right before write and has access to self that can calculate automatically fields that can be deduced from other fields - I can do it using writer/reader for only
bits_data
and expose only bits_data with qty taken care of behind the scene. - Could I define a new type
type MyVecBit = Vec<Bit>
and implement relevant traits to do the same as 4, only seamlessly from the person defining the structure? Any examples for that? Because this pattern will be used in multiple structures and I don't want to have verbose definitions all over
I think there would be value in having an attribute update_on_write that executes right before write and has access to self that can calculate automatically fields that can be deduced from other fields
Agreed, this would need to be a breaking change, as DekuWrite
doesn't mutate self
.