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

Support deriving TransmuteFrom #1941

Open
joshlf opened this issue Oct 18, 2024 · 0 comments
Open

Support deriving TransmuteFrom #1941

joshlf opened this issue Oct 18, 2024 · 0 comments

Comments

@joshlf
Copy link
Member

joshlf commented Oct 18, 2024

A hypothetical #[derive(TransmuteFrom)] could emit a number of useful impls:

#[derive(TransmuteFrom)]
#[repr(C)]
struct Foo {
    a: u8,
    b: u16,
    c: u32,
}

// Emitted by the derive
unsafe impl TransmuteFrom<Foo> for u8 { ... }

Further, imagine that we define a type like the following:

// Has the same layout as a `#[repr(C)]` struct with `L` fields
struct ReprC<L: TyList> { ... }

We could also emit:

unsafe impl TransmuteFrom<Foo> for ReprC<(u8, u16)> { ... }
unsafe impl TransmuteFrom<Foo> for ReprC<(u8, (u16, u32))> { ... }
unsafe impl TransmuteFrom<ReprC<(u8, (u16, u32))>> for Foo { ... }

If we can ensure that U: TransmuteFrom<T>, V: TransmuteFrom<U> implies V: TransmuteFrom<T>, then any #[repr(C)] struct with the same sequence of field types would be transmutable from and into Foo.

For types with uninit tails, we could support size extension:

#[derive(TransmuteFrom)]
#[repr(C)]
struct Bar {
    a: u8,
    b: u16,
    c: MaybeUninit<u32>,
}


// Emitted by the derive
unsafe impl TransmuteFrom<ReprC<(u8, u16)>> for Bar { ... }
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