ビットフラグ
bitflags クレートは、ビットフラグを扱う際に便利です。
// 著作権 2023 Google LLC // SPDX-License-Identifier: Apache-2.0 use bitflags::bitflags; bitflags! { /// Flags from the UART flag register. #[repr(transparent)] #[derive(Copy, Clone, Debug, Eq, PartialEq)] struct 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; } }
bitflags!マクロは、struct Flags(u16)のような newtype を作成し、 フラグの取得と設定を行うための多数のメソッド実装も併せて生成します。