Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #261 from KodrAus/feat/bitflags-macro
Add a hidden trait to discourage manual impls of BitFlags
- Loading branch information
Showing
11 changed files
with
243 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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,25 @@ | ||
#[macro_use] | ||
extern crate bitflags; | ||
|
||
// NOTE: Ideally this would work, but our treatment of CFGs | ||
// assumes flags may be missing but not redefined | ||
bitflags! { | ||
pub struct Flags: u32 { | ||
#[cfg(target_os = "linux")] | ||
const FOO = 1; | ||
#[cfg(not(target_os = "linux"))] | ||
const FOO = 2; | ||
} | ||
} | ||
|
||
fn main() { | ||
#[cfg(target_os = "linux")] | ||
{ | ||
assert_eq!(1, Flags::FOO.bits()); | ||
} | ||
|
||
#[cfg(not(target_os = "linux"))] | ||
{ | ||
assert_eq!(1, Flags::FOO.bits()); | ||
} | ||
} |
This file contains 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,35 @@ | ||
error[E0428]: the name `FOO` is defined multiple times | ||
--> $DIR/multi.rs:6:1 | ||
| | ||
6 | / bitflags! { | ||
7 | | pub struct Flags: u32 { | ||
8 | | #[cfg(target_os = "linux")] | ||
9 | | const FOO = 1; | ||
... | | ||
12 | | } | ||
13 | | } | ||
| | ^ | ||
| | | | ||
| |_`FOO` redefined here | ||
| previous definition of the value `FOO` here | ||
| | ||
= note: `FOO` must be defined only once in the value namespace of this trait | ||
= note: this error originates in the macro `__impl_bitflags` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error[E0428]: the name `FOO` is defined multiple times | ||
--> $DIR/multi.rs:6:1 | ||
| | ||
6 | / bitflags! { | ||
7 | | pub struct Flags: u32 { | ||
8 | | #[cfg(target_os = "linux")] | ||
9 | | const FOO = 1; | ||
... | | ||
12 | | } | ||
13 | | } | ||
| | ^ | ||
| | | | ||
| |_`FOO` redefined here | ||
| previous definition of the value `FOO` here | ||
| | ||
= note: `FOO` must be defined only once in the value namespace of this trait | ||
= note: this error originates in the macro `__impl_all_bitflags` (in Nightly builds, run with -Z macro-backtrace for more info) |
This file contains 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 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,65 @@ | ||
use bitflags::BitFlags; | ||
|
||
pub struct BootlegFlags(u32); | ||
|
||
impl BitFlags for BootlegFlags { | ||
type Bits = u32; | ||
|
||
fn empty() -> Self { | ||
unimplemented!() | ||
} | ||
|
||
fn all() -> Self { | ||
unimplemented!() | ||
} | ||
|
||
fn bits(&self) -> u32 { | ||
unimplemented!() | ||
} | ||
|
||
fn from_bits(_: u32) -> Option<BootlegFlags> { | ||
unimplemented!() | ||
} | ||
|
||
fn from_bits_truncate(_: u32) -> BootlegFlags { | ||
unimplemented!() | ||
} | ||
|
||
unsafe fn from_bits_unchecked(_: u32) -> BootlegFlags { | ||
unimplemented!() | ||
} | ||
|
||
fn is_empty(&self) -> bool { | ||
unimplemented!() | ||
} | ||
|
||
fn is_all(&self) -> bool { | ||
unimplemented!() | ||
} | ||
|
||
fn intersects(&self, _: BootlegFlags) -> bool { | ||
unimplemented!() | ||
} | ||
|
||
fn contains(&self, _: BootlegFlags) -> bool { | ||
unimplemented!() | ||
} | ||
|
||
fn insert(&mut self, _: BootlegFlags) { | ||
unimplemented!() | ||
} | ||
|
||
fn remove(&mut self, _: BootlegFlags) { | ||
unimplemented!() | ||
} | ||
|
||
fn toggle(&mut self, _: BootlegFlags) { | ||
unimplemented!() | ||
} | ||
|
||
fn set(&mut self, _: BootlegFlags, value: bool) { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
fn main() { } |
This file contains 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 @@ | ||
error[E0277]: the trait bound `BootlegFlags: ImplementedByBitFlagsMacro` is not satisfied | ||
--> $DIR/custom_impl.rs:5:6 | ||
| | ||
5 | impl BitFlags for BootlegFlags { | ||
| ^^^^^^^^ the trait `ImplementedByBitFlagsMacro` is not implemented for `BootlegFlags` | ||
| | ||
note: required by a bound in `BitFlags` | ||
--> $DIR/bitflags_trait.rs:7:21 | ||
| | ||
7 | pub trait BitFlags: ImplementedByBitFlagsMacro { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `BitFlags` |
This file contains 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 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,23 @@ | ||
#[macro_use] | ||
extern crate bitflags; | ||
|
||
bitflags! { | ||
pub struct Flags: u32 { | ||
const FOO = { | ||
#[cfg(target_os = "linux")] { 1 } | ||
#[cfg(not(target_os = "linux"))] { 2 } | ||
}; | ||
} | ||
} | ||
|
||
fn main() { | ||
#[cfg(target_os = "linux")] | ||
{ | ||
assert_eq!(1, Flags::FOO.bits()); | ||
} | ||
|
||
#[cfg(not(target_os = "linux"))] | ||
{ | ||
assert_eq!(2, Flags::FOO.bits()); | ||
} | ||
} |
This file contains 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,22 @@ | ||
use bitflags::{bitflags, BitFlags}; | ||
|
||
bitflags! { | ||
struct Flags: u32 { | ||
const A = 0b00000001; | ||
} | ||
} | ||
|
||
impl From<u32> for Flags { | ||
fn from(v: u32) -> Flags { | ||
Flags::from_bits_truncate(v) | ||
} | ||
} | ||
|
||
fn all_from_trait<F: BitFlags>() { | ||
let _ = F::all(); | ||
} | ||
|
||
fn main() { | ||
all_from_trait::<Flags>(); | ||
let _ = Flags::all(); | ||
} |
This file contains 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