rust-lang / rust-lang/rust-bindgen

Generating a dummy `dynamic-loading`-like struct when not using libloading

Open
#3,259 0 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

Some crates have the ability to decide whether the shared object file will be dlopened or linked into the executable. For example, ash-rs exposes Entry::linked() and Entry::load() depending whether features linked or loaded were enabled. Achieving this with bindgen is currently possible but the exported symbols have different API which requires using conditional compilation whenever a function from the library is called, e.g.,

impl Foo {
    #[cfg(feature = "linked")]
    pub fn linked() -> Self {
        Self {}
    }
    #[cfg(feature = "loaded")]
    pub fn loaded() -> Self {
        Self {
            lib: FooLib::new("libfoo.so").unwrap(),
        }
    }

    pub fn mk_bar_wrapped(&self) {
        #[cfg(feature = "linked")]
        mk_bar();

        #[cfg(feature = "loaded")]
        self.lib.mk_bar();
    }
}

It would be if it were possible to emit a dummy FooLib struct with impl that ignores self and calls the exported function from the linked binary, something like:

impl FooLib {
    pub fn dummy() -> Self {
        Self { _unused: [] }
    }

    pub fn mk_bar(&self) {
        mk_bar()
    }
}

This way the mk_bar_wrapped implementation could be simplified to always call the mk_bar fn through the self.lib object regardless of whether libfoo is loaded or linked.

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

The issue names no source files or tests; start by locating bindgen's code for generating dynamic-loading structs and comparing the linked and loaded paths. Check how generated APIs differ between those modes. Done means linked mode exposes a dummy struct whose methods can be called through the same self.lib API as the loaded mode, with coverage for both configurations.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.