Skip to content

Commit 62ffd41

Browse files
author
bors-servo
authored
Auto merge of #698 - jonhoo:more-bitops, r=fitzgen
Add other bitwise ops for bitflag enums Currently, bitflag enums only implement `BitOr`. However, it is fairly common to `&` bitflags, requiring the `BitAnd` trait to be implemented as well. I also implemented `BitOrAssign` and `BitAndAssign` in the process.
2 parents d4f83a9 + dafa5a5 commit 62ffd41

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

src/codegen/mod.rs

+35
Original file line numberDiff line numberDiff line change
@@ -2009,8 +2009,43 @@ impl<'a> EnumBuilder<'a> {
20092009
}
20102010
)
20112011
.unwrap();
2012+
result.push(impl_);
2013+
2014+
let impl_ = quote_item!(ctx.ext_cx(),
2015+
impl ::$prefix::ops::BitOrAssign for $rust_ty {
2016+
#[inline]
2017+
fn bitor_assign(&mut self, rhs: $rust_ty) {
2018+
self.0 |= rhs.0;
2019+
}
2020+
}
2021+
)
2022+
.unwrap();
2023+
result.push(impl_);
2024+
2025+
let impl_ = quote_item!(ctx.ext_cx(),
2026+
impl ::$prefix::ops::BitAnd<$rust_ty> for $rust_ty {
2027+
type Output = Self;
2028+
2029+
#[inline]
2030+
fn bitand(self, other: Self) -> Self {
2031+
$rust_ty_name(self.0 & other.0)
2032+
}
2033+
}
2034+
)
2035+
.unwrap();
2036+
result.push(impl_);
20122037

2038+
let impl_ = quote_item!(ctx.ext_cx(),
2039+
impl ::$prefix::ops::BitAndAssign for $rust_ty {
2040+
#[inline]
2041+
fn bitand_assign(&mut self, rhs: $rust_ty) {
2042+
self.0 &= rhs.0;
2043+
}
2044+
}
2045+
)
2046+
.unwrap();
20132047
result.push(impl_);
2048+
20142049
aster
20152050
}
20162051
EnumBuilder::Consts { aster, .. } => aster,

tests/expectations/tests/bitfield-enum-basic.rs

+66
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ impl ::std::ops::BitOr<Foo> for Foo {
1616
#[inline]
1717
fn bitor(self, other: Self) -> Self { Foo(self.0 | other.0) }
1818
}
19+
impl ::std::ops::BitOrAssign for Foo {
20+
#[inline]
21+
fn bitor_assign(&mut self, rhs: Foo) { self.0 |= rhs.0; }
22+
}
23+
impl ::std::ops::BitAnd<Foo> for Foo {
24+
type
25+
Output
26+
=
27+
Self;
28+
#[inline]
29+
fn bitand(self, other: Self) -> Self { Foo(self.0 & other.0) }
30+
}
31+
impl ::std::ops::BitAndAssign for Foo {
32+
#[inline]
33+
fn bitand_assign(&mut self, rhs: Foo) { self.0 &= rhs.0; }
34+
}
1935
#[repr(C)]
2036
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2137
pub struct Foo(pub ::std::os::raw::c_int);
@@ -31,6 +47,22 @@ impl ::std::ops::BitOr<Buz> for Buz {
3147
#[inline]
3248
fn bitor(self, other: Self) -> Self { Buz(self.0 | other.0) }
3349
}
50+
impl ::std::ops::BitOrAssign for Buz {
51+
#[inline]
52+
fn bitor_assign(&mut self, rhs: Buz) { self.0 |= rhs.0; }
53+
}
54+
impl ::std::ops::BitAnd<Buz> for Buz {
55+
type
56+
Output
57+
=
58+
Self;
59+
#[inline]
60+
fn bitand(self, other: Self) -> Self { Buz(self.0 & other.0) }
61+
}
62+
impl ::std::ops::BitAndAssign for Buz {
63+
#[inline]
64+
fn bitand_assign(&mut self, rhs: Buz) { self.0 &= rhs.0; }
65+
}
3466
#[repr(C)]
3567
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
3668
pub struct Buz(pub ::std::os::raw::c_schar);
@@ -44,6 +76,22 @@ impl ::std::ops::BitOr<_bindgen_ty_1> for _bindgen_ty_1 {
4476
#[inline]
4577
fn bitor(self, other: Self) -> Self { _bindgen_ty_1(self.0 | other.0) }
4678
}
79+
impl ::std::ops::BitOrAssign for _bindgen_ty_1 {
80+
#[inline]
81+
fn bitor_assign(&mut self, rhs: _bindgen_ty_1) { self.0 |= rhs.0; }
82+
}
83+
impl ::std::ops::BitAnd<_bindgen_ty_1> for _bindgen_ty_1 {
84+
type
85+
Output
86+
=
87+
Self;
88+
#[inline]
89+
fn bitand(self, other: Self) -> Self { _bindgen_ty_1(self.0 & other.0) }
90+
}
91+
impl ::std::ops::BitAndAssign for _bindgen_ty_1 {
92+
#[inline]
93+
fn bitand_assign(&mut self, rhs: _bindgen_ty_1) { self.0 &= rhs.0; }
94+
}
4795
#[repr(C)]
4896
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
4997
pub struct _bindgen_ty_1(pub ::std::os::raw::c_uint);
@@ -64,6 +112,24 @@ impl ::std::ops::BitOr<Dummy__bindgen_ty_1> for Dummy__bindgen_ty_1 {
64112
Dummy__bindgen_ty_1(self.0 | other.0)
65113
}
66114
}
115+
impl ::std::ops::BitOrAssign for Dummy__bindgen_ty_1 {
116+
#[inline]
117+
fn bitor_assign(&mut self, rhs: Dummy__bindgen_ty_1) { self.0 |= rhs.0; }
118+
}
119+
impl ::std::ops::BitAnd<Dummy__bindgen_ty_1> for Dummy__bindgen_ty_1 {
120+
type
121+
Output
122+
=
123+
Self;
124+
#[inline]
125+
fn bitand(self, other: Self) -> Self {
126+
Dummy__bindgen_ty_1(self.0 & other.0)
127+
}
128+
}
129+
impl ::std::ops::BitAndAssign for Dummy__bindgen_ty_1 {
130+
#[inline]
131+
fn bitand_assign(&mut self, rhs: Dummy__bindgen_ty_1) { self.0 &= rhs.0; }
132+
}
67133
#[repr(C)]
68134
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
69135
pub struct Dummy__bindgen_ty_1(pub ::std::os::raw::c_uint);

0 commit comments

Comments
 (0)