Skip to content

Commit c2f8db1

Browse files
author
Clark Gaebel
committed
Added bitflag toggling.
1 parent 2550243 commit c2f8db1

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/libstd/bitflags.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
///
9494
/// - `BitOr`: union
9595
/// - `BitAnd`: intersection
96+
/// - `BitXor`: toggle
9697
/// - `Sub`: set difference
9798
/// - `Not`: set complement
9899
///
@@ -109,6 +110,8 @@
109110
/// - `contains`: `true` all of the flags in `other` are contained within `self`
110111
/// - `insert`: inserts the specified flags in-place
111112
/// - `remove`: removes the specified flags in-place
113+
/// - `toggle`: the specified flags will be inserted if not present, and removed
114+
/// if they are.
112115
#[macro_export]
113116
macro_rules! bitflags {
114117
($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
@@ -184,6 +187,11 @@ macro_rules! bitflags {
184187
pub fn remove(&mut self, other: $BitFlags) {
185188
self.bits &= !other.bits;
186189
}
190+
191+
/// Toggles the specified flags in-place.
192+
pub fn toggle(&mut self, other: $BitFlags) {
193+
self.bits ^= other.bits;
194+
}
187195
}
188196

189197
impl BitOr<$BitFlags, $BitFlags> for $BitFlags {
@@ -194,6 +202,14 @@ macro_rules! bitflags {
194202
}
195203
}
196204

205+
impl BitXor<$BitFlags, $BitFlags> for $BitFlags {
206+
/// Returns the left flags, but with all the right flags toggled.
207+
#[inline]
208+
fn bitxor(&self, other: &$BitFlags) -> $BitFlags {
209+
$BitFlags { bits: self.bits ^ other.bits }
210+
}
211+
}
212+
197213
impl BitAnd<$BitFlags, $BitFlags> for $BitFlags {
198214
/// Returns the intersection between the two sets of flags.
199215
#[inline]
@@ -234,7 +250,7 @@ macro_rules! bitflags {
234250
mod tests {
235251
use hash;
236252
use option::{Some, None};
237-
use ops::{BitOr, BitAnd, Sub, Not};
253+
use ops::{BitOr, BitAnd, BitXor, Sub, Not};
238254

239255
bitflags! {
240256
#[doc = "> The first principle is that you must not fool yourself — and"]
@@ -358,10 +374,14 @@ mod tests {
358374
fn test_operators() {
359375
let e1 = FlagA | FlagC;
360376
let e2 = FlagB | FlagC;
361-
assert!((e1 | e2) == FlagABC); // union
362-
assert!((e1 & e2) == FlagC); // intersection
363-
assert!((e1 - e2) == FlagA); // set difference
364-
assert!(!e2 == FlagA); // set complement
377+
assert!((e1 | e2) == FlagABC); // union
378+
assert!((e1 & e2) == FlagC); // intersection
379+
assert!((e1 - e2) == FlagA); // set difference
380+
assert!(!e2 == FlagA); // set complement
381+
assert!(e1 ^ e2 == FlagA | FlagB); // toggle
382+
let mut e3 = e1;
383+
e3.toggle(e2);
384+
assert!(e3 == FlagA | FlagB);
365385
}
366386

367387
#[test]

src/libstd/io/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ use int;
229229
use iter::Iterator;
230230
use libc;
231231
use mem::transmute;
232-
use ops::{BitOr, BitAnd, Sub, Not};
232+
use ops::{BitOr, BitXor, BitAnd, Sub, Not};
233233
use option::{Option, Some, None};
234234
use os;
235235
use boxed::Box;

0 commit comments

Comments
 (0)