Skip to content

bindgen very inconsistent with generated Rust types for opaque C++ types #652

@jsgf

Description

@jsgf

Input C/C++ Header

struct simple {
 int a, b, c;
};

template<class ty>
struct templated {
 ty a, b, c;
};

typedef templated<int> int_tmpl;

Bindgen Invokation

$ bindgen input.h --no-layout-tests --opaque-type '.*' -- -x c++

Actual Results

#[repr(C)]
#[derive(Debug, Copy)]
pub struct simple {
    pub _bindgen_opaque_blob: [u32; 3usize],
}
impl Clone for simple {
    fn clone(&self) -> Self { *self }
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct templated {
}
pub type int_tmpl = u8;

Expected Results

The type for simple is fine.

The type for templated is also OK, but this would be better:

#[repr(C)]
pub struct templated<T> {
  _bindgen_opaque_blob: [size], // ideally
  _marker: PhantomData<T>,
}

The type for int_tmpl is bad - it should be one of:

  • type int_tmpl = templated<i32>
  • struct int_tmpl { _bindgen_opaque_blob: templated<i32>, }
  • or simply struct int_tmpl;

As plain u8, it's not possible to implement methods or traits on it, and it's awkward to use for any function signatures.

It will also generate type std_string = [u64; 3]; for std::string, but I haven't got a simple repro for that yet. It should generate struct std_string { _blob: [u64; 3], }.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions