]> git.lizzy.rs Git - rust.git/blob - crates/core_simd/src/masks.rs
Improve Debug implementation, add additional formatting traits
[rust.git] / crates / core_simd / src / masks.rs
1 macro_rules! define_mask {
2     { $(#[$attr:meta])* struct $name:ident($type:ty); } => {
3         $(#[$attr])*
4         #[allow(non_camel_case_types)]
5         #[derive(Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash)]
6         #[repr(transparent)]
7         pub struct $name(pub(crate) $type);
8
9         impl $name {
10             /// Construct a mask from the given value.
11             pub const fn new(value: bool) -> Self {
12                 if value {
13                     Self(!0)
14                 } else {
15                     Self(0)
16                 }
17             }
18
19             /// Test if the mask is set.
20             pub const fn test(&self) -> bool {
21                 self.0 != 0
22             }
23         }
24
25         impl core::convert::From<bool> for $name {
26             fn from(value: bool) -> Self {
27                 Self::new(value)
28             }
29         }
30
31         impl core::convert::From<$name> for bool {
32             fn from(mask: $name) -> Self {
33                 mask.test()
34             }
35         }
36
37         impl core::fmt::Debug for $name {
38             fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
39                 self.test().fmt(f)
40             }
41         }
42     }
43 }
44
45 define_mask! {
46     #[doc = "8-bit mask"]
47     struct mask8(i8);
48 }
49
50 define_mask! {
51     #[doc = "16-bit mask"]
52     struct mask16(i16);
53 }
54
55 define_mask! {
56     #[doc = "32-bit mask"]
57     struct mask32(i32);
58 }
59
60 define_mask! {
61     #[doc = "64-bit mask"]
62     struct mask64(i64);
63 }
64
65 define_mask! {
66     #[doc = "128-bit mask"]
67     struct mask128(i128);
68 }
69
70 define_mask! {
71     #[doc = "`isize`-wide mask"]
72     struct masksize(isize);
73 }