-
Notifications
You must be signed in to change notification settings - Fork 142
Description
See also: #1122
Currently, we have a number of traits that express some aspect of transmutability:
These, in turn, are used to bound certain Ptr transmutation methods, e.g.:
Both transparent_wrapper_into_inner and as_bytes implement special cases of a generic Ptr<T> to Ptr<U> transmutation. This suggests that we could in theory support a more generic Ptr::transmute method, supported by a new TransmutableFrom trait. This trait would share some similarities with the built-in transmutability trait. However, as TransparentWrapper's support for invariant variance shows, it might be more powerful than that trait in certain ways.
In its most basic form, this API would look something like:
unsafe trait TransmutableFrom<T> {}
impl<'a, T, A: Aliasing> Ptr<'a, T, (A, Aligned, Valid)> {
fn transmute<U: TransmutableFrom<T>>(self) -> Ptr<'a, U, (A, Aligned, Valid)>
}However, we will likely want to relax the Aligned and Valid requirements and post-conditions, and support a generic framework for mapping source invariants (on T) to destination invariants (on U) - essentially a generalization of what's currently supported with TransparentWrapper's invariant variance concept.
There may be multiple overlapping reasons to support transmuting a particular pair of types. For example, we could imagine the following impls:
unsafe impl<T: IntoBytes, U: FromBytes> TransmutableFrom<T> for U {}
unsafe impl<T> TransmutableFrom<T> for MaybeUninit<T> {}Currently, these would result in an impl conflict. If #[marker] traits are stabilized, this won't be an issue - we can just mark TransmutableFrom as a #[marker] trait. Alternatively, we could use a dummy "reason" parameter to disambiguate, as we do with AliasingSafe today:
unsafe trait TransmutableFrom<T, R> {}
enum BecauseIntoBytesFromBytes {}
enum BecauseMaybeUninit {}
unsafe impl<T: IntoBytes, U: FromBytes> TransmutableFrom<T, BecauseIntoBytesFromBytes> for U {}
unsafe impl<T> TransmutableFrom<T> for MaybeUninit<T, BecauseMaybeUninit> {}