rust-lang / rust-lang/rust-bindgen

feature request: exporting bindings' name mappings

Open
#2,657 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

(this is a feature request)

Hello,

I was wondering you were interested in letting bindgen::Builder provide a HashMap<String, String>, or something similar, mapping the original name of a C-type to its Rust rust-bindgen generated binding. This would give a mapping like:

// C => Rust
"type_A" => "type_A" // for a typedef type
"struct type_B" => "type_B" // for a normal struct type

I could also build this mapping myself if the bindgen::callbacks::ParseCallbacks trait had a callback method providing both the "original" name and the final "binding" name but this is currently not the case.

Use case

I am working on a library that imports some structs from a C library using rust-bindgen, defines some functions that return the imported types and are made available to C using cbindgen through the cargo-c crate.

My issue is that the types in the function signatures exported by cbindgen are not the same as the one imported (see example):
struct MySuperStruct becomes MySuperStruct in Rust and stay MySuperStruct when re-exported to C. This unnecessarily duplicates all type definitions.

This is however only partially a cbindgen issue since this happens because depending on whether the type was declared using a typedef or not. cbindgen has no way to know what the original name was.
cbindgen however provides a way to rename types by manually providing a new name for some types, which works.

My goal would be to get the mapping from rust-bindgen and generate a rename config for cbindgen

Example:

the C header

struct MySuperStruct {
   ...
}

the Rust bindings from rust-bindgen

#[repr(C, packed)]
pub struct MySuperStruct {
   ...
}

my Rust function

#[no_mangle]
pub extern "C" fn my_super_function() -> MySuperStruct {
   ...
}

the C function generated by cbindgen

// this is a header generated by cbindgen
MySuperStruct my_super_function();

Notice that the type loses the struct keyword here and thus requires an additional unwanted typedef / cast to the correct type.

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

The issue names bindgen::Builder and callbacks::ParseCallbacks as entry points; start by tracing how C names become final Rust binding names. Done means exposing an original-to-generated-name mapping that can be used to configure cbindgen renames; no test or file is named in the issue.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.