packed_struct.rs icon indicating copy to clipboard operation
packed_struct.rs copied to clipboard

Allow for split integers

Open wucke13 opened this issue 4 years ago • 3 comments

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.

wucke13 avatar Dec 03 '20 01:12 wucke13

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.

rudib avatar Dec 03 '20 16:12 rudib

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.

wucke13 avatar Dec 03 '20 17:12 wucke13

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

cujomalainey avatar Jan 22 '22 14:01 cujomalainey