Transmute() with container types

I hate being that "well ackchyually" guy, but to quote a previous Quote of the Week...

I agree with you that in practice this is almost certainly going to work without any hiccups, but according to Rust's memory model it's UB to transmute between #[repr(Rust)] types.

The only possible exception might be when you can make guarantees around Option<Wrapper>'s layout. As far as I'm aware, that could only happen if Wrapper and f64 contained a "niche" because then we can rely on the "null value optimisation". However, I don't think that'll help in your case because switching to &[Option<&f64>] and #[repr(transparent)] struct Wrapper(&f64) isn't suitable for what you are doing.

1 Like