rust-lang / rust-lang/rust-bindgen
[Feature Request] Allow the addition of custom Rust annotations to generated function definitions
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 5.3k
- Forks
- 829
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 15
Description
We would like to wrap all our calls into the FFI boundary with a standard piece of code. In particular. we would like to use cee_scape to solve some long_jmp behaviour from the code that we are calling into. A nice way to be able to do this is to have an annotation on the generated FFI code from rust-bindgen. We can then use this proc macro to generate the wrappers that we want.
This seemed like something that other users of rust-bindgen could use, as it provides a lot of flexibility with, what I hope is, very little investment from rust-bindgen.
This issue is to see if the contributors of rust-bindgen would be interested in adding such a feature and what they would require to be added.
My view is that we allow add a setting that adds a custom annotation to the extern block, lets say extern_annotation("my_crate::my_annotation")
rust-bindgen would then generate all functions as:
[my_crate::my_annotation]
extern "C" {
pub fn my_c_function(
ctype: *const ::std::os::raw::c_char,
) -> ::std::os::raw::c_int;
}
After this point, a user of rust-bindgen can put what they want/need inside the annotation implementation. Something like:
pub fn my_annotation(_args: TokenStream, input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as ItemForeignMod);
quote! {
fn <macro code to get wrapping function name> {
#input
unsafe { my_c_function(ctype) }
}
}
.into()
}
I assume we would want to add include and exclude configuration in case there someone does not want to put the annotation on all functions. Or one could argue that they can implement exclusions in their macro, but its probably cleaner to add in rust-bindgen
If there is an existing way of doing this in rust-bindgen already that I just missed, please let me know.
I am more than happy to take a stab at producing a pull request for this, although I would appreciate some help in finding the right code.
Any thoughts/help would be appreciated.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating rust-bindgen's configuration handling and the code that emits generated extern blocks and function definitions. Clarify whether annotations apply to all functions or support include/exclude rules, then define the required behavior and tests for the proposed configuration before attempting a pull request.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100