Struct test_setup::BitFlags
source · pub struct BitFlags<T, N = <T as RawBitFlags>::Numeric> { /* private fields */ }
Expand description
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 fn
s 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 only possible with
incorrect unsafe code.
Implementations§
source§impl<T> BitFlags<T>where
T: BitFlag,
impl<T> BitFlags<T>where T: BitFlag,
sourcepub fn from_bits(
bits: <T as RawBitFlags>::Numeric
) -> Result<BitFlags<T>, FromBitsError<T>>
pub fn from_bits( bits: <T as RawBitFlags>::Numeric ) -> Result<BitFlags<T>, FromBitsError<T>>
Returns a BitFlags<T>
if the raw value provided does not contain
any illegal flags.
sourcepub fn from_bits_truncate(bits: <T as RawBitFlags>::Numeric) -> BitFlags<T>
pub fn from_bits_truncate(bits: <T as RawBitFlags>::Numeric) -> BitFlags<T>
Create a BitFlags<T>
from an underlying bitwise value. If any
invalid bits are set, ignore them.
sourcepub unsafe fn from_bits_unchecked(
val: <T as RawBitFlags>::Numeric
) -> BitFlags<T>
pub unsafe fn from_bits_unchecked( val: <T as RawBitFlags>::Numeric ) -> BitFlags<T>
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
All bits set in val
must correspond to a value of the enum.
sourcepub fn from_flag(flag: T) -> BitFlags<T>
pub fn from_flag(flag: T) -> BitFlags<T>
Turn a T
into a BitFlags<T>
. Also available as flag.into()
.
sourcepub fn empty() -> BitFlags<T>
pub fn empty() -> BitFlags<T>
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);
sourcepub fn all() -> BitFlags<T>
pub fn all() -> BitFlags<T>
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);
sourcepub const EMPTY: BitFlags<T> = _
pub const EMPTY: BitFlags<T> = _
An empty BitFlags
. Equivalent to empty()
,
but works in a const context.
sourcepub const ALL: BitFlags<T> = _
pub const ALL: BitFlags<T> = _
A BitFlags
with all flags set. Equivalent to all()
,
but works in a const context.
sourcepub const CONST_TOKEN: ConstToken<T, <T as RawBitFlags>::Numeric> = _
pub const CONST_TOKEN: ConstToken<T, <T as RawBitFlags>::Numeric> = _
A ConstToken
for this type of flag.
sourcepub fn exactly_one(self) -> Option<T>
pub fn exactly_one(self) -> Option<T>
If exactly one flag is set, the flag is returned. Otherwise, returns None
.
See also Itertools::exactly_one
.
sourcepub fn bits(self) -> <T as RawBitFlags>::Numeric
pub fn bits(self) -> <T as RawBitFlags>::Numeric
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);
sourcepub fn intersects<B>(self, other: B) -> boolwhere
B: Into<BitFlags<T>>,
pub fn intersects<B>(self, other: B) -> boolwhere B: Into<BitFlags<T>>,
Returns true if at least one flag is shared.
sourcepub fn contains<B>(self, other: B) -> boolwhere
B: Into<BitFlags<T>>,
pub fn contains<B>(self, other: B) -> boolwhere B: Into<BitFlags<T>>,
Returns true if all flags are contained.
source§impl<T> BitFlags<T, u8>
impl<T> BitFlags<T, u8>
sourcepub const unsafe fn from_bits_unchecked_c(
val: u8,
const_token: ConstToken<T, u8>
) -> BitFlags<T, u8>
pub const unsafe fn from_bits_unchecked_c( val: u8, const_token: ConstToken<T, u8> ) -> BitFlags<T, u8>
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
All bits set in val
must correspond to a value of the enum.
sourcepub const fn from_bits_truncate_c(
bits: u8,
const_token: ConstToken<T, u8>
) -> BitFlags<T, u8>
pub const fn from_bits_truncate_c( bits: u8, const_token: ConstToken<T, u8> ) -> BitFlags<T, u8>
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);
sourcepub const fn union_c(self, other: BitFlags<T, u8>) -> BitFlags<T, u8>
pub const fn union_c(self, other: BitFlags<T, u8>) -> BitFlags<T, u8>
Bitwise OR — return value contains flag if either argument does.
Also available as a | b
, but operator overloads are not usable
in const fn
s at the moment.
sourcepub const fn intersection_c(self, other: BitFlags<T, u8>) -> BitFlags<T, u8>
pub const fn intersection_c(self, other: BitFlags<T, u8>) -> BitFlags<T, u8>
Bitwise AND — return value contains flag if both arguments do.
Also available as a & b
, but operator overloads are not usable
in const fn
s at the moment.
sourcepub const fn not_c(self, const_token: ConstToken<T, u8>) -> BitFlags<T, u8>
pub const fn not_c(self, const_token: ConstToken<T, u8>) -> BitFlags<T, u8>
Bitwise NOT — return value contains flag if argument doesn’t.
Also available as !a
, but operator overloads are not usable
in const fn
s 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);
Trait Implementations§
source§impl<T, B> BitAndAssign<B> for BitFlags<T>where
T: BitFlag,
B: Into<BitFlags<T>>,
impl<T, B> BitAndAssign<B> for BitFlags<T>where T: BitFlag, B: Into<BitFlags<T>>,
source§fn bitand_assign(&mut self, other: B)
fn bitand_assign(&mut self, other: B)
&=
operation. Read moresource§impl<T, B> BitOrAssign<B> for BitFlags<T>where
T: BitFlag,
B: Into<BitFlags<T>>,
impl<T, B> BitOrAssign<B> for BitFlags<T>where T: BitFlag, B: Into<BitFlags<T>>,
source§fn bitor_assign(&mut self, other: B)
fn bitor_assign(&mut self, other: B)
|=
operation. Read moresource§impl<T, B> BitXorAssign<B> for BitFlags<T>where
T: BitFlag,
B: Into<BitFlags<T>>,
impl<T, B> BitXorAssign<B> for BitFlags<T>where T: BitFlag, B: Into<BitFlags<T>>,
source§fn bitxor_assign(&mut self, other: B)
fn bitxor_assign(&mut self, other: B)
^=
operation. Read moresource§impl<T> Default for BitFlags<T>where
T: BitFlag,
impl<T> Default for BitFlags<T>where T: BitFlag,
The default value returned is one with all flags unset, i. e. empty
,
unless customized.
source§impl<'a, T> Deserialize<'a> for BitFlags<T>where
T: BitFlag,
<T as RawBitFlags>::Numeric: Deserialize<'a> + Into<u64>,
impl<'a, T> Deserialize<'a> for BitFlags<T>where T: BitFlag, <T as RawBitFlags>::Numeric: Deserialize<'a> + Into<u64>,
source§fn deserialize<D>(d: D) -> Result<BitFlags<T>, <D as Deserializer<'a>>::Error>where
D: Deserializer<'a>,
fn deserialize<D>(d: D) -> Result<BitFlags<T>, <D as Deserializer<'a>>::Error>where D: Deserializer<'a>,
source§impl<T, B> Extend<B> for BitFlags<T>where
T: BitFlag,
B: Into<BitFlags<T>>,
impl<T, B> Extend<B> for BitFlags<T>where T: BitFlag, B: Into<BitFlags<T>>,
source§fn extend<I>(&mut self, it: I)where
I: IntoIterator<Item = B>,
fn extend<I>(&mut self, it: I)where I: IntoIterator<Item = B>,
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl<T> IntoIterator for BitFlags<T>where
T: BitFlag,
impl<T> IntoIterator for BitFlags<T>where T: BitFlag,
source§impl<T, N> PartialEq for BitFlags<T, N>where
N: PartialEq,
impl<T, N> PartialEq for BitFlags<T, N>where N: PartialEq,
source§impl<T> Serialize for BitFlags<T>where
T: BitFlag,
<T as RawBitFlags>::Numeric: Serialize,
impl<T> Serialize for BitFlags<T>where T: BitFlag, <T as RawBitFlags>::Numeric: Serialize,
source§fn serialize<S>(
&self,
s: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>( &self, s: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where S: Serializer,
impl<T, N> Copy for BitFlags<T, N>where T: Copy, N: Copy,
impl<T, N> Eq for BitFlags<T, N>where T: Eq, N: Eq,
impl<T, N> StructuralEq for BitFlags<T, N>
Auto Trait Implementations§
impl<T, N> RefUnwindSafe for BitFlags<T, N>where N: RefUnwindSafe, T: RefUnwindSafe,
impl<T, N> Send for BitFlags<T, N>where N: Send, T: Send,
impl<T, N> Sync for BitFlags<T, N>where N: Sync, T: Sync,
impl<T, N> Unpin for BitFlags<T, N>where N: Unpin, T: Unpin,
impl<T, N> UnwindSafe for BitFlags<T, N>where N: UnwindSafe, T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CallHasher for Twhere
T: Hash + ?Sized,
impl<T> CallHasher for Twhere T: Hash + ?Sized,
source§impl<Choices> CoproductSubsetter<CNil, HNil> for Choices
impl<Choices> CoproductSubsetter<CNil, HNil> for Choices
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T> FmtForward for T
impl<T> FmtForward for T
source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where Self: Display,
self
to use its Display
implementation when
Debug
-formatted.source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere Self: Borrow<B>, B: 'a + ?Sized, R: 'a,
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> Rwhere Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere Self: AsMut<U>, U: 'a + ?Sized, R: 'a,
self
, then passes self.as_mut()
into the pipe
function.source§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds.