Skip to content

Commit 2041dc3

Browse files
Add methods to get the known/unknown bits from a flags value
1 parent 90488e5 commit 2041dc3

3 files changed

Lines changed: 17 additions & 21 deletions

File tree

src/traits.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,19 @@ pub trait Flags: Sized + 'static {
152152
Self::from_bits_retain(truncated)
153153
}
154154

155+
/// Get the known bits from a flags value.
156+
fn known_bits(&self) -> Self::Bits {
157+
self.bits() & Self::all().bits()
158+
}
159+
160+
/// Get the unknown bits from a flags value.
161+
fn unknown_bits(&self) -> Self::Bits {
162+
self.bits() & !Self::all().bits()
163+
}
164+
155165
/// This method will return `true` if any unknown bits are set.
156166
fn contains_unknown_bits(&self) -> bool {
157-
Self::all().bits() & self.bits() != self.bits()
167+
self.unknown_bits() != Self::Bits::EMPTY
158168
}
159169

160170
/// Get the underlying bits value.

tests/compile-fail/bitflags_redefined.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
error[E0428]: the name `Flags1` is defined multiple times
2-
--> tests/compile-fail/bitflags_redefined.rs:4:1
2+
--> tests/compile-fail/bitflags_redefined.rs:10:1
33
|
44
4 | / bitflags! {
55
5 | | pub struct Flags1: u32 {
66
6 | | const A = 1;
77
7 | | }
88
8 | | }
9-
| |_^ `Flags1` redefined here
9+
| |_- previous definition of the type `Flags1` here
1010
9 |
1111
10 | / bitflags! {
1212
11 | | pub struct Flags1: u32 {
1313
12 | | const A = 1;
1414
13 | | }
1515
14 | | }
16-
| |_- previous definition of the type `Flags1` here
16+
| |_^ `Flags1` redefined here
1717
|
1818
= note: `Flags1` must be defined only once in the type namespace of this module
1919
= note: this error originates in the macro `$crate::__declare_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)

tests/compile-fail/unnamed_const.stderr

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,8 @@ error: expected identifier, found reserved identifier `_`
33
|
44
7 | const A = Self::_.bits();
55
| ^ expected identifier, found reserved identifier
6-
7-
error[E0599]: no associated item named `_` found for struct `Unnamed` in the current scope
8-
--> tests/compile-fail/unnamed_const.rs:7:25
9-
|
10-
3 | / bitflags! {
11-
4 | | pub struct Unnamed: u8 {
12-
5 | | const _ = 1;
13-
6 | |
14-
7 | | const A = Self::_.bits();
15-
| | ^ associated item not found in `Unnamed`
16-
8 | | }
17-
9 | | }
18-
| |_- associated item `_` not found for this struct
19-
|
20-
help: there is an associated constant `A` with a similar name
216
|
22-
7 - const A = Self::_.bits();
23-
7 + const A = Self::A.bits();
7+
::: src/lib.rs
248
|
9+
| const $Flag:tt = $value:expr;
10+
| ----------- while parsing argument for this `expr` macro fragment

0 commit comments

Comments
 (0)