#struct to bits

6 messages · Page 1 of 1 (latest)

next yacht
#

I have two instances of the same struct, I'd like to do a cross over at a given bit. How can I address a struct at a certain bit?

toxic pivot
#

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

molten bay
#

Bytemuck can automatically check it in it's proc macros if your type is well formed enough

molten bay
next yacht
toxic pivot
#

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