Bitflags

bitflags crate 非常适用于处理 bitflag。

  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. }
  • bitflags! 宏会创建类似于 Flags(u16) 的新类型,以及一系列用于获取和设置标记的方法实现。