Support passing attributes on the command line

Open
#3,343 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust
Domain
cli, tooling

Research direction

Start by reviewing bindgen's command-line options and the derived-type generation path; no repository file or test is named in the issue. Define how a flag should add zerocopy attributes to generated types, including the requested unconditional derives and on_error behavior, then add coverage showing the generated bindings compile.

Written by the indexing model from the issue text.

Description

Seems like this has been discussed in various places, but I'm not sure if there's a single issue that tracks this. Feel free to close in favor of one of those.

In https://github.com/google/zerocopy/pull/2982, we're adding a #[zerocopy(on_error = skip/fail)] attribute that would allow code to compile even if a type derives a trait which it can't implement, e.g.:

use zerocopy::*;

// Can't be `FromBytes` because `bool: !FromBytes`
#[derive(IntoBytes, FromBytes)]
#[zerocopy(on_error = skip)]
struct Foo(bool);

This is especially useful in conjunction with bindgen. Many zerocopy users add zerocopy derives to bindgen-generated types, but need to jump through significant hoops (such as the hoops that Fuchsia jumps through) in order to avoid adding #[derive(Xxx)] on a type that can't support the trait being derived.

Once this attribute is supported, they can unconditionally add all zerocopy traits and add #[zerocopy(on_error = skip)] to ensure everything compiles regardless of which traits are supported.

Currently, bindgen doesn't support a flag to automatically add attributes to derived types, so this would have to be done as a post-processing step. It'd be great if such a flag were supported. Note that this is a soft blocker for the Linux kernel to be able to use zerocopy.

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.