Skip to content

Commit

Permalink
Bitfield enums use #[repr(transparent)] on Rust 1.28+
Browse files Browse the repository at this point in the history
  • Loading branch information
LegNeato committed Jan 3, 2019
1 parent 651605f commit 4eaca74
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2573,7 +2573,11 @@ impl CodeGenerator for Enum {
if variation.is_rust() {
attrs.push(attributes::repr(repr_name));
} else if variation.is_bitfield() {
attrs.push(attributes::repr("C"));
if ctx.options().rust_features.repr_transparent {
attrs.push(attributes::repr("transparent"));
} else {
attrs.push(attributes::repr("C"));
}
}

if let Some(comment) = item.comment(ctx) {
Expand Down
6 changes: 6 additions & 0 deletions src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ macro_rules! rust_target_base {
=> Stable_1_26 => 1.26;
/// Rust stable 1.27
=> Stable_1_27 => 1.27;
/// Rust stable 1.28
=> Stable_1_28 => 1.28;
/// Nightly rust
=> Nightly => nightly;
);
Expand Down Expand Up @@ -184,6 +186,10 @@ rust_feature_def!(
/// `must_use` attribute on functions ([PR](https://github.com/rust-lang/rust/pull/48925))
=> must_use_function;
}
Stable_1_28 {
/// repr(transparent) ([PR](https://github.com/rust-lang/rust/pull/51562))
=> repr_transparent;
}
Nightly {
/// `thiscall` calling convention ([Tracking issue](https://github.com/rust-lang/rust/issues/42202))
=> thiscall_abi;
Expand Down
7 changes: 6 additions & 1 deletion tests/expectations/tests/bitfield-enum-basic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* automatically generated by rust-bindgen */

#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

impl Foo {
pub const Bar: Foo = Foo(2);
Expand Down
50 changes: 50 additions & 0 deletions tests/expectations/tests/bitfield-enum-repr-c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* 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(2);
}
impl Foo {
pub const Baz: Foo = Foo(4);
}
impl Foo {
pub const Duplicated: Foo = Foo(4);
}
impl Foo {
pub const Negative: Foo = Foo(-3);
}
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 i32);
50 changes: 50 additions & 0 deletions tests/expectations/tests/bitfield-enum-repr-transparent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* 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(2);
}
impl Foo {
pub const Baz: Foo = Foo(4);
}
impl Foo {
pub const Duplicated: Foo = Foo(4);
}
impl Foo {
pub const Negative: Foo = Foo(-3);
}
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(transparent)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Foo(pub i32);
8 changes: 8 additions & 0 deletions tests/headers/bitfield-enum-repr-c.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// bindgen-flags: --bitfield-enum "Foo" --rust-target 1.27 -- -std=c++11

enum Foo {
Bar = 1 << 1,
Baz = 1 << 2,
Duplicated = 1 << 2,
Negative = -3,
};
8 changes: 8 additions & 0 deletions tests/headers/bitfield-enum-repr-transparent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// bindgen-flags: --bitfield-enum "Foo" --rust-target 1.28 -- -std=c++11

enum Foo {
Bar = 1 << 1,
Baz = 1 << 2,
Duplicated = 1 << 2,
Negative = -3,
};

0 comments on commit 4eaca74

Please sign in to comment.