Skip to content

feature request: exporting bindings' name mappings #2657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mxyns opened this issue Oct 5, 2023 · 0 comments
Open

feature request: exporting bindings' name mappings #2657

mxyns opened this issue Oct 5, 2023 · 0 comments

Comments

@mxyns
Copy link
Contributor

mxyns commented Oct 5, 2023

(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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant