Borrowing & bitfields & other field recoding & packing

Previously:

I vaguely recall some other proposals that attacked this problem slightly differently, but the proposal I remember the best is my own, so this is the one I am going to plug.

As for #[repr(packed)], I have been wondering a couple of times whether a wrapper type could work better than an attribute. Something like:

#[repr(C)]
struct CrushdTinBox {
    years_of_waiting: u8,
    weird_fishes: [Packed<Sardine>; 16],
}

where Packed<T> is guaranteed to have an alignment of 1, but otherwise the same memory representation as T, so we could have unsafe fn assume_aligned(&Packed<T>) -> &T and fn get(&Packed<T>) -> T where T: Copy. To make it truly expedient we would have to add some kind of language feature that would enable struct member access to ‘factor through’ the wrapper. But we already need to solve that problem for Cell, MaybeUninit and other such wrappers, so… that just adds more justification to what is already a wanted feature.

1 Like