Skip to content

Commit 53daf12

Browse files
committed
WIP: double-enum
1 parent 27c1f23 commit 53daf12

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

bindgen/codegen/mod.rs

+23-5
Original file line numberDiff line numberDiff line change
@@ -3575,7 +3575,7 @@ impl EnumBuilder {
35753575
ctx: &BindgenContext,
35763576
rust_ty: &syn::Type,
35773577
// Optionally place the enum bindings in a module
3578-
module: Option<syn::Ident>,
3578+
module: Option<Ident>,
35793579
enum_variants: &[EnumVariantInfo],
35803580
result: &mut CodegenResult<'_>,
35813581
) -> proc_macro2::TokenStream {
@@ -3813,9 +3813,27 @@ impl CodeGenerator for Enum {
38133813

38143814
let variation = self.computed_enum_variation(ctx, item);
38153815
self.codegen_variation(ctx, result, item, None, variation);
3816-
// let module = ctx.rust_ident("rustified_enum");
3817-
// Todo: add an option for additional rustified enum
3818-
// self.codegen_variation(ctx, result, item, Some(module), EnumVariation::Rust {non_exhaustive: true});
3816+
// todo: expose options for:
3817+
// - non-exhaustive
3818+
// - disambiguation: module-name / rename enum ( callback ?)
3819+
// - Variant-Casing
3820+
// perhaps we just make the additional enum a callback?
3821+
if ctx
3822+
.options()
3823+
.safe_rust_enums
3824+
.matches(item.canonical_name(ctx))
3825+
{
3826+
let module = ctx.rust_ident("rustified_enum");
3827+
self.codegen_variation(
3828+
ctx,
3829+
result,
3830+
item,
3831+
Some(module),
3832+
EnumVariation::Rust {
3833+
non_exhaustive: true,
3834+
},
3835+
);
3836+
}
38193837
}
38203838
}
38213839

@@ -3831,7 +3849,7 @@ impl Enum {
38313849
ctx: &BindgenContext,
38323850
result: &mut CodegenResult<'_>,
38333851
item: &Item,
3834-
module: Option<syn::Ident>,
3852+
module: Option<Ident>,
38353853
variation: EnumVariation,
38363854
) {
38373855
debug!("<Enum as CodeGenerator>::codegen: item = {item:?}");

bindgen/options/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,27 @@ options! {
438438
},
439439
as_args: "--newtype-enum",
440440
},
441+
/// Generate an **additional** rustified `enum`s, which can be converted from the FFI-safe
442+
/// representation.
443+
safe_rust_enums: RegexSet {
444+
methods: {
445+
regex_option! {
446+
/// Generate a rustified `enum` **in addition** to an FFI-safe representation
447+
///
448+
/// Regular rustified enums are not FFI-safe, since unknown variants are
449+
/// immediate undefined behavior. This option allows generating a secondary
450+
/// rustified enum representation, with From implemementation to conveninetly
451+
/// convert the FFI-safe representation to the rustified enum.
452+
///
453+
/// The rustified enum is placed in a module.
454+
pub fn safe_rust_enums<T: AsRef<str>>(mut self, arg: T) -> Builder {
455+
self.options.safe_rust_enums.insert(arg);
456+
self
457+
}
458+
}
459+
},
460+
as_args: "--safe-rust-enum",
461+
},
441462
/// `enum`s marked as `Result<(), ErrorEnum>`.
442463
result_error_enums: RegexSet {
443464
methods: {

0 commit comments

Comments
 (0)