Skip to content

Generic transmutability #1359

Open
1 of 3 issues completed
Open
1 of 3 issues completed
@joshlf

Description

@joshlf

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> {}

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions