Meta types to replace derive logic
- Dominant language
- Rust
- Stars
- 2.6k
- Forks
- 179
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 29
Description
Imagine the following type:
```rust
// Has the same layout as a `#[repr(C)]` struct with `L` fields
struct ReprC { ... }
```
Imagine further that this type has hand-rolled impls of all of the zerocopy traits.
We could then simplify our derives to handle types like:
```rust
#[derive(KnownLayout)]
#[repr(C)]
struct Foo {
a: A,
b: B,
}
```
By emitting:
```rust
unsafe impl KnownLayout for Foo
where
ReprC<(A, B)>: KnownLayout
{
type PointerMetadata = as KnownLayout>::PointerMetadata;
const LAYOUT: DstLayout = as KnownLayout>::LAYOUT;
fn raw_from_ptr_len(bytes: NonNull, meta: Self::PointerMetadata) -> NonNull {
let ptr = as KnownLayout>::raw_from_ptr_len(bytes, meta).as_ptr();
let ptr = ptr as *mut Self;
unsafe { NonNull::new_unchecked(ptr) }
}
fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata {
as KnownLayout>::pointer_to_metadata(ptr as *mut ReprC<(A, B)>)
}
}
```
We could presumably do something similar for other traits, and we could presumably support other `#[repr(...)]` attributes and other kinds (enums and unions). Ideally this would let us drastically simplify the internals of our derives.
Contributor guide
Assessment
This issue has not been assessed yet.