Skip to content

Support generating enums as structs with associated consts #1483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

XCemaXX
Copy link

@XCemaXX XCemaXX commented May 1, 2025

Implements support for issue #1421 by introducing a new macro:

enum_style!(ENUM_TYPE, "Enum1", "Enum2", …)

This overrides the default enum‐generation style (Rust enum, newtype, bitflags, etc.) on a per-enum basis.

Supported styles

Mapped to the enum modes in bindgen.

  • NewtypeEnum - Generate integer newtype representing the enum type and its variants will be represented as constants inside of this type's impl block.
  • BitfieldEnum - Same as NewtypeEnum with bitwise ops (BitOr, BitAnd, …).
  • RustifiedEnum - Generate a native Rust enum (unsafe if invalid discriminant). Default style
  • RustifiedNonExhaustiveEnum - Same as RustifiedEnum, but annotated #[non_exhaustive]
    Not yet supported: NewtypeGlobalEnum, ConstifiedEnum, ConstifiedEnumModule

Current limitation

To ensure that C++ enums remain “plain old data” and are not wrapped in opaque structs, you must emit them with the generate_pod! macro. Otherwise, using the regular generate! macro turns your enum into an opaque C++ type and hides its layout:

// generated with "generate_pod" macro
#[allow(unused_imports)]
    pub use bindgen::root::SomeEnum;
    unsafe impl cxx::ExternType for SomeEnum{
        type Id = cxx::type_id!("SomeEnum");
        type Kind = cxx::kind::Trivial;
    }	
// generated with "generate" macro
  #[repr(transparent)]
  pub struct SomeEnum{
      _hidden_contents:
          ::core::cell::UnsafeCell<::core::mem::MaybeUninit<bindgen::root::SomeEnum>>,
  }
  unsafe impl cxx::ExternType for SomeEnum{
      type Id = cxx::type_id!("SomeEnum");
      type Kind = cxx::kind::Opaque;
  }

Currently, all C++ enums that I've turned into "BitfieldEnum" style get treated as structs by the engine’s conversion pass.
To avoid generating constructors and destructors in the FFI header autocxxgen_ffi.h enums are filtered in autocxx_engine::conversion::analysis::fun::FnAnalyzer::add_constructors_present.

Questions for reviewers

Is there a better place to hook into the conversion pipeline so that newtype enums remain classified as “enums” rather than “structs” downstream?
Any suggestions for additional tests or documentation examples?

Generated Rust code for BitfieldEnum style

#[repr(transparent)]
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct SomeFlags(pub ::std::os::raw::c_int);

impl SomeFlags{
	pub const FLAG_A: root::SomeFlags= root::SomeFlags(1);
}
impl SomeFlags{
	pub const FLAG_B: root::SomeFlags= root::SomeFlags(4);
}
impl ::std::ops::BitOr for SomeFlags { /* … */ } // and others

Copy link

google-cla bot commented May 1, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@XCemaXX XCemaXX force-pushed the 1421_enums_as_structs branch from 7467f6c to 3675788 Compare May 2, 2025 14:27
@XCemaXX XCemaXX marked this pull request as ready for review May 2, 2025 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant