rust-lang / rust-lang/rust-bindgen

feature: wrap every C function on unsafe blocks

Open
#2,774 8 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

Please let me know if a "discussion" would be a better fit...

Every C function requires to be wrapped by an unsafe block, otherwise rust complains about it because it considers C unasfe.

What I see getting generate for an arbitrary C function is:

extern "C" {
    #[doc = "C function documentation"]
    pub fn c_function_name(
        arg_1: arg_1__type,
        arg_2: arg_2__type,
    ) -> ::std::os::raw::c_int;
}

When the generator like:

    let bindings = bindgen::Builder::default()
        .header("include/aruba/dp.h")
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        .generate()
        .expect("Unable to generate bindings");

The thing with the generated rust bindings, is that one always has to wrap each binding with an unsafe block, since C is considered unsafe...

Note I also tried:

    let bindings = bindgen::Builder::default()
        .header("include/aruba/dp.h")
        .wrap_unsafe_ops(true)
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        .generate()
        .expect("Unable to generate bindings");

And that doesn't help, one still needs to wrap bindings with unsafe blocks. The doc for unsafe_ops sort of suggest it only wraps functions considered unsafe on the C context, not any C function.

Does it make sense? Should all C functions be wrapped by default with unsafe blocks, and if wanted, turn that behavior off?

Thanks !

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 bindgen::Builder configuration and compare generated output for a representative C function with and without wrap_unsafe_ops(true). Review how generated extern functions are represented and determine the intended default and opt-out behavior; done means the behavior is implemented consistently and covered by an appropriate generated-output test.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.