Lokathor / bytemuck

A crate for mucking around with piles of bytes
https://docs.rs/bytemuck
Apache License 2.0
697 stars 77 forks source link

Don't require T to be AnyBitPattern for MaybeUninit<T> to be AnyBitPattern #249

Closed tyilo closed 3 weeks ago

tyilo commented 3 months ago

MaybeUninit<T> is always valid for any bit pattern even if T is not.

zachs18 commented 3 months ago

This was going to be implemented in #152, but was discovered to possibly be unsound in general (https://github.com/Lokathor/bytemuck/pull/152#issuecomment-1336252308), so a never-unsound-but-more-conservative implementation was implemented instead (#160, require T to be AnyBitPattern so we know it has no interior mutability).If Freeze was stable, then this would be possible (either impl<T: Freeze + Copy + 'static> AnyBitPattern for MaybeUninit<T>, or under #153, impl<T> AnyBitPattern for MaybeUninit<T>).

tyilo commented 3 months ago

Ah, so this is only sound if implementing Copy means a type also implements Freeze.

As I understand it, there are currently no !Freeze type that implements Copy so this change would be sound for the current version of Rust.

However this could change in a future version of Rust, where this change would then be unsound.

Lokathor commented 1 month ago

I've heard occasional rumblings of wanting Freeze to be a a stable trait some day. It wouldn't be any time soon, but more people are coming around to it being quite useful. So, I think that we should wait until that happens.

Lokathor commented 3 weeks ago

I'm going to close this until Rust as a language develops further, and then maybe someday later we can revisit the concept.