Support generating enums as structs with associated consts #1483
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implements support for issue #1421 by introducing a new macro:
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 theenum
type and its variants will be represented as constants inside of this type'simpl
block.BitfieldEnum
- Same asNewtypeEnum
with bitwise ops (BitOr, BitAnd, …).RustifiedEnum
- Generate a native Rustenum
(unsafe if invalid discriminant). Default styleRustifiedNonExhaustiveEnum
- Same asRustifiedEnum
, 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 regulargenerate!
macro turns your enum into an opaque C++ type and hides its layout: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 inautocxx_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