Skip to content

Treat #define similarly to templates when used for types #1044

Open
@RReverser

Description

@RReverser

Input C Header

#define BUFFER_T(ITEM_T) struct { ITEM_T *data; size_t capacity; }
typedef BUFFER_T(char) char_buffer_t;
typedef BUFFER_T(int) int_buffer_t;

Actual Results

/* automatically generated by rust-bindgen */

pub type wchar_t = ::std::os::raw::c_int;
pub type max_align_t = f64;
#[repr(C)]
#[derive(Debug, Copy)]
pub struct char_buffer_t {
    pub data: *mut ::std::os::raw::c_char,
    pub capacity: usize,
}
#[test]
fn bindgen_test_layout_char_buffer_t() {
    // ...
}
impl Clone for char_buffer_t {
    fn clone(&self) -> Self { *self }
}
#[repr(C)]
#[derive(Debug, Copy)]
pub struct int_buffer_t {
    pub data: *mut ::std::os::raw::c_int,
    pub capacity: usize,
}
#[test]
fn bindgen_test_layout_int_buffer_t() {
    // ...
}
impl Clone for int_buffer_t {
    fn clone(&self) -> Self { *self }
}

Expected Results

Note that this is not a bug, but more of a feature request. Is there any chance to produce generic types for this case? (and maybe also for multiple-types case like covered in tests/headers/complex.h, but that's not so important)

Looking at handling of macro in var.rs, it should be doable, but I'm not sure if it's worth introducing separate handling / recognition just for macro or is it possible to somehow share the logic between C++ templates and C macro handling? The templates logic seems to be quite complex and scattered across bindgen, so I'm not sure how that would look like, but maybe there is an easy entry point that could be reused?

Currently I can workaround this by doing #ifdef __cplusplus magic that defines a template for C++ and macro then just instantiates that template, but this hack looks quite ugly and I don't really need it outside of the bindgen.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions