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
Should the BitFlags
trait subtrait BitAnd
et al.?
#293
Comments
Hi @PatchMixolydic 👋 I think the only reason we don't currently use In the meantime, you can use the |
To leave a more complex example: fn contains_uncorresponding_bits<T, U>(flags: T) -> bool
where
T: BitFlags<Bits = U> + BitAnd<Output = T>,
U: Not<Output = U>,
{
!(flags & (T::from_bits_retain(!T::all().bits()))).is_empty()
}
The |
Ok, this should give you almost everything: use core::fmt::{Binary, LowerHex, Octal, UpperHex};
use core::ops::{
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
Mul, MulAssign, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
};
use bitflags::BitFlags;
pub trait BitFlagsExt {
fn complement_retain(self) -> Self;
fn has_uncorresponding_bits(self) -> bool;
}
impl<T, U> BitFlagsExt for T
where
T: BitFlags<Bits = U>
+ BitAnd<Output = T>
+ BitAndAssign
+ BitOr<Output = T>
+ BitOrAssign
+ BitXor<Output = T>
+ BitXorAssign
+ Not<Output = T>
+ Sub<Output = T>
+ SubAssign
+ Extend<T>
+ FromIterator<T>
+ IntoIterator
+ Binary
+ LowerHex
+ Octal
+ UpperHex,
U: BitAnd<Output = U>
+ BitAndAssign
+ BitOr<Output = U>
+ BitOrAssign
+ BitXor<Output = U>
+ BitXorAssign
+ Not<Output = U>
+ Shl<Output = U>
+ ShlAssign
+ Shr<Output = U>
+ ShrAssign
+ Add<Output = U>
+ AddAssign
+ Div<Output = U>
+ DivAssign
+ Mul<Output = U>
+ MulAssign
+ Rem<Output = U>
+ RemAssign
+ Sub<Output = U>
+ SubAssign,
{
fn complement_retain(self) -> Self {
Self::from_bits_retain(!self.bits())
}
fn has_uncorresponding_bits(self) -> bool {
!(self & Self::all().complement_retain()).is_empty()
}
} |
Ah, those are methods I think we should add to the |
In #348 I made the Thanks for the input everyone! |
I'm currently using
bitflags
to implement collision bitmasks in a game engine. Of course, since different games use collision for different things, I decided to try making my collider type generic over the mask type using theBitFlags
trait. However, this didn't quite work:Since the types created by
bitflags!
always implementBitAnd
(alongside several other traits), it might be convenient forBitFlags
to subtraitBitAnd
et al. Is this feasible?(I realize I could probably just use
BitAnd
as a bound on its own in this case, but I'd like to push users towards usingbitflags
to define their bitmasks.)cc #262
The text was updated successfully, but these errors were encountered: