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

Opaque types don't include !Send, !Sync, !Pin #2685

Open
CGMossa opened this issue Nov 16, 2023 · 5 comments
Open

Opaque types don't include !Send, !Sync, !Pin #2685

CGMossa opened this issue Nov 16, 2023 · 5 comments

Comments

@CGMossa
Copy link
Contributor

CGMossa commented Nov 16, 2023

We have a pointer type to an opaque type in our FFI project.

Bindgen generates this definition for the opaque type:

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct SEXPREC {
    _unused: [u8; 0],
}

But then I was reading https://stackoverflow.com/questions/38315383/whats-the-rust-idiom-to-define-a-field-pointing-to-a-c-opaque-pointer, and https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs,
which says that the definition of the above should really be:

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

Is this something I should replace the bindgen-version with this?

Lastly, I merely have these in the C-headers:

typedef struct SEXPREC s_object;

with no definition of s_object, and I'm quite certain this is intended behavior.

@CGMossa
Copy link
Contributor Author

CGMossa commented Nov 18, 2023

See rust-lang/nomicon#250

@Lokathor
Copy link
Contributor

Lokathor commented Nov 18, 2023

Hello. I was asked to look at this issue but I don't have any wider context of your specific project.

That said, I'd advise the following:

/// S-expression data that is opaque to Rust
#[repr(transparent)]
pub struct SEXPREC(c_void);

// if you want this alias too
pub type s_object = SEXPREC;

I'm genuinely not sure why the Nomicon is concerned with the value itself being Send/Sync or not because pointers to it will naturally be !Send and !Sync. The c_void field will also correctly make the type Unpin.

@CGMossa
Copy link
Contributor Author

CGMossa commented Nov 18, 2023

Thanks! My use case requires pub type SEXP = *mut SEXPREC;.

So

/// S-expression data that is opaque to Rust
#[repr(transparent)]
pub struct SEXPREC(c_void);

// if you want this alias too
pub type SEXP = *mut SEXPREC;

Is it possible to make bindgen produce this pattern?

@Lokathor
Copy link
Contributor

I haven't used bindgen in years, I just make bindings by hand generally. You would probably have to file an issue to make them support this.

@HadrienG2
Copy link

HadrienG2 commented Feb 12, 2024

I agree that the current codegen for opaque types is unnecessarily dangerous and should be changed to follow the current nomicon guidelines.

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