Skip to main content

Flags

Struct Flags 

Source
#[repr(transparent)]
pub struct Flags<T>(pub T);
Expand description

Flag set wrapping a primitive unsigned integer.

For named, fixed sets of flags use the bitflags crate. Flags<T> is for ad hoc flag manipulation where bit positions are data.

use bitkit::Flags;
const READ: u32 = 1 << 0;
const WRITE: u32 = 1 << 1;
let mut f = Flags::<u32>::empty();
f.enable(READ | WRITE);
assert!(f.has(READ));
assert!(f.has_all(READ | WRITE));

Tuple Fields§

§0: T

Implementations§

Source§

impl Flags<u8>

Source

pub const fn empty() -> Self

Empty set.

Source

pub const fn all() -> Self

All bits set.

Source

pub const fn from_bits(b: u8) -> Self

Wrap raw bits.

Source

pub const fn bits(self) -> u8

Underlying bits.

Source

pub const fn has(self, flags: u8) -> bool

Every bit of flags is set in self. (has(0) is true.)

Source

pub const fn has_all(self, flags: u8) -> bool

Alias for has. Reads better at call sites with multiple bits.

Source

pub const fn has_any(self, flags: u8) -> bool

Any bit of flags is set in self.

Source

pub const fn with(self, flags: u8) -> Self

Copy with flags set.

Source

pub const fn without(self, flags: u8) -> Self

Copy with flags cleared.

Source

pub const fn toggled(self, flags: u8) -> Self

Copy with flags toggled.

Source

pub fn enable(&mut self, flags: u8)

Set flags in place.

Source

pub fn disable(&mut self, flags: u8)

Clear flags in place.

Source

pub fn toggle(&mut self, flags: u8)

Toggle flags in place.

Source§

impl Flags<u16>

Source

pub const fn empty() -> Self

Empty set.

Source

pub const fn all() -> Self

All bits set.

Source

pub const fn from_bits(b: u16) -> Self

Wrap raw bits.

Source

pub const fn bits(self) -> u16

Underlying bits.

Source

pub const fn has(self, flags: u16) -> bool

Every bit of flags is set in self. (has(0) is true.)

Source

pub const fn has_all(self, flags: u16) -> bool

Alias for has. Reads better at call sites with multiple bits.

Source

pub const fn has_any(self, flags: u16) -> bool

Any bit of flags is set in self.

Source

pub const fn with(self, flags: u16) -> Self

Copy with flags set.

Source

pub const fn without(self, flags: u16) -> Self

Copy with flags cleared.

Source

pub const fn toggled(self, flags: u16) -> Self

Copy with flags toggled.

Source

pub fn enable(&mut self, flags: u16)

Set flags in place.

Source

pub fn disable(&mut self, flags: u16)

Clear flags in place.

Source

pub fn toggle(&mut self, flags: u16)

Toggle flags in place.

Source§

impl Flags<u32>

Source

pub const fn empty() -> Self

Empty set.

Source

pub const fn all() -> Self

All bits set.

Source

pub const fn from_bits(b: u32) -> Self

Wrap raw bits.

Source

pub const fn bits(self) -> u32

Underlying bits.

Source

pub const fn has(self, flags: u32) -> bool

Every bit of flags is set in self. (has(0) is true.)

Source

pub const fn has_all(self, flags: u32) -> bool

Alias for has. Reads better at call sites with multiple bits.

Source

pub const fn has_any(self, flags: u32) -> bool

Any bit of flags is set in self.

Source

pub const fn with(self, flags: u32) -> Self

Copy with flags set.

Source

pub const fn without(self, flags: u32) -> Self

Copy with flags cleared.

Source

pub const fn toggled(self, flags: u32) -> Self

Copy with flags toggled.

Source

pub fn enable(&mut self, flags: u32)

Set flags in place.

Source

pub fn disable(&mut self, flags: u32)

Clear flags in place.

Source

pub fn toggle(&mut self, flags: u32)

Toggle flags in place.

Source§

impl Flags<u64>

Source

pub const fn empty() -> Self

Empty set.

Source

pub const fn all() -> Self

All bits set.

Source

pub const fn from_bits(b: u64) -> Self

Wrap raw bits.

Source

pub const fn bits(self) -> u64

Underlying bits.

Source

pub const fn has(self, flags: u64) -> bool

Every bit of flags is set in self. (has(0) is true.)

Source

pub const fn has_all(self, flags: u64) -> bool

Alias for has. Reads better at call sites with multiple bits.

Source

pub const fn has_any(self, flags: u64) -> bool

Any bit of flags is set in self.

Source

pub const fn with(self, flags: u64) -> Self

Copy with flags set.

Source

pub const fn without(self, flags: u64) -> Self

Copy with flags cleared.

Source

pub const fn toggled(self, flags: u64) -> Self

Copy with flags toggled.

Source

pub fn enable(&mut self, flags: u64)

Set flags in place.

Source

pub fn disable(&mut self, flags: u64)

Clear flags in place.

Source

pub fn toggle(&mut self, flags: u64)

Toggle flags in place.

Source§

impl Flags<u128>

Source

pub const fn empty() -> Self

Empty set.

Source

pub const fn all() -> Self

All bits set.

Source

pub const fn from_bits(b: u128) -> Self

Wrap raw bits.

Source

pub const fn bits(self) -> u128

Underlying bits.

Source

pub const fn has(self, flags: u128) -> bool

Every bit of flags is set in self. (has(0) is true.)

Source

pub const fn has_all(self, flags: u128) -> bool

Alias for has. Reads better at call sites with multiple bits.

Source

pub const fn has_any(self, flags: u128) -> bool

Any bit of flags is set in self.

Source

pub const fn with(self, flags: u128) -> Self

Copy with flags set.

Source

pub const fn without(self, flags: u128) -> Self

Copy with flags cleared.

Source

pub const fn toggled(self, flags: u128) -> Self

Copy with flags toggled.

Source

pub fn enable(&mut self, flags: u128)

Set flags in place.

Source

pub fn disable(&mut self, flags: u128)

Clear flags in place.

Source

pub fn toggle(&mut self, flags: u128)

Toggle flags in place.

Source§

impl Flags<usize>

Source

pub const fn empty() -> Self

Empty set.

Source

pub const fn all() -> Self

All bits set.

Source

pub const fn from_bits(b: usize) -> Self

Wrap raw bits.

Source

pub const fn bits(self) -> usize

Underlying bits.

Source

pub const fn has(self, flags: usize) -> bool

Every bit of flags is set in self. (has(0) is true.)

Source

pub const fn has_all(self, flags: usize) -> bool

Alias for has. Reads better at call sites with multiple bits.

Source

pub const fn has_any(self, flags: usize) -> bool

Any bit of flags is set in self.

Source

pub const fn with(self, flags: usize) -> Self

Copy with flags set.

Source

pub const fn without(self, flags: usize) -> Self

Copy with flags cleared.

Source

pub const fn toggled(self, flags: usize) -> Self

Copy with flags toggled.

Source

pub fn enable(&mut self, flags: usize)

Set flags in place.

Source

pub fn disable(&mut self, flags: usize)

Clear flags in place.

Source

pub fn toggle(&mut self, flags: usize)

Toggle flags in place.

Trait Implementations§

Source§

impl<T: Clone> Clone for Flags<T>

Source§

fn clone(&self) -> Flags<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Flags<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for Flags<T>

Source§

fn default() -> Flags<T>

Returns the “default value” for a type. Read more
Source§

impl From<Flags<u128>> for u128

Source§

fn from(f: Flags<u128>) -> Self

Converts to this type from the input type.
Source§

impl From<Flags<u16>> for u16

Source§

fn from(f: Flags<u16>) -> Self

Converts to this type from the input type.
Source§

impl From<Flags<u32>> for u32

Source§

fn from(f: Flags<u32>) -> Self

Converts to this type from the input type.
Source§

impl From<Flags<u64>> for u64

Source§

fn from(f: Flags<u64>) -> Self

Converts to this type from the input type.
Source§

impl From<Flags<u8>> for u8

Source§

fn from(f: Flags<u8>) -> Self

Converts to this type from the input type.
Source§

impl From<Flags<usize>> for usize

Source§

fn from(f: Flags<usize>) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for Flags<u128>

Source§

fn from(v: u128) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Flags<u16>

Source§

fn from(v: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Flags<u32>

Source§

fn from(v: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Flags<u64>

Source§

fn from(v: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Flags<u8>

Source§

fn from(v: u8) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for Flags<usize>

Source§

fn from(v: usize) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash> Hash for Flags<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: PartialEq> PartialEq for Flags<T>

Source§

fn eq(&self, other: &Flags<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Copy> Copy for Flags<T>

Source§

impl<T: Eq> Eq for Flags<T>

Source§

impl<T> StructuralPartialEq for Flags<T>

Auto Trait Implementations§

§

impl<T> Freeze for Flags<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Flags<T>
where T: RefUnwindSafe,

§

impl<T> Send for Flags<T>
where T: Send,

§

impl<T> Sync for Flags<T>
where T: Sync,

§

impl<T> Unpin for Flags<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Flags<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Flags<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.