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

Open
#3,494 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

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

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.

Description

rust-for-linux

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/[email protected]/ . 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)]

Dominant language
Rust
Stars
5.3k
Forks
846
Avg merge
2d 6h
Merged PRs (30d)
11

Getting set up

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.

More from rust-lang/rust-bindgen

All issues in rust-lang/rust-bindgen

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.