ash/vk/
macros.rs

1#[macro_export]
2macro_rules! vk_bitflags_wrapped {
3    ($ name : ident , $ flag_type : ty) => {
4        impl Default for $name {
5            fn default() -> Self {
6                Self(0)
7            }
8        }
9        impl $name {
10            #[inline]
11            pub const fn empty() -> Self {
12                Self(0)
13            }
14            #[inline]
15            pub const fn from_raw(x: $flag_type) -> Self {
16                Self(x)
17            }
18            #[inline]
19            pub const fn as_raw(self) -> $flag_type {
20                self.0
21            }
22            #[inline]
23            pub const fn is_empty(self) -> bool {
24                self.0 == Self::empty().0
25            }
26            #[inline]
27            pub const fn intersects(self, other: Self) -> bool {
28                !Self(self.0 & other.0).is_empty()
29            }
30            #[doc = r" Returns whether `other` is a subset of `self`"]
31            #[inline]
32            pub const fn contains(self, other: Self) -> bool {
33                self.0 & other.0 == other.0
34            }
35        }
36        impl ::core::ops::BitOr for $name {
37            type Output = Self;
38            #[inline]
39            fn bitor(self, rhs: Self) -> Self {
40                Self(self.0 | rhs.0)
41            }
42        }
43        impl ::core::ops::BitOrAssign for $name {
44            #[inline]
45            fn bitor_assign(&mut self, rhs: Self) {
46                *self = *self | rhs
47            }
48        }
49        impl ::core::ops::BitAnd for $name {
50            type Output = Self;
51            #[inline]
52            fn bitand(self, rhs: Self) -> Self {
53                Self(self.0 & rhs.0)
54            }
55        }
56        impl ::core::ops::BitAndAssign for $name {
57            #[inline]
58            fn bitand_assign(&mut self, rhs: Self) {
59                *self = *self & rhs
60            }
61        }
62        impl ::core::ops::BitXor for $name {
63            type Output = Self;
64            #[inline]
65            fn bitxor(self, rhs: Self) -> Self {
66                Self(self.0 ^ rhs.0)
67            }
68        }
69        impl ::core::ops::BitXorAssign for $name {
70            #[inline]
71            fn bitxor_assign(&mut self, rhs: Self) {
72                *self = *self ^ rhs
73            }
74        }
75        impl ::core::ops::Not for $name {
76            type Output = Self;
77            #[inline]
78            fn not(self) -> Self {
79                Self(!self.0)
80            }
81        }
82    };
83}
84#[macro_export]
85macro_rules! handle_nondispatchable {
86    ($ name : ident , $ ty : ident) => {
87        handle_nondispatchable!($name, $ty, doc = "");
88    };
89    ($ name : ident , $ ty : ident , $ doc_link : meta) => {
90        #[repr(transparent)]
91        #[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Default)]
92        #[$doc_link]
93        pub struct $name(u64);
94        impl Handle for $name {
95            const TYPE: ObjectType = ObjectType::$ty;
96            fn as_raw(self) -> u64 {
97                self.0
98            }
99            fn from_raw(x: u64) -> Self {
100                Self(x)
101            }
102        }
103        impl $name {
104            pub const fn null() -> Self {
105                Self(0)
106            }
107        }
108        impl fmt::Pointer for $name {
109            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110                write!(f, "0x{:x}", self.0)
111            }
112        }
113        impl fmt::Debug for $name {
114            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115                write!(f, "0x{:x}", self.0)
116            }
117        }
118    };
119}
120#[macro_export]
121macro_rules! define_handle {
122    ($ name : ident , $ ty : ident) => {
123        define_handle!($name, $ty, doc = "");
124    };
125    ($ name : ident , $ ty : ident , $ doc_link : meta) => {
126        #[repr(transparent)]
127        #[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash)]
128        #[$doc_link]
129        pub struct $name(*mut u8);
130        impl Default for $name {
131            fn default() -> Self {
132                Self::null()
133            }
134        }
135        impl Handle for $name {
136            const TYPE: ObjectType = ObjectType::$ty;
137            fn as_raw(self) -> u64 {
138                self.0 as u64
139            }
140            fn from_raw(x: u64) -> Self {
141                Self(x as _)
142            }
143        }
144        unsafe impl Send for $name {}
145        unsafe impl Sync for $name {}
146        impl $name {
147            pub const fn null() -> Self {
148                Self(::core::ptr::null_mut())
149            }
150        }
151        impl fmt::Pointer for $name {
152            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
153                fmt::Pointer::fmt(&self.0, f)
154            }
155        }
156        impl fmt::Debug for $name {
157            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158                fmt::Debug::fmt(&self.0, f)
159            }
160        }
161    };
162}