rust-lang / rust-lang/rust-bindgen

Treat #define similarly to templates when used for types

Open
#1,044 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros A-templates E-hard enhancement
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the macro handling in var.rs and tracing the existing C++ template logic. Inspect tests/headers/complex.h for related multi-type coverage, then determine how macro-defined types should be represented generically. Done means bindgen handles the shown BUFFER_T pattern without C++ template workarounds and includes coverage for the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp, rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.