Skip to content

Implement assignment operators #32

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

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ documentation = "https://doc.rust-lang.org/bitflags"
description = """
A macro to generate structures which behave like bitflags.
"""

[features]
assignment_operators = []
74 changes: 70 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![cfg_attr(feature = "assignment_operators", feature(augmented_assignments, op_assign_traits))]
//! A typesafe bitmask flag generator.

/// The `bitflags!` macro generates a `struct` that holds a set of C-style
Expand All @@ -19,6 +20,7 @@
/// # Example
///
/// ```{.rust}
/// #![cfg_attr(feature = "assignment_operators", feature(augmented_assignments, op_assign_traits))]
/// #[macro_use]
/// extern crate bitflags;
///
Expand Down Expand Up @@ -47,6 +49,7 @@
/// implementations:
///
/// ```{.rust}
/// #![cfg_attr(feature = "assignment_operators", feature(augmented_assignments, op_assign_traits))]
/// #[macro_use]
/// extern crate bitflags;
///
Expand Down Expand Up @@ -104,12 +107,15 @@
///
/// The following operator traits are implemented for the generated `struct`:
///
/// - `BitOr`: union
/// - `BitAnd`: intersection
/// - `BitXor`: toggle
/// - `Sub`: set difference
/// - `BitOr` and `BitOrAssign`: union
/// - `BitAnd` and `BitAndAssign`: intersection
/// - `BitXor` and `BitXorAssign`: toggle
/// - `Sub` and `SubAssign`: set difference
/// - `Not`: set complement
///
/// As long as the assignment operators are unstable rust feature they are only
/// available with the crate feature `assignment_ops` enabled.
///
/// # Methods
///
/// The following methods are defined for the generated `struct`:
Expand Down Expand Up @@ -286,6 +292,16 @@ macro_rules! bitflags {
}
}

#[cfg(feature="assignment_operators")]
impl ::std::ops::BitOrAssign for $BitFlags {

/// Adds the set of flags.
#[inline]
fn bitor_assign(&mut self, other: $BitFlags) {
self.bits |= other.bits;
}
}

impl ::std::ops::BitXor for $BitFlags {
type Output = $BitFlags;

Expand All @@ -296,6 +312,16 @@ macro_rules! bitflags {
}
}

#[cfg(feature="assignment_operators")]
impl ::std::ops::BitXorAssign for $BitFlags {

/// Toggles the set of flags.
#[inline]
fn bitxor_assign(&mut self, other: $BitFlags) {
self.bits ^= other.bits;
}
}

impl ::std::ops::BitAnd for $BitFlags {
type Output = $BitFlags;

Expand All @@ -306,6 +332,17 @@ macro_rules! bitflags {
}
}

#[cfg(feature="assignment_operators")]
impl ::std::ops::BitAndAssign for $BitFlags {


/// Disables all flags disabled in the set.
#[inline]
fn bitand_assign(&mut self, other: $BitFlags) {
self.bits &= other.bits;
}
}

impl ::std::ops::Sub for $BitFlags {
type Output = $BitFlags;

Expand All @@ -316,6 +353,16 @@ macro_rules! bitflags {
}
}

#[cfg(feature="assignment_operators")]
impl ::std::ops::SubAssign for $BitFlags {

/// Disables all flags enabled in the set.
#[inline]
fn sub_assign(&mut self, other: $BitFlags) {
self.bits &= !other.bits;
}
}

impl ::std::ops::Not for $BitFlags {
type Output = $BitFlags;

Expand Down Expand Up @@ -523,6 +570,25 @@ mod tests {
assert!(m4 == AnotherSetOfFlags::empty());
}

#[cfg(feature="assignment_operators")]
#[test]
fn test_assignment_operators() {
let mut m1 = Flags::empty();
let e1 = FlagA | FlagC;
// union
m1 |= FlagA;
assert!(m1 == FlagA);
// intersection
m1 &= e1;
assert!(m1 == FlagA);
// set difference
m1 -= m1;
assert!(m1 == Flags::empty());
// toggle
m1 ^= e1;
assert!(m1 == e1);
}

#[test]
fn test_from_iterator() {
assert_eq!([].iter().cloned().collect::<Flags>(), Flags::empty());
Expand Down