packed_struct.rs
packed_struct.rs copied to clipboard
Allow for split integers
I want to use packed_struct to implement this protocol. Unfortunately it uses split integers. By that I mean an integer which is derived by concatenating multiple bit sequences together. For example, the subprotocol is the number one gets by the following expression: (buf[0] & 1)<<5 | (buf[1] & 0b11111) | (buf[26] & 0b11000000)
. Is it possible to make this work in packed_struct with reasonable effort?
Edit: Actually flipping bits would be useful as well, but there is ways to work around it.
This seems like a very niche problem, if there are more protocols like this in the wild (I've never encountered one) then I might be interested in this. Internally, all of the fields are represented by bits ranges (from bit 0 to bit 7, for instance), so that would need to be rewritten into a list-of-ranges-or-bits structure.
For your use case, I'd define three fields with smaller bit widths, then simply implement a method on the structure to either set or get the final value from these three small integers.
Ok then. I also was kind of stunned when I first saw that protocol, but I guess that's what happens if evolution hits a project.
Garmin's ANT+ Environment Profile also suffers from this problem, I think it was more of a design error though because I have yet to see it any other profile and I have read probably half of them