Opaque FFI types

I've been told elsewhere that using something like enum Opaque {} is a bad idea for FFI pointers (e.g. *const Opaque). So now I'm using something like:

use std::os::raw::c_void;
#[repr(transparent)] pub struct Opaque(c_void);

#[repr(transparent)] pub struct OpaqueType(*const Opaque);
#[repr(transparent)] pub struct AnotherOpaqueType(*const Opaque);

Is this the right way to do things?

bindgen uses struct Opaque {_private: [u8; 0]}

2 Likes

The best approach is to use "extern types", but unfortunately this feature is not yet stabilized.

1 Like

This is also what the nomicon will recommend once the submodule in rustc has been updated.

2 Likes

Looking at the code for c_void it says:

For LLVM to recognize the void pointer type and by extension functions like malloc(), we need to have it represented as i8* in LLVM bitcode.

Does using a pointer to that struct with repr(C) always have that representation?

I don’t think you want that type of Opaque because it’s no longer a ZST since c_void has hidden variants.

Use the empty array that @kornel suggested until extern types are stable.