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

Generic transmutability #1359

Open
joshlf opened this issue May 30, 2024 · 0 comments
Open

Generic transmutability #1359

joshlf opened this issue May 30, 2024 · 0 comments

Comments

@joshlf
Copy link
Member

joshlf commented May 30, 2024

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> {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant