Skip to content

Added bitflag toggling. #17548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/libstd/bitflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
///
/// - `BitOr`: union
/// - `BitAnd`: intersection
/// - `BitXor`: toggle
/// - `Sub`: set difference
/// - `Not`: set complement
///
Expand All @@ -109,6 +110,8 @@
/// - `contains`: `true` all of the flags in `other` are contained within `self`
/// - `insert`: inserts the specified flags in-place
/// - `remove`: removes the specified flags in-place
/// - `toggle`: the specified flags will be inserted if not present, and removed
/// if they are.
#[macro_export]
macro_rules! bitflags {
($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
Expand Down Expand Up @@ -184,6 +187,11 @@ macro_rules! bitflags {
pub fn remove(&mut self, other: $BitFlags) {
self.bits &= !other.bits;
}

/// Toggles the specified flags in-place.
pub fn toggle(&mut self, other: $BitFlags) {
self.bits ^= other.bits;
}
}

impl BitOr<$BitFlags, $BitFlags> for $BitFlags {
Expand All @@ -194,6 +202,14 @@ macro_rules! bitflags {
}
}

impl BitXor<$BitFlags, $BitFlags> for $BitFlags {
/// Returns the left flags, but with all the right flags toggled.
#[inline]
fn bitxor(&self, other: &$BitFlags) -> $BitFlags {
$BitFlags { bits: self.bits ^ other.bits }
}
}

impl BitAnd<$BitFlags, $BitFlags> for $BitFlags {
/// Returns the intersection between the two sets of flags.
#[inline]
Expand Down Expand Up @@ -234,7 +250,7 @@ macro_rules! bitflags {
mod tests {
use hash;
use option::{Some, None};
use ops::{BitOr, BitAnd, Sub, Not};
use ops::{BitOr, BitAnd, BitXor, Sub, Not};

bitflags! {
#[doc = "> The first principle is that you must not fool yourself — and"]
Expand Down Expand Up @@ -358,10 +374,14 @@ mod tests {
fn test_operators() {
let e1 = FlagA | FlagC;
let e2 = FlagB | FlagC;
assert!((e1 | e2) == FlagABC); // union
assert!((e1 & e2) == FlagC); // intersection
assert!((e1 - e2) == FlagA); // set difference
assert!(!e2 == FlagA); // set complement
assert!((e1 | e2) == FlagABC); // union
assert!((e1 & e2) == FlagC); // intersection
assert!((e1 - e2) == FlagA); // set difference
assert!(!e2 == FlagA); // set complement
assert!(e1 ^ e2 == FlagA | FlagB); // toggle
let mut e3 = e1;
e3.toggle(e2);
assert!(e3 == FlagA | FlagB);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ use int;
use iter::Iterator;
use libc;
use mem::transmute;
use ops::{BitOr, BitAnd, Sub, Not};
use ops::{BitOr, BitXor, BitAnd, Sub, Not};
use option::{Option, Some, None};
use os;
use boxed::Box;
Expand Down