-
Notifications
You must be signed in to change notification settings - Fork 748
Add an option to set the default codegen style for all enums #1328
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,7 @@ use ir::context::{BindgenContext, ItemId}; | |
use ir::item::Item; | ||
use parse::{ClangItemParser, ParseError}; | ||
use regex_set::RegexSet; | ||
pub use codegen::EnumVariation; | ||
|
||
use std::borrow::Cow; | ||
use std::collections::HashMap; | ||
|
@@ -203,6 +204,16 @@ impl Builder { | |
output_vector.push("--rust-target".into()); | ||
output_vector.push(self.options.rust_target.into()); | ||
|
||
if self.options.default_enum_variant != Default::default() { | ||
output_vector.push("--default-enum-variant=".into()); | ||
output_vector.push(match self.options.default_enum_variant { | ||
codegen::EnumVariation::Rust => "rust", | ||
codegen::EnumVariation::Bitfield => "bitfield", | ||
codegen::EnumVariation::Consts => "consts", | ||
codegen::EnumVariation::ModuleConsts => "moduleconsts", | ||
}.into()) | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: No need for this semicolon. |
||
|
||
self.options | ||
.bitfield_enums | ||
.get_items() | ||
|
@@ -730,6 +741,12 @@ impl Builder { | |
} | ||
|
||
|
||
/// Set the default type of code to generate for enums | ||
pub fn default_enum_variant(mut self, arg: codegen::EnumVariation) -> Builder { | ||
self.options.default_enum_variant = arg; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Maybe call this |
||
self | ||
} | ||
|
||
/// Mark the given enum (or set of enums, if using a pattern) as being | ||
/// bitfield-like. Regular expressions are supported. | ||
/// | ||
|
@@ -1240,6 +1257,9 @@ struct BindgenOptions { | |
/// Whitelisted variables. See docs for `whitelisted_types` for more. | ||
whitelisted_vars: RegexSet, | ||
|
||
/// The default type of code to generate for enums | ||
default_enum_variant: codegen::EnumVariation, | ||
|
||
/// The enum patterns to mark an enum as bitfield. | ||
bitfield_enums: RegexSet, | ||
|
||
|
@@ -1458,6 +1478,7 @@ impl Default for BindgenOptions { | |
whitelisted_types: Default::default(), | ||
whitelisted_functions: Default::default(), | ||
whitelisted_vars: Default::default(), | ||
default_enum_variant: Default::default(), | ||
bitfield_enums: Default::default(), | ||
rustified_enums: Default::default(), | ||
constified_enum_modules: Default::default(), | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* automatically generated by rust-bindgen */ | ||
|
||
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] | ||
|
||
impl Foo { | ||
pub const Bar: Foo = Foo(0); | ||
} | ||
impl Foo { | ||
pub const Qux: Foo = Foo(1); | ||
} | ||
impl ::std::ops::BitOr<Foo> for Foo { | ||
type Output = Self; | ||
#[inline] | ||
fn bitor(self, other: Self) -> Self { | ||
Foo(self.0 | other.0) | ||
} | ||
} | ||
impl ::std::ops::BitOrAssign for Foo { | ||
#[inline] | ||
fn bitor_assign(&mut self, rhs: Foo) { | ||
self.0 |= rhs.0; | ||
} | ||
} | ||
impl ::std::ops::BitAnd<Foo> for Foo { | ||
type Output = Self; | ||
#[inline] | ||
fn bitand(self, other: Self) -> Self { | ||
Foo(self.0 & other.0) | ||
} | ||
} | ||
impl ::std::ops::BitAndAssign for Foo { | ||
#[inline] | ||
fn bitand_assign(&mut self, rhs: Foo) { | ||
self.0 &= rhs.0; | ||
} | ||
} | ||
#[repr(C)] | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | ||
pub struct Foo(pub u32); | ||
pub mod Neg { | ||
pub type Type = i32; | ||
pub const MinusOne: Type = -1; | ||
pub const One: Type = 1; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* automatically generated by rust-bindgen */ | ||
|
||
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] | ||
|
||
pub const Foo_Bar: Foo = 0; | ||
pub const Foo_Qux: Foo = 1; | ||
pub type Foo = u32; | ||
pub mod Neg { | ||
pub type Type = i32; | ||
pub const MinusOne: Type = -1; | ||
pub const One: Type = 1; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* automatically generated by rust-bindgen */ | ||
|
||
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] | ||
|
||
pub mod Foo { | ||
pub type Type = u32; | ||
pub const Bar: Type = 0; | ||
pub const Qux: Type = 1; | ||
} | ||
pub mod Neg { | ||
pub type Type = i32; | ||
pub const MinusOne: Type = -1; | ||
pub const One: Type = 1; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* automatically generated by rust-bindgen */ | ||
|
||
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] | ||
|
||
#[repr(u32)] | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | ||
pub enum Foo { | ||
Bar = 0, | ||
Qux = 1, | ||
} | ||
pub mod Neg { | ||
pub type Type = i32; | ||
pub const MinusOne: Type = -1; | ||
pub const One: Type = 1; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// bindgen-flags: --default-enum-variant=bitfield --constified-enum-module=Neg | ||
|
||
enum Foo { | ||
Bar = 0, | ||
Qux | ||
}; | ||
|
||
enum Neg { | ||
MinusOne = -1, | ||
One = 1, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// bindgen-flags: --default-enum-variant=consts --constified-enum-module=Neg | ||
|
||
enum Foo { | ||
Bar = 0, | ||
Qux | ||
}; | ||
|
||
enum Neg { | ||
MinusOne = -1, | ||
One = 1, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// bindgen-flags: --default-enum-variant=moduleconsts --constified-enum-module=Neg | ||
|
||
enum Foo { | ||
Bar = 0, | ||
Qux | ||
}; | ||
|
||
enum Neg { | ||
MinusOne = -1, | ||
One = 1, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// bindgen-flags: --default-enum-variant=rust --constified-enum-module=Neg | ||
|
||
enum Foo { | ||
Bar = 0, | ||
Qux | ||
}; | ||
|
||
enum Neg { | ||
MinusOne = -1, | ||
One = 1, | ||
}; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: no need for
as_ref
I'm pretty sure.