openxr_sys/
support.rs

1//! Static helpers used by generated code
2use std::fmt;
3
4macro_rules! wrapper {
5    {$(#[$meta: meta])* $ident:ident($ty:ty)} => {
6        $(#[$meta])* #[repr(transparent)]
7        pub struct $ident($ty);
8        impl $ident {
9            pub fn from_raw(x: $ty) -> Self { Self(x) }
10            pub fn into_raw(self) -> $ty { self.0 }
11        }
12    }
13}
14
15macro_rules! bitmask {
16    ($name:ident) => {
17        impl $name {
18            pub const EMPTY: Self = Self(0);
19
20            #[inline]
21            pub fn from_raw(x: u64) -> Self {
22                Self(x)
23            }
24
25            #[inline]
26            pub fn into_raw(self) -> u64 {
27                self.0
28            }
29
30            #[inline]
31            pub fn is_empty(self) -> bool {
32                self == $name::EMPTY
33            }
34
35            #[inline]
36            pub fn intersects(self, other: $name) -> bool {
37                self & other != $name::EMPTY
38            }
39
40            /// Returns whether `other` is a subset of `self`
41            #[inline]
42            pub fn contains(self, other: $name) -> bool {
43                self & other == other
44            }
45        }
46
47        impl Default for $name {
48            fn default() -> Self {
49                Self::EMPTY
50            }
51        }
52
53        impl std::ops::BitOr for $name {
54            type Output = $name;
55
56            #[inline]
57            fn bitor(self, rhs: $name) -> $name {
58                $name(self.0 | rhs.0)
59            }
60        }
61
62        impl std::ops::BitOrAssign for $name {
63            #[inline]
64            fn bitor_assign(&mut self, rhs: $name) {
65                *self = *self | rhs
66            }
67        }
68
69        impl std::ops::BitAnd for $name {
70            type Output = $name;
71
72            #[inline]
73            fn bitand(self, rhs: $name) -> $name {
74                $name(self.0 & rhs.0)
75            }
76        }
77
78        impl std::ops::BitAndAssign for $name {
79            #[inline]
80            fn bitand_assign(&mut self, rhs: $name) {
81                *self = *self & rhs
82            }
83        }
84
85        impl std::ops::BitXor for $name {
86            type Output = $name;
87
88            #[inline]
89            fn bitxor(self, rhs: $name) -> $name {
90                $name(self.0 ^ rhs.0)
91            }
92        }
93
94        impl std::ops::BitXorAssign for $name {
95            #[inline]
96            fn bitxor_assign(&mut self, rhs: $name) {
97                *self = *self ^ rhs
98            }
99        }
100
101        impl std::ops::Not for $name {
102            type Output = $name;
103
104            #[inline]
105            fn not(self) -> $name {
106                Self(!self.0)
107            }
108        }
109    };
110}
111
112macro_rules! handle {
113    ($name:ident) => {
114        impl $crate::Handle for $name {
115            const NULL: Self = Self(0);
116            #[inline]
117            fn from_raw(x: u64) -> Self {
118                Self(x)
119            }
120            #[inline]
121            fn into_raw(self) -> u64 {
122                self.0
123            }
124        }
125        impl Default for $name {
126            fn default() -> Self {
127                Self::NULL
128            }
129        }
130    };
131}
132
133pub fn fmt_enum(f: &mut fmt::Formatter, value: i32, name: Option<&'static str>) -> fmt::Result {
134    match name {
135        Some(x) => f.pad(x),
136        None => <i32 as fmt::Debug>::fmt(&value, f),
137    }
138}