rust-lang / rust-lang/rust-bindgen

Is there a config that not pub out the func variable in struct when using dynamic loading?

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

I'm using bindgen to generate un4seen's BASS lib binding. Here is my build.rs:

let bindings = bindgen::Builder::default()
        // The input header we would like to generate
        // bindings for.
        .header("BASS/bass.h")
        .dynamic_library_name("Bass")
        // Tell cargo to invalidate the built crate whenever any of the
        // included header files changed.
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        .allowlist_item("BASS_.*")
        // Finish the builder and generate the bindings.
        .generate()
        // Unwrap the Result and panic on failure.
        .expect("Unable to generate bindings");

    // Write the bindings to the $OUT_DIR/bindings.rs file.
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("bass.rs"))
        .expect("Couldn't write bindings!");

It will generate the bass.rs file contains:

pub struct Bass {
    __library: ::libloading::Library,
    pub BASS_Init: Result<
        unsafe extern "C" fn(
            device: ::std::os::raw::c_int,
            freq: DWORD,
            flags: DWORD,
            win: HWND,
            dsguid: *const ::std::os::raw::c_void,
        ) -> BOOL,
        ::libloading::Error,
    >,
}

impl Bass {
pub unsafe fn BASS_Init(
        &self,
        device: ::std::os::raw::c_int,
        freq: DWORD,
        flags: DWORD,
        win: HWND,
        dsguid: *const ::std::os::raw::c_void,
    ) -> BOOL {
        (self
            .BASS_Init
            .as_ref()
            .expect("Expected function, got error."))(device, freq, flags, win, dsguid)
    }
}

The variable BASS_Init and function BASS_Init has the same name, which bring some trouble in codeing. For example, when I type BASS_Ini and the vscode will advise me with both variable and func.
Image

Any helps? 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 build.rs example and the generated bass.rs output, then trace bindgen's dynamic_library_name handling for how struct fields and wrapper methods are named. Reproduce the generated BASS_Init collision and determine whether an existing configuration can suppress or rename the public field; done means dynamic-loading bindings no longer produce the unwanted completion conflict.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.