Trait Flags

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

    const FLAGS: &'static [Flag<Self>];
Show 24 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 contains_unknown_bits(&self) -> bool { ... } 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 truncate(&mut self) 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 contains_unknown_bits(&self) -> bool

This method will return true if any unknown bits are 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 truncate(&mut self)
where Self: Sized,

Remove any unknown bits from the flags.

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.

Implementors§

impl Flags for WriteOptions

impl Flags for AacChannels

impl Flags for AacObjectType

impl Flags for AacSamplingFrequency

impl Flags for SbcAllocation

impl Flags for SbcBlockCount

impl Flags for SbcChannelMode

impl Flags for SbcSamplingFrequency

impl Flags for SbcSubBands

impl Flags for MapSupportedFeatures

impl Flags for SetPathFlags

impl Flags for ConfigMutability

impl Flags for SpawnOptions

impl Flags for AtRestFlags

impl Flags for DynamicFlags

impl Flags for Flags

impl Flags for KernelDriverIrqFlags

impl Flags for TopologyProcessorFlags

impl Flags for Uint16Bits

impl Flags for Uint32Bits

impl Flags for Uint64Bits

impl Flags for Uint8Bits

impl Flags for Bits

impl Flags for WriteOptions

impl Flags for TeardownReason

impl Flags for DefaultBits

impl Flags for U16Bits

impl Flags for U32Bits

impl Flags for U64Bits

impl Flags for U8Bits

impl Flags for PacketFlags

impl Flags for MajorPlayerType

impl Flags for Notifications

impl Flags for PlayerFeatureBits

impl Flags for PlayerFeatureBitsExt

impl Flags for PlayerSubType

impl Flags for MessageType

impl Flags for ConfigMutability

impl Flags for DeviceSignal

impl Flags for ConnectionType

impl Flags for RestartRematchFlags

impl Flags for FileMode

impl Flags for FlexibleFileMode

impl Flags for TypefaceRequestFlags

impl Flags for ListTypefacesFlags

impl Flags for OutputFlags

impl Flags for CommandBufferFlags

impl Flags for IcdFlags

impl Flags for ImportFlags

impl Flags for MapFlags

impl Flags for ResultFlags

impl Flags for StatusFlags

impl Flags for Flag

impl Flags for BlockIoFlag

impl Flags for ModeFlags

impl Flags for EthernetFeatures

impl Flags for RxFlags

impl Flags for SessionFlags

impl Flags for StatusFlags

impl Flags for TxFlags

impl Flags for TxReturnFlags

impl Flags for Command

impl Flags for Status

impl Flags for RamdiskFlag

impl Flags for AllocateMode

impl Flags for FileSignal

impl Flags for Flags

impl Flags for ModeType

impl Flags for NodeAttributeFlags

impl Flags for NodeAttributesQuery

impl Flags for NodeProtocolKinds

impl Flags for OpenFlags

impl Flags for Operations

impl Flags for UnlinkFlags

impl Flags for VmoFlags

impl Flags for WatchMask

impl Flags for AudioGainInfoFlags

impl Flags for AudioGainValidFlags

impl Flags for PlayerCapabilityFlags

impl Flags for NodeTypes

impl Flags for IpVersions

impl Flags for Media

impl Flags for Signals

impl Flags for ChannelRights

impl Flags for EventPairRights

impl Flags for Signals

impl Flags for SocketRights

impl Flags for CmsgRequests

impl Flags for InterfaceFlags

impl Flags for RecvMsgFlags

impl Flags for SendMsgFlags

impl Flags for ShutdownMode

impl Flags for Permissions

impl Flags for ThemeMode

impl Flags for ToggleStateFlags

impl Flags for FileFlags

impl Flags for ReadFlags

impl Flags for LockState

impl Flags for Modifiers

impl Flags for Error

impl Flags for Features

impl Flags for ResetConfigFlags

impl Flags for ContextFeatureFlags

impl Flags for InputTypes

impl Flags for NavigationPhase

impl Flags for MgmtFrameCaptureFlags

impl Flags for WlanRxInfoFlags

impl Flags for WlanRxInfoValid

impl Flags for WlanTxInfoFlags

impl Flags for WlanTxInfoValid

impl Flags for ConfigMutability

impl Flags for ConnectionType

impl Flags for AllocateMode

impl Flags for FileSignal

impl Flags for Flags

impl Flags for ModeType

impl Flags for NodeAttributeFlags

impl Flags for NodeAttributesQuery

impl Flags for NodeProtocolKinds

impl Flags for OpenFlags

impl Flags for Operations

impl Flags for UnlinkFlags

impl Flags for VmoFlags

impl Flags for WatchMask

impl Flags for BitsUint32

impl Flags for BitsUint8

impl Flags for EmptyBits

impl Flags for FidlvizBits

impl Flags for FlexibleBitsUint16

impl Flags for FlexibleBitsUint32

impl Flags for FlexibleBitsUint64

impl Flags for FlexibleBitsUint8

impl Flags for GoldenBits

impl Flags for Rights

impl Flags for StrictBitsUint16

impl Flags for StrictBitsUint32

impl Flags for StrictBitsUint64

impl Flags for StrictBitsUint8

impl Flags for FlexibleButtons

impl Flags for StrictButtons

impl Flags for AudioLocation

impl Flags for AddressFlags

impl Flags for AddressHeaderFlags

impl Flags for Inet6IfaceFlags

impl Flags for LinkFlags

impl Flags for NeighbourFlags

impl Flags for RouteFlags

impl Flags for RouteNextHopFlags

impl Flags for RuleFlags

impl Flags for TcActionMessageFlags

impl Flags for TcNatFlags

impl Flags for TcU32OptionFlags

impl Flags for TcU32SelectorFlags

impl Flags for AtFlags

impl Flags for FdFlag

impl Flags for OFlag

impl Flags for SaFlags

impl Flags for MsgFlags

impl Flags for SockFlag

impl Flags for Mode

impl Flags for SFlag

impl Flags for FsFlags

impl Flags for Permission

impl Flags for ChangedFlags

impl Flags for LinkModeConfig

impl Flags for SegmentFlags

impl Flags for PathSpace

impl Flags for ThemeMode

impl Flags for InterfaceFlags

impl Flags for DeviceState

impl Flags for HandleFlags

impl Flags for MemoryAccess

impl Flags for Usage

impl Flags for TestBitfield

impl Flags for DndAction

impl Flags for Mode

impl Flags for Capability

impl Flags for Resize

impl Flags for Transient

impl Flags for DndAction

impl Flags for Mode

impl Flags for Capability

impl Flags for Resize

impl Flags for Transient

impl Flags for ConstraintAdjustment

impl Flags for ConstraintAdjustment

impl Flags for ScaleProperty

impl Flags for ScaleProperty

impl Flags for ClockOpts

impl Flags for CpuFeatureFlags

impl Flags for DebugLogOpts

impl Flags for IobAccess

impl Flags for JobCriticalOptions

impl Flags for MemoryStallKind

impl Flags for PagerOptions

impl Flags for PortOptions

impl Flags for ProcessInfoFlags

impl Flags for ProcessOptions

impl Flags for RaiseExceptionOptions

impl Flags for ResourceFlag

impl Flags for ResourceKind

impl Flags for Rights

impl Flags for Signals

impl Flags for SocketOpts

impl Flags for SocketReadOpts

impl Flags for SocketWriteOpts

impl Flags for StreamOptions

impl Flags for StreamReadOptions

impl Flags for StreamWriteOptions

impl Flags for TransferDataOptions

impl Flags for VmarFlags

impl Flags for VmarFlagsExtended

impl Flags for VmoChildOptions

impl Flags for VmoInfoFlags

impl Flags for VmoOptions

impl Flags for WaitAsyncOpts