rust-bindgen
rust-bindgen copied to clipboard
feature request: exporting bindings' name mappings
(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.