Skip to content

Commit 187f7a0

Browse files
committed
Added bool and flag inversion
Added `core::ops::*` implementation generation
1 parent 30d26b1 commit 187f7a0

13 files changed

+1328
-104
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
/.idea/
2+
/.vs/
13
/target/
2-
Cargo.lock
4+
Cargo.lock

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ keywords = ["bit", "bitfield", "bitfields", "bitflag", "bitflags", "bitmask", "f
55
license = "MIT/Apache-2.0"
66
name = "bitfield"
77
readme = "README.md"
8-
version = "1.5.0"
8+
version = "1.6.0"
99

1010
[lib]
1111
proc-macro = true

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ It supports:
1313
- Compile-time overlap and boundary checking.
1414

1515
For more specific documentation look at the documentation of the macros, or at the files in
16-
`examples/*`.
16+
`examples/*`.

examples/gnu_io_channel.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,18 @@ fn main() {
101101
reserved1: 0 as _, reserved2: 0 as _,
102102

103103
flags: Flags::new()
104+
+ Flag::DoEncode // Same as: `.set_flag(Flag::DoEncode, true)`
105+
+ Flag::IsWritable // Same as: `.set_flag(Flag::IsWritable, true)`
104106
};
105107

106-
// Check and update flags.
107-
channel.flags = channel.flags
108-
.set(Flag::DoEncode, true)
109-
.set(Flag::IsWritable, true);
110-
111-
if !channel.flags.has(Flag::CloseOnUnref) {
112-
channel.flags = channel.flags.set(Flag::CloseOnUnref, true);
108+
// Check and update flag.
109+
if channel.flags.has(Flag::DoEncode) {
110+
channel.flags -= Flag::DoEncode; // Same as: `.set_flag(Flag::DoEncode, false)`
113111
}
112+
// Invert flag.
113+
channel.flags ^= Flag::CloseOnUnref; // Same as: `.invert_flag(Flag::CloseOnUnref)`
114114

115-
assert_eq!(&format!("{}", &channel.flags), "DoEncode | CloseOnUnref | IsWritable");
115+
assert_eq!(&format!("{}", &channel.flags), "CloseOnUnref | IsWritable");
116116

117117
println!("Flags: {}", &channel.flags);
118118
}

examples/vga_text_mode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn main() {
5858
.set_blink(true);
5959

6060
if !char.styles.foreground_bright() {
61-
char.styles = char.styles.set_foreground_bright(true);
61+
char.styles = char.styles.invert_foreground_bright();
6262
}
6363

6464
assert_eq!(&format!("{:#?}", &char),

examples/windows_memory_protection.rs

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,7 @@ struct Protection {
4747

4848
// Getters for specific access flags.
4949
impl Protection {
50-
#[cfg(const_trait_impl)]
51-
const fn copy_on_write(&self) -> bool {
52-
match self.access() {
53-
Ok(Access::ReadWriteCopy) |
54-
Ok(Access::ExecuteReadWriteCopy) => true,
55-
_ => false
56-
}
57-
}
58-
59-
// TODO: Remove when https://github.com/rust-lang/rfcs/pull/2632 is merged.
60-
#[cfg(not(const_trait_impl))]
50+
// TODO: Add `const` when https://github.com/rust-lang/rfcs/pull/2632 is merged.
6151
fn copy_on_write(&self) -> bool {
6252
match self.access() {
6353
Ok(Access::ReadWriteCopy) |
@@ -66,19 +56,7 @@ impl Protection {
6656
}
6757
}
6858

69-
#[cfg(const_trait_impl)]
70-
const fn execute(&self) -> bool {
71-
match self.access() {
72-
Ok(Access::Execute) |
73-
Ok(Access::ExecuteRead) |
74-
Ok(Access::ExecuteReadWrite) |
75-
Ok(Access::ExecuteReadWriteCopy) => true,
76-
_ => false
77-
}
78-
}
79-
80-
// TODO: Remove when https://github.com/rust-lang/rfcs/pull/2632 is merged.
81-
#[cfg(not(const_trait_impl))]
59+
// TODO: Add `const` when https://github.com/rust-lang/rfcs/pull/2632 is merged.
8260
fn execute(&self) -> bool {
8361
match self.access() {
8462
Ok(Access::Execute) |
@@ -89,23 +67,7 @@ impl Protection {
8967
}
9068
}
9169

92-
#[cfg(const_trait_impl)]
93-
const fn read(&self) -> bool {
94-
match self.access() {
95-
Ok(Access::Read) |
96-
Ok(Access::ReadWrite) |
97-
Ok(Access::ReadWriteCopy) |
98-
Ok(Access::ExecuteRead) |
99-
Ok(Access::ExecuteReadWrite) |
100-
Ok(Access::ExecuteReadWriteCopy) => true,
101-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
102-
Ok(Access::Execute) => true,
103-
_ => false
104-
}
105-
}
106-
107-
// TODO: Remove when https://github.com/rust-lang/rfcs/pull/2632 is merged.
108-
#[cfg(not(const_trait_impl))]
70+
// TODO: Add `const` when https://github.com/rust-lang/rfcs/pull/2632 is merged.
10971
fn read(&self) -> bool {
11072
match self.access() {
11173
Ok(Access::Read) |
@@ -120,19 +82,7 @@ impl Protection {
12082
}
12183
}
12284

123-
#[cfg(const_trait_impl)]
124-
const fn write(&self) -> bool {
125-
match self.access() {
126-
Ok(Access::ReadWrite) |
127-
Ok(Access::ReadWriteCopy) |
128-
Ok(Access::ExecuteReadWrite) |
129-
Ok(Access::ExecuteReadWriteCopy) => true,
130-
_ => false
131-
}
132-
}
133-
134-
// TODO: Remove when https://github.com/rust-lang/rfcs/pull/2632 is merged.
135-
#[cfg(not(const_trait_impl))]
85+
// TODO: Add `const` when https://github.com/rust-lang/rfcs/pull/2632 is merged.
13686
fn write(&self) -> bool {
13787
match self.access() {
13888
Ok(Access::ReadWrite) |
@@ -198,10 +148,10 @@ enum FlagUnknown {
198148

199149
fn main() {
200150
let protection = Protection::new()
201-
.set_access(Access::ExecuteReadWrite)
202-
.set_flag(Flag::Guard, true)
203-
.set_flag(Flag::NoCache, true)
204-
.set_flag_alloc(FlagAlloc::TargetsInvalid, true);
151+
+ Access::ExecuteReadWrite // Same as: `.set_access(Access::ExecuteReadWrite)`
152+
+ Flag::Guard // Same as: `.set_flag(Flag::Guard, true)`
153+
+ Flag::NoCache // Same as: `.set_flag(Flag::NoCache, true)`
154+
+ FlagAlloc::TargetsInvalid; // Same as: `.set_flag_alloc(FlagAlloc::TargetsInvalid, true)`
205155

206156
assert_eq!(
207157
protection.0,

examples/windows_message_box.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ enum Modality {
9494

9595
fn main() {
9696
let styles = Styles::new()
97-
.set_button(Button::CancelTryContinue)
98-
.set_icon(Icon::Exclamation)
99-
.set_default_button(DefaultButton::Two)
100-
.set_modality(Modality::Task)
101-
.set_style(Style::Foreground, true)
102-
.set_style(Style::TopMost, true);
97+
+ Button::CancelTryContinue // Same as: `.set_button(Button::CancelTryContinue)`
98+
+ Icon::Exclamation // Same as: `.set_icon(Icon::Exclamation)`
99+
+ DefaultButton::Two // Same as: `.set_default_button(DefaultButton::Two)`
100+
+ Modality::Task // Same as: `.set_modality(Modality::Task)`
101+
+ Style::Foreground // Same as: `.set_style(Style::Foreground, true)`
102+
+ Style::TopMost; // Same as: `.set_style(Style::TopMost, true)`
103103

104104
assert_eq!(
105105
styles.0,

examples/windows_object_access.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -376,16 +376,16 @@ enum Thread {
376376

377377
fn main() {
378378
let directory = AccessDirectory::new()
379-
.set_object(Directory::List, true)
380-
.set_object(Directory::Traverse, true)
381-
.set_standard(Standard::Delete, true)
382-
.set_standard(Standard::Synchronize, true);
379+
+ Directory::List // Same as: `.set_object(Directory::List, true)`
380+
+ Directory::Traverse // Same as: `.set_object(Directory::Traverse, true)`
381+
+ Standard::Delete // Same as: `.set_standard(Standard::Delete, true)`
382+
+ Standard::Synchronize; // Same as: `.set_standard(Standard::Synchronize, true)`
383383

384384
let file = AccessFile::new()
385-
.set_object(File::Write, true)
386-
.set_object(File::WriteAttributes, true)
387-
.set_standard(Standard::Delete, true)
388-
.set_standard(Standard::Synchronize, true);
385+
+ File::Write // Same as: `.set_object(File::Write, true)`
386+
+ File::WriteAttributes // Same as: `.set_object(File::WriteAttributes, true)`
387+
+ Standard::Delete // Same as: `.set_standard(Standard::Delete, true)`
388+
+ Standard::Synchronize; // Same as: `.set_standard(Standard::Synchronize, true)`
389389

390390
for flag in Standard::iter() {
391391
assert_eq!(

examples/x86_debug_registers.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ enum BreakPointLength {
136136
fn main() {
137137
// Set a break point:
138138
let control = DebugControl::new() // = GetDR7();
139-
.set_flag(Control::ExactInstructionLocal, true)
140-
.set_flag(Control::DebugRegister0Local, true)
141-
.set_type0(BreakPointType::Execute)
142-
.set_length0(BreakPointLength::One);
139+
.set_type0(BreakPointType::Execute) // Not possible as `+ BreakPointType::Execute`, because `BreakPointType` is used more than once.
140+
.set_length0(BreakPointLength::One) // Not possible as `+ BreakPointLength::One`, because `BreakPointLength` is used more than once.
141+
+ Control::ExactInstructionLocal // Same as: `.set_flag(Control::ExactInstructionLocal, true)`
142+
+ Control::DebugRegister0Local; // Same as: `.set_flag(Control::DebugRegister0Local, true)`
143143

144144
println!("{:#?}", &control);
145145

0 commit comments

Comments
 (0)