unicycle icon indicating copy to clipboard operation
unicycle copied to clipboard

Can we prove that Vec<T> is safe to coerce into Vec<U> if T has identical layout to U?

Open udoprog opened this issue 4 years ago • 2 comments

If so, the library could currently make use of an assumption that &mut Vec<AtomicUsize> can be safely coerced to &mut Vec<usize>.

This is desirable since it would let us use Vec<T> more directly in both BitSet and AtomicBitSet, leading to less work, less unsafe, and less code.

Note: You can test this by explicitly disabling the vec-safety feature in which case we will fall back to using a vector. Otherwise we make use of a wrapper for all vector operations which has a fixed representation #[repr(C)], and mimics all the vector APIs we use. This feature currently exists for benchmarks and will most likely be removed in the future.

udoprog avatar Jan 27 '20 15:01 udoprog

This sounds like something that safe transmute might help with if/when that becomes a thing. In the meantime, perhaps the zerocopy crate would work? Although, it looks like zerocopy is more narrowly focused on casting between a T and a [u8], which is probably overly specific for this crate's needs.

eholk avatar Mar 30 '22 18:03 eholk

Note that there's a note under transmute which covers this. Thanks to ark from the Rust language Discord (turning a Vec<&T> into a Vec<Option<&T>>): https://doc.rust-lang.org/core/mem/fn.transmute.html#alternatives

udoprog avatar Jun 29 '22 23:06 udoprog