Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
udoprog opened this issue Jan 27, 2020 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@udoprog
Copy link
Owner

udoprog commented Jan 27, 2020

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 udoprog added the help wanted Extra attention is needed label Jan 27, 2020
@udoprog udoprog changed the title Can Vec<T> be safely coerced to Vec<U> if T has identical layout to U? Can we prove that Vec<T> is safe to coerce into Vec<U> if T has identical layout to U? Jan 27, 2020
@eholk
Copy link
Collaborator

eholk commented Mar 30, 2022

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.

@udoprog
Copy link
Owner Author

udoprog commented Jun 29, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants