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

Can we use non_exhausive attr instead of zero length array in "Representing Opaque Structs" #342

Open
Havvy opened this issue Feb 20, 2022 · 2 comments

Comments

@Havvy
Copy link
Contributor

Havvy commented Feb 20, 2022

Section: https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs

Currently, it uses a struct like this:

#[repr(C)]
pub struct Foo {
    _data: [u8; 0],
    _marker:
        core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}

But since that was written, we have access to the #[non_exhausive] attribute. I think it has the same functionality. Any reason we shouldn't be suggesting this instead?

#[repr(C)]
#[non_exhaustive]
pub struct Foo {
    _marker:
        core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}
@JohnTitor
Copy link
Member

JohnTitor commented Mar 17, 2022

I guess it's just not updated (we have a lot of outdated content!), I'm happy to see a PR :)

@reisnera
Copy link

Is there a benefit to adding #[non_exhaustive] or even having the _data member at all? PhantomData itself is a ZST no? Also I feel like you could just as well use () rather than u8 for the pointer just to further remove any suggestion that this is anything other than a trick to make an opaque FFI type. I might be missing something but wouldn't the following be just as good?

#[repr(C)]
pub struct Foo {
    _marker: core::marker::PhantomData<(*mut (), core::marker::PhantomPinned)>,
}

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

3 participants