wojciech-graj / bin-proto

Simple bit-level protocol definitions in Rust.
MIT License
17 stars 1 forks source link

Seek/Peek support #4

Open vhdirk opened 6 days ago

vhdirk commented 6 days ago

My weird protocol has a 'operation' structure like this:

struct SomeOperation {
   #[protocol(bits=1)]
   b7: u8,
   #[protocol(bits=1)]
   b6: u8,

   #[protocol(bits=6)]
   opcode: u8,

  # more data fields as defined by 'SomeOperation'
   data: SomeOperationData,
}

At first glance, you would think that opcode would define how data should be parsed. And that's mostly true, were it not for the small but very annoying detail that the 2 first bits (b7 and b6) are actually part of data. Depending on opcode, b7 and b6 might be 2 independent variables, but they may as well be one 2 bit field.

So to parse this particular operation, I need to skip 2 bits, read the opcode and then rewind the reader. Or succinctly, just peek at bits 5 through 0. And the reverse when serializing the whole thing again.

It would be quite cool (yet probably quite confusing) to just read the opcode, rewind, and read the whole thing again, jumping over the bits that have already been read before.

Any ideas ? :)