rust-lang / rust-lang/rust-bindgen

Bindings for headers using rust-style integer aliases are awkward

Open
#2,256 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

Input C/C++ Header
#include <stdint.h>

typedef u16 uint16_t;

extern void meow(u16 a);
Bindgen Invocation
bindgen::builder()
        .header("bindgen.h")
        .generate()
        .unwrap()
Actual Results
/* snip */

pub type u16_ = u16;

extern "C" {
    pub fn meow(a: u16_);
}
Expected Results

There doesn't appear to be an easy way for this to simply become:

extern "C" {
    pub fn meow(a: u16);
}

As renaming via ParseCallbacks doesn't appear to work.

It'd be highly unfortunate to have to hack around it like this:

// Pretend we already included the header with the type aliases
#define _LIB_TYPES_H
#include <stdint.h>

// Do the alias in the preprocessor instead
#define u16 uint16_t

// Include the actual header
#include <lib.h>

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

Reproduce the case from bindgen.h with the shown bindgen::builder invocation, using the u16 alias and meow declaration. Trace how the generated Rust alias and function signature are produced, then add coverage showing that this alias can yield the expected u16 parameter without the workaround; done means the generated binding uses the requested spelling.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp, rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.