rust-lang / rust-lang/rust-bindgen

Support for Optional arguments

Open
#1,699 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-C++ enhancement help wanted
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

Input C/C++ Header

Note: In the real world, priority is an enum, but I wanted to keep this header minimal, as the outcome is the same.

extern "C" {
    void op_log(const char *message, const int priority = 1, const int line = -1, const char *function = "", const char *file = "");
}
Bindgen Invocation

This is probably irrelevant, but for completeness:

let bindings = bindgen::Builder::default()
        .clang_arg("include/rust_wrapper/wrapper.hpp")
        .clang_arg("-I./openpose/include")
        .clang_arg("-xc++")
        .clang_arg("-std=c++14")
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        .generate()
        .expect("Unable to generate bindings");
Actual Results
extern "C" {
    pub fn op_log(
        message: *const ::std::os::raw::c_char,
        priority: op_Priority,
        line: ::std::os::raw::c_int,
        function: *const ::std::os::raw::c_char,
        file: *const ::std::os::raw::c_char,
    );
}

Expected Results
extern "C" {
    pub fn op_log(
        message: *const ::std::os::raw::c_char,
        priority: ::std::Option<op_Priority>,
        line: ::std::Option<::std::os::raw::c_int>,
        function:  ::std::Option<*const ::std::os::raw::c_char>,
        file:  ::std::Option<*const ::std::os::raw::c_char>,
    );
}

Now I am uncertain whether extern "C" supports that, as it might change the ABI, but if that's the case, one could keep op_log private (maybe _op_log) and expose a small op_log wrapper, which only passes the parameters when Some().

Alternatively, one could extract the default values and pass them using said wrapper.

Maybe you have a different idea as well :)

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

Start with the minimal C++ header and the shown bindgen Builder invocation, then inspect how bindgen represents C++ default arguments in generated Rust declarations. Check whether Optional parameters are ABI-safe for extern "C" functions; done should be a settled implementation or documented decision covering the expected generated bindings and default values.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.