Struct enumflags2::BitFlags [−][src]
Represents a set of flags of some type T.
T must have the #[bitflags] attribute applied.
A BitFlags<T> is as large as the T itself,
and stores one flag per bit.
Memory layout
BitFlags<T> is marked with the #[repr(transparent)] trait, meaning
it can be safely transmuted into the corresponding numeric type.
Usually, the same can be achieved by using BitFlags::from_bits,
BitFlags::from_bits_truncate or BitFlags::from_bits_unchecked,
but transmuting might still be useful if, for example, you’re dealing with
an entire array of BitFlags.
Transmuting from a numeric type into BitFlags may also be done, but
care must be taken to make sure that each set bit in the value corresponds
to an existing flag
(cf. from_bits_unchecked).
For example:
#[bitflags] #[repr(u8)] // <-- the repr determines the numeric type #[derive(Copy, Clone)] enum TransmuteMe { One = 1 << 0, Two = 1 << 1, } // NOTE: we use a small, self-contained function to handle the slice // conversion to make sure the lifetimes are right. fn transmute_slice<'a>(input: &'a [BitFlags<TransmuteMe>]) -> &'a [u8] { unsafe { slice::from_raw_parts(input.as_ptr() as *const u8, input.len()) } } let many_flags = &[ TransmuteMe::One.into(), TransmuteMe::One | TransmuteMe::Two, ]; let as_nums = transmute_slice(many_flags); assert_eq!(as_nums, &[0b01, 0b11]);
Implementation notes
You might expect this struct to be defined as
struct BitFlags<T: BitFlag> { value: T::Numeric }
Ideally, that would be the case. However, because const fns cannot
have trait bounds in current Rust, this would prevent us from providing
most const fn APIs. As a workaround, we define BitFlags with two
type parameters, with a default for the second one:
struct BitFlags<T, N = <T as BitFlag>::Numeric> { value: N, marker: PhantomData<T>, }
The types substituted for T and N must always match, creating a
BitFlags value where that isn’t the case is considered to be impossible
without unsafe code.
Implementations
impl<T> BitFlags<T> where
T: BitFlag, [src]
T: BitFlag,
pub fn from_bits(bits: T::Numeric) -> Result<Self, FromBitsError<T>>[src]
Returns a BitFlags<T> if the raw value provided does not contain
any illegal flags.
pub fn from_bits_truncate(bits: T::Numeric) -> Self[src]
Create a BitFlags<T> from an underlying bitwise value. If any
invalid bits are set, ignore them.
pub unsafe fn from_bits_unchecked(val: T::Numeric) -> Self[src]
Create a new BitFlags unsafely, without checking if the bits form a valid bit pattern for the type.
Consider using from_bits
or from_bits_truncate instead.
Safety
The argument must not have set bits at positions not corresponding to any flag.
pub fn from_flag(flag: T) -> Self[src]
Turn a T into a BitFlags<T>. Also available as flag.into().
pub fn empty() -> Self[src]
Create a BitFlags with no flags set (in other words, with a value of 0).
See also: BitFlag::empty, a convenience reexport;
BitFlags::EMPTY, the same functionality available
as a constant for const fn code.
#[bitflags] #[repr(u8)] #[derive(Clone, Copy, PartialEq, Eq)] enum MyFlag { One = 1 << 0, Two = 1 << 1, Three = 1 << 2, } let empty: BitFlags<MyFlag> = BitFlags::empty(); assert!(empty.is_empty()); assert_eq!(empty.contains(MyFlag::One), false); assert_eq!(empty.contains(MyFlag::Two), false); assert_eq!(empty.contains(MyFlag::Three), false);
pub fn all() -> Self[src]
Create a BitFlags with all flags set.
See also: BitFlag::all, a convenience reexport;
BitFlags::ALL, the same functionality available
as a constant for const fn code.
#[bitflags] #[repr(u8)] #[derive(Clone, Copy, PartialEq, Eq)] enum MyFlag { One = 1 << 0, Two = 1 << 1, Three = 1 << 2, } let empty: BitFlags<MyFlag> = BitFlags::all(); assert!(empty.is_all()); assert_eq!(empty.contains(MyFlag::One), true); assert_eq!(empty.contains(MyFlag::Two), true); assert_eq!(empty.contains(MyFlag::Three), true);
pub const EMPTY: Self[src]
An empty BitFlags. Equivalent to empty(),
but works in a const context.
pub const ALL: Self[src]
A BitFlags with all flags set. Equivalent to all(),
but works in a const context.
pub const CONST_TOKEN: ConstToken<T, T::Numeric>[src]
A ConstToken for this type of flag.
pub fn is_all(self) -> bool[src]
Returns true if all flags are set
pub fn is_empty(self) -> bool[src]
Returns true if no flag is set
pub fn bits(self) -> T::Numeric[src]
Returns the underlying bitwise value.
#[bitflags] #[repr(u8)] #[derive(Clone, Copy)] enum Flags { Foo = 1 << 0, Bar = 1 << 1, } let both_flags = Flags::Foo | Flags::Bar; assert_eq!(both_flags.bits(), 0b11);
pub fn intersects<B: Into<BitFlags<T>>>(self, other: B) -> bool[src]
Returns true if at least one flag is shared.
pub fn contains<B: Into<BitFlags<T>>>(self, other: B) -> bool[src]
Returns true if all flags are contained.
pub fn toggle<B: Into<BitFlags<T>>>(&mut self, other: B)[src]
Toggles the matching bits
pub fn insert<B: Into<BitFlags<T>>>(&mut self, other: B)[src]
Inserts the flags into the BitFlag
pub fn remove<B: Into<BitFlags<T>>>(&mut self, other: B)[src]
Removes the matching flags
pub fn iter(self) -> impl Iterator<Item = T>[src]
Returns an iterator that yields each set flag
impl<T> BitFlags<T, u8>[src]
pub const unsafe fn from_bits_unchecked_c(
val: u8,
const_token: ConstToken<T, u8>
) -> Self[src]
val: u8,
const_token: ConstToken<T, u8>
) -> Self
Create a new BitFlags unsafely, without checking if the bits form a valid bit pattern for the type.
Const variant of
from_bits_unchecked.
Consider using
from_bits_truncate_c instead.
Safety
The argument must not have set bits at positions not corresponding to any flag.
pub const fn from_bits_truncate_c(
bits: u8,
const_token: ConstToken<T, u8>
) -> Self[src]
bits: u8,
const_token: ConstToken<T, u8>
) -> Self
Create a BitFlags<T> from an underlying bitwise value. If any
invalid bits are set, ignore them.
#[bitflags] #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum MyFlag { One = 1 << 0, Two = 1 << 1, Three = 1 << 2, } const FLAGS: BitFlags<MyFlag> = BitFlags::<MyFlag>::from_bits_truncate_c(0b10101010, BitFlags::CONST_TOKEN); assert_eq!(FLAGS, MyFlag::Two);
#[must_use]pub const fn union_c(self, other: Self) -> Self[src]
Bitwise OR — return value contains flag if either argument does.
Also available as a | b, but operator overloads are not usable
in const fns at the moment.
#[must_use]pub const fn intersection_c(self, other: Self) -> Self[src]
Bitwise AND — return value contains flag if both arguments do.
Also available as a & b, but operator overloads are not usable
in const fns at the moment.
pub const fn not_c(self, const_token: ConstToken<T, u8>) -> Self[src]
Bitwise NOT — return value contains flag if argument doesn’t.
Also available as !a, but operator overloads are not usable
in const fns at the moment.
Moreover, due to const fn limitations, not_c needs a
ConstToken as an argument.
#[bitflags] #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum MyFlag { One = 1 << 0, Two = 1 << 1, Three = 1 << 2, } const FLAGS: BitFlags<MyFlag> = make_bitflags!(MyFlag::{One | Two}); const NEGATED: BitFlags<MyFlag> = FLAGS.not_c(BitFlags::CONST_TOKEN); assert_eq!(NEGATED, MyFlag::Three);
pub const fn bits_c(self) -> u8[src]
Returns the underlying bitwise value.
const variant of bits.
Trait Implementations
impl<T> Binary for BitFlags<T> where
T: BitFlag,
T::Numeric: Binary, [src]
T: BitFlag,
T::Numeric: Binary,
impl<T, B> BitAnd<B> for BitFlags<T> where
T: BitFlag,
B: Into<BitFlags<T>>, [src]
T: BitFlag,
B: Into<BitFlags<T>>,
type Output = BitFlags<T>
The resulting type after applying the & operator.
fn bitand(self, other: B) -> BitFlags<T>[src]
impl<T, B> BitAndAssign<B> for BitFlags<T> where
T: BitFlag,
B: Into<BitFlags<T>>, [src]
T: BitFlag,
B: Into<BitFlags<T>>,
fn bitand_assign(&mut self, other: B)[src]
impl<T, B> BitOr<B> for BitFlags<T> where
T: BitFlag,
B: Into<BitFlags<T>>, [src]
T: BitFlag,
B: Into<BitFlags<T>>,
type Output = BitFlags<T>
The resulting type after applying the | operator.
fn bitor(self, other: B) -> BitFlags<T>[src]
impl<T, B> BitOrAssign<B> for BitFlags<T> where
T: BitFlag,
B: Into<BitFlags<T>>, [src]
T: BitFlag,
B: Into<BitFlags<T>>,
fn bitor_assign(&mut self, other: B)[src]
impl<T, B> BitXor<B> for BitFlags<T> where
T: BitFlag,
B: Into<BitFlags<T>>, [src]
T: BitFlag,
B: Into<BitFlags<T>>,
type Output = BitFlags<T>
The resulting type after applying the ^ operator.
fn bitxor(self, other: B) -> BitFlags<T>[src]
impl<T, B> BitXorAssign<B> for BitFlags<T> where
T: BitFlag,
B: Into<BitFlags<T>>, [src]
T: BitFlag,
B: Into<BitFlags<T>>,
fn bitxor_assign(&mut self, other: B)[src]
impl<T: Clone, N: Clone> Clone for BitFlags<T, N>[src]
fn clone(&self) -> BitFlags<T, N>[src]
pub fn clone_from(&mut self, source: &Self)1.0.0[src]
impl<T: Copy, N: Copy> Copy for BitFlags<T, N>[src]
impl<T> Debug for BitFlags<T> where
T: BitFlag + Debug, [src]
T: BitFlag + Debug,
impl<T> Default for BitFlags<T> where
T: BitFlag, [src]
T: BitFlag,
The default value returned is one with all flags unset, i. e. empty,
unless customized.
impl<T: Eq, N: Eq> Eq for BitFlags<T, N>[src]
impl<T, B> Extend<B> for BitFlags<T> where
T: BitFlag,
B: Into<BitFlags<T>>, [src]
T: BitFlag,
B: Into<BitFlags<T>>,
fn extend<I>(&mut self, it: I) where
I: IntoIterator<Item = B>, [src]
I: IntoIterator<Item = B>,
pub fn extend_one(&mut self, item: A)[src]
pub fn extend_reserve(&mut self, additional: usize)[src]
impl<T: BitFlag> From<T> for BitFlags<T>[src]
impl<T, B> FromIterator<B> for BitFlags<T> where
T: BitFlag,
B: Into<BitFlags<T>>, [src]
T: BitFlag,
B: Into<BitFlags<T>>,
fn from_iter<I>(it: I) -> BitFlags<T> where
I: IntoIterator<Item = B>, [src]
I: IntoIterator<Item = B>,
impl<T, N: Hash> Hash for BitFlags<T, N>[src]
fn hash<H: Hasher>(&self, state: &mut H)[src]
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher, 1.3.0[src]
H: Hasher,
impl<T> LowerHex for BitFlags<T> where
T: BitFlag,
T::Numeric: LowerHex, [src]
T: BitFlag,
T::Numeric: LowerHex,
impl<T> Not for BitFlags<T> where
T: BitFlag, [src]
T: BitFlag,
type Output = BitFlags<T>
The resulting type after applying the ! operator.
fn not(self) -> BitFlags<T>[src]
impl<T> Octal for BitFlags<T> where
T: BitFlag,
T::Numeric: Octal, [src]
T: BitFlag,
T::Numeric: Octal,
impl<T, N: PartialEq> PartialEq<BitFlags<T, N>> for BitFlags<T, N>[src]
impl<T> PartialEq<T> for BitFlags<T> where
T: BitFlag, [src]
T: BitFlag,
impl<T, N> StructuralEq for BitFlags<T, N>[src]
impl<T> TryFrom<u128> for BitFlags<T> where
T: BitFlag<Numeric = u128>, [src]
T: BitFlag<Numeric = u128>,
type Error = FromBitsError<T>
The type returned in the event of a conversion error.
fn try_from(bits: T::Numeric) -> Result<Self, Self::Error>[src]
impl<T> TryFrom<u16> for BitFlags<T> where
T: BitFlag<Numeric = u16>, [src]
T: BitFlag<Numeric = u16>,
type Error = FromBitsError<T>
The type returned in the event of a conversion error.
fn try_from(bits: T::Numeric) -> Result<Self, Self::Error>[src]
impl<T> TryFrom<u32> for BitFlags<T> where
T: BitFlag<Numeric = u32>, [src]
T: BitFlag<Numeric = u32>,
type Error = FromBitsError<T>
The type returned in the event of a conversion error.
fn try_from(bits: T::Numeric) -> Result<Self, Self::Error>[src]
impl<T> TryFrom<u64> for BitFlags<T> where
T: BitFlag<Numeric = u64>, [src]
T: BitFlag<Numeric = u64>,
type Error = FromBitsError<T>
The type returned in the event of a conversion error.
fn try_from(bits: T::Numeric) -> Result<Self, Self::Error>[src]
impl<T> TryFrom<u8> for BitFlags<T> where
T: BitFlag<Numeric = u8>, [src]
T: BitFlag<Numeric = u8>,
type Error = FromBitsError<T>
The type returned in the event of a conversion error.
fn try_from(bits: T::Numeric) -> Result<Self, Self::Error>[src]
impl<T> UpperHex for BitFlags<T> where
T: BitFlag,
T::Numeric: UpperHex, [src]
T: BitFlag,
T::Numeric: UpperHex,
Auto Trait Implementations
impl<T, N> Send for BitFlags<T, N> where
N: Send,
T: Send,
N: Send,
T: Send,
impl<T, N> Sync for BitFlags<T, N> where
N: Sync,
T: Sync,
N: Sync,
T: Sync,
impl<T, N> Unpin for BitFlags<T, N> where
N: Unpin,
T: Unpin,
N: Unpin,
T: Unpin,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized, [src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized, [src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T[src]
impl<T> From<T> for T[src]
impl<T, U> Into<U> for T where
U: From<T>, [src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>, [src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>, [src]
U: TryFrom<T>,