rustmax::bitflags

Trait Flags

Source
pub trait Flags: Sized + 'static {
    type Bits: Bits;

    const FLAGS: &'static [Flag<Self>];
Show 22 methods // Required methods fn bits(&self) -> Self::Bits; fn from_bits_retain(bits: Self::Bits) -> Self; // Provided methods fn empty() -> Self { ... } fn all() -> Self { ... } fn from_bits(bits: Self::Bits) -> Option<Self> { ... } fn from_bits_truncate(bits: Self::Bits) -> Self { ... } fn from_name(name: &str) -> Option<Self> { ... } fn iter(&self) -> Iter<Self> { ... } fn iter_names(&self) -> IterNames<Self> { ... } fn is_empty(&self) -> bool { ... } fn is_all(&self) -> bool { ... } fn intersects(&self, other: Self) -> bool where Self: Sized { ... } fn contains(&self, other: Self) -> bool where Self: Sized { ... } fn insert(&mut self, other: Self) where Self: Sized { ... } fn remove(&mut self, other: Self) where Self: Sized { ... } fn toggle(&mut self, other: Self) where Self: Sized { ... } fn set(&mut self, other: Self, value: bool) where Self: Sized { ... } fn intersection(self, other: Self) -> Self { ... } fn union(self, other: Self) -> Self { ... } fn difference(self, other: Self) -> Self { ... } fn symmetric_difference(self, other: Self) -> Self { ... } fn complement(self) -> Self { ... }
}
Expand description

A set of defined flags using a bits type as storage.

§Implementing Flags

This trait is implemented by the bitflags macro:

use bitflags::bitflags;

bitflags! {
    struct MyFlags: u8 {
        const A = 1;
        const B = 1 << 1;
    }
}

It can also be implemented manually:

use bitflags::{Flag, Flags};

struct MyFlags(u8);

impl Flags for MyFlags {
    const FLAGS: &'static [Flag<Self>] = &[
        Flag::new("A", MyFlags(1)),
        Flag::new("B", MyFlags(1 << 1)),
    ];

    type Bits = u8;

    fn from_bits_retain(bits: Self::Bits) -> Self {
        MyFlags(bits)
    }

    fn bits(&self) -> Self::Bits {
        self.0
    }
}

§Using Flags

The Flags trait can be used generically to work with any flags types. In this example, we can count the number of defined named flags:

fn defined_flags<F: Flags>() -> usize {
    F::FLAGS.iter().filter(|f| f.is_named()).count()
}

bitflags! {
    struct MyFlags: u8 {
        const A = 1;
        const B = 1 << 1;
        const C = 1 << 2;

        const _ = !0;
    }
}

assert_eq!(3, defined_flags::<MyFlags>());

Required Associated Constants§

Source

const FLAGS: &'static [Flag<Self>]

The set of defined flags.

Required Associated Types§

Source

type Bits: Bits

The underlying bits type.

Required Methods§

Source

fn bits(&self) -> Self::Bits

Get the underlying bits value.

The returned value is exactly the bits set in this flags value.

Source

fn from_bits_retain(bits: Self::Bits) -> Self

Convert from a bits value exactly.

Provided Methods§

Source

fn empty() -> Self

Get a flags value with all bits unset.

Source

fn all() -> Self

Get a flags value with all known bits set.

Source

fn from_bits(bits: Self::Bits) -> Option<Self>

Convert from a bits value.

This method will return None if any unknown bits are set.

Source

fn from_bits_truncate(bits: Self::Bits) -> Self

Convert from a bits value, unsetting any unknown bits.

Source

fn from_name(name: &str) -> Option<Self>

Get a flags value with the bits of a flag with the given name set.

This method will return None if name is empty or doesn’t correspond to any named flag.

Source

fn iter(&self) -> Iter<Self>

Yield a set of contained flags values.

Each yielded flags value will correspond to a defined named flag. Any unknown bits will be yielded together as a final flags value.

Source

fn iter_names(&self) -> IterNames<Self>

Yield a set of contained named flags values.

This method is like Flags::iter, except only yields bits in contained named flags. Any unknown bits, or bits not corresponding to a contained flag will not be yielded.

Source

fn is_empty(&self) -> bool

Whether all bits in this flags value are unset.

Source

fn is_all(&self) -> bool

Whether all known bits in this flags value are set.

Source

fn intersects(&self, other: Self) -> bool
where Self: Sized,

Whether any set bits in a source flags value are also set in a target flags value.

Source

fn contains(&self, other: Self) -> bool
where Self: Sized,

Whether all set bits in a source flags value are also set in a target flags value.

Source

fn insert(&mut self, other: Self)
where Self: Sized,

The bitwise or (|) of the bits in two flags values.

Source

fn remove(&mut self, other: Self)
where Self: Sized,

The intersection of a source flags value with the complement of a target flags value (&!).

This method is not equivalent to self & !other when other has unknown bits set. remove won’t truncate other, but the ! operator will.

Source

fn toggle(&mut self, other: Self)
where Self: Sized,

The bitwise exclusive-or (^) of the bits in two flags values.

Source

fn set(&mut self, other: Self, value: bool)
where Self: Sized,

Call Flags::insert when value is true or Flags::remove when value is false.

Source

fn intersection(self, other: Self) -> Self

The bitwise and (&) of the bits in two flags values.

Source

fn union(self, other: Self) -> Self

The bitwise or (|) of the bits in two flags values.

Source

fn difference(self, other: Self) -> Self

The intersection of a source flags value with the complement of a target flags value (&!).

This method is not equivalent to self & !other when other has unknown bits set. difference won’t truncate other, but the ! operator will.

Source

fn symmetric_difference(self, other: Self) -> Self

The bitwise exclusive-or (^) of the bits in two flags values.

Source

fn complement(self) -> Self

The bitwise negation (!) of the bits in a flags value, truncating the result.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Flags for FileType

Source§

const FLAGS: &'static [Flag<FileType>] = _

Source§

type Bits = u32

Source§

fn bits(&self) -> u32

Source§

fn from_bits_retain(bits: u32) -> FileType

Source§

impl Flags for AtFlags

Source§

const FLAGS: &'static [Flag<AtFlags>] = _

Source§

type Bits = i32

Source§

fn bits(&self) -> i32

Source§

fn from_bits_retain(bits: i32) -> AtFlags

Source§

impl Flags for FallocateFlags

Source§

impl Flags for FdFlag

Source§

const FLAGS: &'static [Flag<FdFlag>] = _

Source§

type Bits = i32

Source§

fn bits(&self) -> i32

Source§

fn from_bits_retain(bits: i32) -> FdFlag

Source§

impl Flags for OFlag

Source§

const FLAGS: &'static [Flag<OFlag>] = _

Source§

type Bits = i32

Source§

fn bits(&self) -> i32

Source§

fn from_bits_retain(bits: i32) -> OFlag

Source§

impl Flags for RenameFlags

Source§

impl Flags for ResolveFlag

Source§

impl Flags for SealFlag

Source§

const FLAGS: &'static [Flag<SealFlag>] = _

Source§

type Bits = i32

Source§

fn bits(&self) -> i32

Source§

fn from_bits_retain(bits: i32) -> SealFlag

Source§

impl Flags for PollFlags

Source§

const FLAGS: &'static [Flag<PollFlags>] = _

Source§

type Bits = i16

Source§

fn bits(&self) -> i16

Source§

fn from_bits_retain(bits: i16) -> PollFlags

Source§

impl Flags for MemFdCreateFlag

Source§

impl Flags for SaFlags

Source§

const FLAGS: &'static [Flag<SaFlags>] = _

Source§

type Bits = i32

Source§

fn bits(&self) -> i32

Source§

fn from_bits_retain(bits: i32) -> SaFlags

Source§

impl Flags for SfdFlags

Source§

const FLAGS: &'static [Flag<SfdFlags>] = _

Source§

type Bits = i32

Source§

fn bits(&self) -> i32

Source§

fn from_bits_retain(bits: i32) -> SfdFlags

Source§

impl Flags for Mode

Source§

const FLAGS: &'static [Flag<Mode>] = _

Source§

type Bits = u32

Source§

fn bits(&self) -> u32

Source§

fn from_bits_retain(bits: u32) -> Mode

Source§

impl Flags for SFlag

Source§

const FLAGS: &'static [Flag<SFlag>] = _

Source§

type Bits = u32

Source§

fn bits(&self) -> u32

Source§

fn from_bits_retain(bits: u32) -> SFlag

Source§

impl Flags for FsFlags

Source§

const FLAGS: &'static [Flag<FsFlags>] = _

Source§

type Bits = u64

Source§

fn bits(&self) -> u64

Source§

fn from_bits_retain(bits: u64) -> FsFlags

Source§

impl Flags for ControlFlags

Source§

impl Flags for InputFlags

Source§

impl Flags for LocalFlags

Source§

impl Flags for OutputFlags

Source§

impl Flags for WaitPidFlag

Source§

impl Flags for AccessFlags

Source§

impl Flags for CipherCtxFlags

Source§

impl Flags for CMSOptions

Source§

impl Flags for OcspFlag

Source§

const FLAGS: &'static [Flag<OcspFlag>] = _

Source§

type Bits = u64

Source§

fn bits(&self) -> u64

Source§

fn from_bits_retain(bits: u64) -> OcspFlag

Source§

impl Flags for Pkcs7Flags

Source§

impl Flags for ExtensionContext

Source§

impl Flags for ShutdownState

Source§

impl Flags for SslMode

Source§

const FLAGS: &'static [Flag<SslMode>] = _

Source§

type Bits = i64

Source§

fn bits(&self) -> i64

Source§

fn from_bits_retain(bits: i64) -> SslMode

Source§

impl Flags for SslOptions

Source§

impl Flags for SslSessionCacheMode

Source§

impl Flags for SslVerifyMode

Source§

impl Flags for X509CheckFlags

Source§

impl Flags for X509VerifyFlags

Source§

impl Flags for CreateFlags

Source§

impl Flags for ReadFlags

Source§

const FLAGS: &'static [Flag<ReadFlags>] = _

Source§

type Bits = u32

Source§

fn bits(&self) -> u32

Source§

fn from_bits_retain(bits: u32) -> ReadFlags

Source§

impl Flags for WatchFlags

Source§

impl Flags for Access

Source§

const FLAGS: &'static [Flag<Access>] = _

Source§

type Bits = u32

Source§

fn bits(&self) -> u32

Source§

fn from_bits_retain(bits: u32) -> Access

Source§

impl Flags for AtFlags

Source§

const FLAGS: &'static [Flag<AtFlags>] = _

Source§

type Bits = u32

Source§

fn bits(&self) -> u32

Source§

fn from_bits_retain(bits: u32) -> AtFlags

Source§

impl Flags for FallocateFlags

Source§

impl Flags for MemfdFlags

Source§

impl Flags for Mode

Source§

const FLAGS: &'static [Flag<Mode>] = _

Source§

type Bits = u32

Source§

fn bits(&self) -> u32

Source§

fn from_bits_retain(bits: u32) -> Mode

Source§

impl Flags for OFlags

Source§

const FLAGS: &'static [Flag<OFlags>] = _

Source§

type Bits = u32

Source§

fn bits(&self) -> u32

Source§

fn from_bits_retain(bits: u32) -> OFlags

Source§

impl Flags for RenameFlags

Source§

impl Flags for ResolveFlags

Source§

impl Flags for SealFlags

Source§

const FLAGS: &'static [Flag<SealFlags>] = _

Source§

type Bits = u32

Source§

fn bits(&self) -> u32

Source§

fn from_bits_retain(bits: u32) -> SealFlags

Source§

impl Flags for StatVfsMountFlags

Source§

impl Flags for StatxFlags

Source§

impl Flags for DupFlags

Source§

const FLAGS: &'static [Flag<DupFlags>] = _

Source§

type Bits = u32

Source§

fn bits(&self) -> u32

Source§

fn from_bits_retain(bits: u32) -> DupFlags

Source§

impl Flags for FdFlags

Source§

const FLAGS: &'static [Flag<FdFlags>] = _

Source§

type Bits = u32

Source§

fn bits(&self) -> u32

Source§

fn from_bits_retain(bits: u32) -> FdFlags

Source§

impl Flags for ReadWriteFlags

Source§

impl Flags for MountFlags

Source§

impl Flags for MountPropagationFlags

Source§

impl Flags for UnmountFlags

Source§

impl Flags for IFlags

Source§

const FLAGS: &'static [Flag<IFlags>] = _

Source§

type Bits = u32

Source§

fn bits(&self) -> u32

Source§

fn from_bits_retain(bits: u32) -> IFlags

Source§

impl Flags for XattrFlags

Implementors§