rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

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

Open Ferry-200 opened this issue 1 year ago • 2 comments

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!

Ferry-200 avatar Nov 02 '24 08:11 Ferry-200

There isn't but it shouldn't be to difficult to add (famous last words)

pvdrz avatar Nov 11 '24 04:11 pvdrz

I've just ran into this too. In my case I'd like to hide the generated from_library and new methods and expose my own constructor.

How about a new ParseCallbacks method that operates on these generated items in dynamic libraries, allowing one to adjust their visibility?

Molot2032 avatar Feb 09 '25 01:02 Molot2032