Bitfields in swift seem really unsafe

I wonder if you'd encounter any issues with alignment if you use unsafeBitCast.

> MemoryLayout<(Int8, Int8)>.alignment
$R0: Int = 1
> MemoryLayout<Int16>.alignment 
$R1: Int = 2

I don't think the documentation makes any guarantees about that, so it may not be safe to assume anything. Does it always make a copy in to correctly-aligned storage and never optimise that out?

EDIT: Seems it's fine. Even though it's a value-level operation, I was concerned the compiler would assume the original value was correctly-aligned for the new type and optimise out copying the bits, but apparently it should be considered a bug if it does that.

1 Like