Skip to content
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

Implemented insert_if() and remove_if() #333

Closed
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,20 @@ macro_rules! __impl_internal_bitflags {
}
}

#[inline]
pub fn insert_if(&mut self, other: Self, value: bool) {
if value {
self.insert(other);
}
}

#[inline]
pub fn remove_if(&mut self, other: Self, value: bool) {
if value {
self.remove(other);
}
}

#[inline]
#[must_use]
pub const fn intersection(self, other: Self) -> Self {
Expand Down
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,24 @@ mod tests {
assert_eq!(e1, Flags::A | Flags::B);
}

#[test]
fn test_insert_if() {
let mut e1 = Flags::A;
e1.insert_if(Flags::B, true);
e1.insert_if(Flags::C, false);

assert_eq!(e1, Flags::A | Flags::B);
}

#[test]
fn test_remove_if() {
let mut e1 = Flags::A | Flags::B | Flags::C;
e1.remove_if(Flags::A, true);
e1.remove_if(Flags::B, false);

assert_eq!(e1, Flags::B | Flags::C);
}

#[test]
fn test_assignment_operators() {
let mut m1 = Flags::empty();
Expand Down
20 changes: 20 additions & 0 deletions src/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ macro_rules! __impl_public_bitflags {
self.0.set(other.0, value)
}

/// Inserts the specified flags if the passed value is true.
#[inline]
pub fn insert_if(&mut self, other: Self, value: bool) {
self.0.insert_if(other.0, value)
}

/// Removes the specified flags if the passed value is true.
#[inline]
pub fn remove_if(&mut self, other: Self, value: bool) {
self.0.remove_if(other.0, value)
}

/// Returns the intersection between the flags in `self` and
/// `other`.
///
Expand Down Expand Up @@ -439,6 +451,14 @@ macro_rules! __impl_public_bitflags {
fn set(&mut self, other: $PublicBitFlags, value: bool) {
$PublicBitFlags::set(self, other, value)
}

fn insert_if(&mut self, other: $PublicBitFlags, value: bool) {
$PublicBitFlags::insert_if(self, other, value)
}

fn remove_if(&mut self, other: $PublicBitFlags, value: bool) {
$PublicBitFlags::remove_if(self, other, value)
}
}

impl $crate::__private::ImplementedByBitFlagsMacro for $PublicBitFlags {}
Expand Down
6 changes: 6 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ pub trait BitFlags: ImplementedByBitFlagsMacro {

/// Inserts or removes the specified flags depending on the passed value.
fn set(&mut self, other: Self, value: bool);

/// Inserts the specified flags if the value is true; does nothing if the value is false.
fn insert_if(&mut self, other: Self, value: bool);

/// Removes the specified flags if the value is true; does nothing if the value is false.
fn remove_if(&mut self, other: Self, value: bool);
}

/// A marker trait that signals that an implementation of `BitFlags` came from the `bitflags!` macro.
Expand Down