Skip to main content

Flag

Struct Flag 

Source
pub struct Flag<T> { /* private fields */ }
Expand description

A single-bit flag within a packed integer.

Simpler than BitField for boolean flags - no overflow checking needed.

§Example

use nexus_bits::Flag;

const IS_BUY: Flag<u64> = Flag::<u64>::new(0);
const IS_IOC: Flag<u64> = Flag::<u64>::new(1);

let mut flags: u64 = 0;
flags = IS_BUY.set(flags);
flags = IS_IOC.set(flags);

assert!(IS_BUY.is_set(flags));
assert!(IS_IOC.is_set(flags));

flags = IS_IOC.clear(flags);
assert!(!IS_IOC.is_set(flags));

Implementations§

Source§

impl Flag<u8>

Source

pub const fn new(bit: u32) -> Self

Creates a flag at bit position bit.

§Panics

Panics if bit >= type’s bit width.

Source

pub const fn bit(self) -> u32

Bit position.

Source

pub const fn mask(self) -> u8

Mask with 1 at flag position.

Source

pub const fn is_set(self, val: u8) -> bool

Returns true if flag is set.

Source

pub const fn set(self, val: u8) -> u8

Set flag to 1.

Source

pub const fn clear(self, val: u8) -> u8

Set flag to 0.

Source

pub const fn toggle(self, val: u8) -> u8

Flip flag.

Source

pub const fn set_to(self, val: u8, enabled: bool) -> u8

Set flag to given boolean value.

Source§

impl Flag<u16>

Source

pub const fn new(bit: u32) -> Self

Creates a flag at bit position bit.

§Panics

Panics if bit >= type’s bit width.

Source

pub const fn bit(self) -> u32

Bit position.

Source

pub const fn mask(self) -> u16

Mask with 1 at flag position.

Source

pub const fn is_set(self, val: u16) -> bool

Returns true if flag is set.

Source

pub const fn set(self, val: u16) -> u16

Set flag to 1.

Source

pub const fn clear(self, val: u16) -> u16

Set flag to 0.

Source

pub const fn toggle(self, val: u16) -> u16

Flip flag.

Source

pub const fn set_to(self, val: u16, enabled: bool) -> u16

Set flag to given boolean value.

Source§

impl Flag<u32>

Source

pub const fn new(bit: u32) -> Self

Creates a flag at bit position bit.

§Panics

Panics if bit >= type’s bit width.

Source

pub const fn bit(self) -> u32

Bit position.

Source

pub const fn mask(self) -> u32

Mask with 1 at flag position.

Source

pub const fn is_set(self, val: u32) -> bool

Returns true if flag is set.

Source

pub const fn set(self, val: u32) -> u32

Set flag to 1.

Source

pub const fn clear(self, val: u32) -> u32

Set flag to 0.

Source

pub const fn toggle(self, val: u32) -> u32

Flip flag.

Source

pub const fn set_to(self, val: u32, enabled: bool) -> u32

Set flag to given boolean value.

Source§

impl Flag<u64>

Source

pub const fn new(bit: u32) -> Self

Creates a flag at bit position bit.

§Panics

Panics if bit >= type’s bit width.

Source

pub const fn bit(self) -> u32

Bit position.

Source

pub const fn mask(self) -> u64

Mask with 1 at flag position.

Source

pub const fn is_set(self, val: u64) -> bool

Returns true if flag is set.

Source

pub const fn set(self, val: u64) -> u64

Set flag to 1.

Source

pub const fn clear(self, val: u64) -> u64

Set flag to 0.

Source

pub const fn toggle(self, val: u64) -> u64

Flip flag.

Source

pub const fn set_to(self, val: u64, enabled: bool) -> u64

Set flag to given boolean value.

Source§

impl Flag<u128>

Source

pub const fn new(bit: u32) -> Self

Creates a flag at bit position bit.

§Panics

Panics if bit >= type’s bit width.

Source

pub const fn bit(self) -> u32

Bit position.

Source

pub const fn mask(self) -> u128

Mask with 1 at flag position.

Source

pub const fn is_set(self, val: u128) -> bool

Returns true if flag is set.

Source

pub const fn set(self, val: u128) -> u128

Set flag to 1.

Source

pub const fn clear(self, val: u128) -> u128

Set flag to 0.

Source

pub const fn toggle(self, val: u128) -> u128

Flip flag.

Source

pub const fn set_to(self, val: u128, enabled: bool) -> u128

Set flag to given boolean value.

Source§

impl Flag<i8>

Source

pub const fn new(bit: u32) -> Self

Creates a flag at bit position bit.

§Panics

Panics if bit >= type’s bit width.

Source

pub const fn bit(self) -> u32

Bit position.

Source

pub const fn mask(self) -> i8

Mask with 1 at flag position.

Source

pub const fn is_set(self, val: i8) -> bool

Returns true if flag is set.

Source

pub const fn set(self, val: i8) -> i8

Set flag to 1.

Source

pub const fn clear(self, val: i8) -> i8

Set flag to 0.

Source

pub const fn toggle(self, val: i8) -> i8

Flip flag.

Source

pub const fn set_to(self, val: i8, enabled: bool) -> i8

Set flag to given boolean value.

Source§

impl Flag<i16>

Source

pub const fn new(bit: u32) -> Self

Creates a flag at bit position bit.

§Panics

Panics if bit >= type’s bit width.

Source

pub const fn bit(self) -> u32

Bit position.

Source

pub const fn mask(self) -> i16

Mask with 1 at flag position.

Source

pub const fn is_set(self, val: i16) -> bool

Returns true if flag is set.

Source

pub const fn set(self, val: i16) -> i16

Set flag to 1.

Source

pub const fn clear(self, val: i16) -> i16

Set flag to 0.

Source

pub const fn toggle(self, val: i16) -> i16

Flip flag.

Source

pub const fn set_to(self, val: i16, enabled: bool) -> i16

Set flag to given boolean value.

Source§

impl Flag<i32>

Source

pub const fn new(bit: u32) -> Self

Creates a flag at bit position bit.

§Panics

Panics if bit >= type’s bit width.

Source

pub const fn bit(self) -> u32

Bit position.

Source

pub const fn mask(self) -> i32

Mask with 1 at flag position.

Source

pub const fn is_set(self, val: i32) -> bool

Returns true if flag is set.

Source

pub const fn set(self, val: i32) -> i32

Set flag to 1.

Source

pub const fn clear(self, val: i32) -> i32

Set flag to 0.

Source

pub const fn toggle(self, val: i32) -> i32

Flip flag.

Source

pub const fn set_to(self, val: i32, enabled: bool) -> i32

Set flag to given boolean value.

Source§

impl Flag<i64>

Source

pub const fn new(bit: u32) -> Self

Creates a flag at bit position bit.

§Panics

Panics if bit >= type’s bit width.

Source

pub const fn bit(self) -> u32

Bit position.

Source

pub const fn mask(self) -> i64

Mask with 1 at flag position.

Source

pub const fn is_set(self, val: i64) -> bool

Returns true if flag is set.

Source

pub const fn set(self, val: i64) -> i64

Set flag to 1.

Source

pub const fn clear(self, val: i64) -> i64

Set flag to 0.

Source

pub const fn toggle(self, val: i64) -> i64

Flip flag.

Source

pub const fn set_to(self, val: i64, enabled: bool) -> i64

Set flag to given boolean value.

Source§

impl Flag<i128>

Source

pub const fn new(bit: u32) -> Self

Creates a flag at bit position bit.

§Panics

Panics if bit >= type’s bit width.

Source

pub const fn bit(self) -> u32

Bit position.

Source

pub const fn mask(self) -> i128

Mask with 1 at flag position.

Source

pub const fn is_set(self, val: i128) -> bool

Returns true if flag is set.

Source

pub const fn set(self, val: i128) -> i128

Set flag to 1.

Source

pub const fn clear(self, val: i128) -> i128

Set flag to 0.

Source

pub const fn toggle(self, val: i128) -> i128

Flip flag.

Source

pub const fn set_to(self, val: i128, enabled: bool) -> i128

Set flag to given boolean value.

Trait Implementations§

Source§

impl<T: Clone> Clone for Flag<T>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Flag<T>

Source§

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

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

impl<T: PartialEq> PartialEq for Flag<T>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 Flag<T>

Source§

impl<T: Eq> Eq for Flag<T>

Source§

impl<T> StructuralPartialEq for Flag<T>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Flag<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, 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.