Opaque FFI types


#1

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?


#2

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


#3

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


#4

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


#5

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?


#6

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.