google / google/zerocopy

Support deriving `TransmuteFrom`

Open
#1,941 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
2.6k
Forks
179
Avg merge
1d 19h
Merged PRs (30d)
29

Description

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

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

// Emitted by the derive
unsafe impl TransmuteFrom for u8 { ... }
```

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

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

We could also emit:

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

If we can ensure that `U: TransmuteFrom, V: TransmuteFrom` implies `V: TransmuteFrom`, 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:

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

// Emitted by the derive
unsafe impl TransmuteFrom> for Bar { ... }
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.