Bitflags

The bitflags crate is useful for working with bitflags.

  1. use bitflags::bitflags;
  2. bitflags! {
  3.     /// Flags from the UART flag register.
  4.     #[repr(transparent)]
  5.     #[derive(Copy, Clone, Debug, Eq, PartialEq)]
  6.     struct Flags: u16 {
  7.         /// Clear to send.
  8.         const CTS = 1 << 0;
  9.         /// Data set ready.
  10.         const DSR = 1 << 1;
  11.         /// Data carrier detect.
  12.         const DCD = 1 << 2;
  13.         /// UART busy transmitting data.
  14.         const BUSY = 1 << 3;
  15.         /// Receive FIFO is empty.
  16.         const RXFE = 1 << 4;
  17.         /// Transmit FIFO is full.
  18.         const TXFF = 1 << 5;
  19.         /// Receive FIFO is full.
  20.         const RXFF = 1 << 6;
  21.         /// Transmit FIFO is empty.
  22.         const TXFE = 1 << 7;
  23.         /// Ring indicator.
  24.         const RI = 1 << 8;
  25.     }
  26. }
  • The bitflags! macro creates a newtype something like Flags(u16), along with a bunch of method implementations to get and set flags.