FFI - Opaque type constant use

Hi,
I'm trying to write a wrapper for a c library (libjit) for a JIT compiler and I struggle with the following.
There's a struct jit_type_t defined without any hints about its structure, so I'm guessing it's an opaque type.
There are also constants of this type defined like this: (in C)

extern jit_type_t const jit_type_int;
extern jit_type_t const jit_type_long; // and so on...

I wrote the type definition in Rust as following (based on the manual):

#[repr(C)] pub struct jit_type_t { _private: [u8; 0] }

And then tried to 'import' the constant definitions:

#[link(name = "jit", kind = "static")]
extern "C" {
    pub static jit_type_int: jit_type_t;
}

I'm not sure if the import even works in the first place, because when I try to use it the compiler fails with error "move occurs because jit_type_int has type jit_type_t, which does not implement the Copy trait".
How to proceed with this?

There's nothing stopping you from adding #[derive(Clone, Copy)] on your struct.

But I doubt that is what you what you want to do, because your type is currently defined to be zero bytes large, and thus copying or moving it carries no meaningful payload.

How are you "using" the type?

If I'm looking at the right project, I see:

typedef struct _jit_type *jit_type_t;

So just define your type to contain a *mut c_void instead and #[derive(Copy, Clone, PartialEq, Eq)]. Or use bindgen to generate these bindings instead.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.