Bitflags

The bitflags crate is useful for working with bitflags.

use bitflags::bitflags;
use zerocopy::{FromBytes, IntoBytes};

/// Flags from the UART flag register.
#[repr(transparent)]
#[derive(Copy, Clone, Debug, Eq, FromBytes, IntoBytes, PartialEq)]
struct Flags(u16);

bitflags! {
    impl Flags: u16 {
        /// Clear to send.
        const CTS = 1 << 0;
        /// Data set ready.
        const DSR = 1 << 1;
        /// Data carrier detect.
        const DCD = 1 << 2;
        /// UART busy transmitting data.
        const BUSY = 1 << 3;
        /// Receive FIFO is empty.
        const RXFE = 1 << 4;
        /// Transmit FIFO is full.
        const TXFF = 1 << 5;
        /// Receive FIFO is full.
        const RXFF = 1 << 6;
        /// Transmit FIFO is empty.
        const TXFE = 1 << 7;
        /// Ring indicator.
        const RI = 1 << 8;
    }
}
  • The bitflags! macro creates a newtype something like Flags(u16), along with a bunch of method implementations to get and set flags.
  • We need to derive FromBytes and IntoBytes for use with safe-mmio, which we’ll see on the next page.