1 /// The error type returned when converting an integer to a mask fails.
2 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
3 pub struct TryFromMaskError(());
5 impl core::fmt::Display for TryFromMaskError {
6 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
7 write!(f, "mask must have all bits set or unset")
11 macro_rules! define_mask {
12 { $(#[$attr:meta])* struct $name:ident($type:ty); } => {
14 #[allow(non_camel_case_types)]
15 #[derive(Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash)]
17 pub struct $name(pub(crate) $type);
20 /// Construct a mask from the given value.
21 pub const fn new(value: bool) -> Self {
29 /// Test if the mask is set.
30 pub const fn test(&self) -> bool {
35 impl core::convert::From<bool> for $name {
36 fn from(value: bool) -> Self {
41 impl core::convert::From<$name> for bool {
42 fn from(mask: $name) -> Self {
47 impl core::convert::TryFrom<$type> for $name {
48 type Error = TryFromMaskError;
49 fn try_from(value: $type) -> Result<Self, Self::Error> {
50 if value == 0 || !value == 0 {
53 Err(TryFromMaskError(()))
58 impl core::convert::From<$name> for $type {
59 fn from(value: $name) -> Self {
64 impl core::fmt::Debug for $name {
65 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
70 impl core::fmt::Binary for $name {
71 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
72 <$type as core::fmt::Binary>::fmt(&self.0, f)
76 impl core::fmt::Octal for $name {
77 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
78 <$type as core::fmt::Octal>::fmt(&self.0, f)
82 impl core::fmt::LowerHex for $name {
83 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
84 <$type as core::fmt::LowerHex>::fmt(&self.0, f)
88 impl core::fmt::UpperHex for $name {
89 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
90 <$type as core::fmt::UpperHex>::fmt(&self.0, f)
118 struct mask128(i128);
122 /// `isize`-wide mask
123 struct masksize(isize);