rust-lang / rust-lang/rust-bindgen

[Feature request] Provide an option to make bindgen generated default implementions inline

Open
#3,494 1 comment 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

When using the core #[derive(Default)] macro the implementation of default is always inlined:

https://doc.rust-lang.org/src/core/default.rs.html#150 :

    ($t:ty, $v:expr, $doc:tt) => {
        #[stable(feature = "rust1", since = "1.0.0")]
        #[rustc_const_unstable(feature = "const_default", issue = "143894")]
        const impl Default for $t {
            #[inline(always)]
            #[doc = $doc]
            fn default() -> $t {
                $v
            }
        }
    };
}

However bindgen must sometimes implement it's own default() implementation. Currently these are not marked #[inline(always)]. Often this isn't much of a problem, however it can become a problem when exporting functions that create these objects externally as the default implementation might be in a different object that isn't linked.

For example the Linux kernel uses bindgen to generate object files containing bindings, but doesn't always want to link these objects. In particular for loadable kernel modules - see the discussion at https://lore.kernel.org/rust-for-linux/20260909060623.888777-1-apopple@nvidia.com/ . This isn't a problem for implementations created via #[derive(Default)] as they are always inline, but bindgen generated implementations are not.

Therefore it would be nice if there was an option to have bindgen add #[inline(always)] to it's default implementations to match the behaviour of #[derive(Default)]

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 files or tests, so start by tracing bindgen's code path for generated Default implementations and its existing option plumbing. Add an option for applying #[inline(always)] to those implementations, then verify generated bindings differ only when the option is enabled.

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
Active
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.