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

Meta types to replace derive logic #1948

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

Meta types to replace derive logic #1948

joshlf opened this issue Oct 19, 2024 · 0 comments

Comments

@joshlf
Copy link
Member

joshlf commented Oct 19, 2024

Imagine the following type:

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

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:

#[derive(KnownLayout)]
#[repr(C)]
struct Foo<A, B: ?Sized> {
    a: A,
    b: B,
}

By emitting:

unsafe impl<A, B: ?Sized> KnownLayout for Foo<A, B>
where
    ReprC<(A, B)>: KnownLayout
{
    type PointerMetadata = <ReprC<(A, B)> as KnownLayout>::PointerMetadata;
    const LAYOUT: DstLayout = <ReprC<(A, B)> as KnownLayout>::LAYOUT;

    fn raw_from_ptr_len(bytes: NonNull<u8>, meta: Self::PointerMetadata) -> NonNull<Self> {
        let ptr = <ReprC<(A, B)> 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 {
        <ReprC<(A, B)> 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.

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