mozilla / mozilla/cbindgen

Support for struct-scoped type aliases

Open
#1,099 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
3k
Forks
386
Avg merge
1h 43m
Merged PRs (30d)
1

Description

Hello! I hope you are doing well.

Love cbindgen! Phenomenal work that you all have accomplished.

I'm reaching out because I'm looking for very particular code generation behavior. I wanted to know if this is something that cbindgen can already do, is planning to do, or is explicitly not going to be supported.


Suppose that I'm trying to replace C++ code class-by-class in an existing codebase. Suppose also that the class definition is very simple and looks like this:

Click to view original C++ code
class FooBar {
public:
  using my_foo_t = uint32_t;

  FooBar(my_foo_t my_foo) : m_favorite_foo(my_foo) {}

  bool is_favorite_foo(const FooBar& foo_under_test) {
    return foo_under_test == m_favorite_foo;
  }

private:
  my_foo_t m_favorite_foo;
}

To port this class, I'd write something like following Rust code:

Click to view Rust port
pub type my_foo_t = u32;

#[repr(C)]
pub struct FooBar {
  favorite_foo: my_foo_t,
}

#[unsafe(no_mangle)]
pub extern "C" fn RUST_is_favorite_foo(foo_bar: &FooBar, foo: my_foo_t) -> bool {
  foo.favorite_foo = other.foo
}

And cbindgen generates the following C++ header:

Click to view generated C++ header
using my_foo_t = uint32_t;

struct FooBar {
  my_foo_t favorite_foo;
};

// ...

However, I would like it instead to generate the following C++ header where the type alias is scoped to the struct:

Click to view the desired generated C++ header
struct FooBar {
  using my_foo_t = uint32_t;
  my_foot_t favorite_foo;
};

// ...

If this is not something that can currently be done, I can't imagine it would be that hard (famous last works) to tell cbindgen to put the using statement inside of the struct instead of in the same scope as the struct definition using some kind of annotation in the lib.rs file.

Thank you again for taking the time to read my question!

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

Start by reading lib.rs and the surrounding cbindgen configuration and annotation handling mentioned in the issue. Trace how a Rust type alias and struct are emitted into the C++ header. Done means an opt-in annotation or equivalent produces the alias inside the generated struct without changing existing default output.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.