#struct to bits
6 messages · Page 1 of 1 (latest)
You can't address individual bits, only at the byte level. Bytes before the crossover bit can be taken wholesale, as can bytes after the crossover bit
For the byte containing the crossover bit, you would need to use bitwise operations on the two bytes
You'd need to implement your own logic for this most likely but perhaps someone can provide a crate already doing so, I'm not sure of one
For getting the actual bytes of the structs, you can use unsafe with the raw pointers, or for a little extra safety you could use a crate like bytemuck
It still requires unsafe AFAIK to implement the traits required for your type to allow bytemuck operations to be safe because there are a bunch of guarantees your type must adhere to in order for it to be safe to arbitrarily muck with the bytes
Bytemuck can automatically check it in it's proc macros if your type is well formed enough
I would recommend just doing the "high level" way to do the operation. The optimizer should make it as optimal as reasonably
I'm trying to use a crate like: https://docs.rs/bitvec/latest/bitvec/view/trait.BitView.html#tymethod.view_bits_mut
Bit View
Looks like [u8] should implement that trait, and bytemuck provides a safe way to get a &mut [u8] from an instance of your type so should be fairly easy with those two in that case