1macro_rules! result {
2 ($error:expr, $good:expr) => {
3 match $error {
4 $crate::sys::Error(0) => Ok($good),
5
6 $crate::sys::Error(err) => {
7 Err($crate::Error::from_c(err).unwrap_or($crate::Error::Bindings))
8 }
9 }
10 };
11
12 ($error:expr) => {
13 result!($error, ())
14 };
15}
16
17macro_rules! c_enum {
18 (
19 $(#[$enum_meta:meta])*
20 $vis:vis enum $EnumName:ident: $Primitive:ident {
21 $(
22 $(#[$variant_meta:meta])*
23 $Variant:ident $(= $Value:expr)?
24 ),*
25 $(,)?
26 }
27 ) => {
28 $(#[$enum_meta])*
29 #[repr($Primitive)]
30 $vis enum $EnumName {
31 $(
32 $(#[$variant_meta])*
33 $Variant $(= $Value)?
34 ),*
35 }
36
37 impl $EnumName {
38 #[allow(dead_code)]
40 pub(crate) fn to_primitive(self) -> $Primitive {
41 self as $Primitive
42 }
43
44 #[allow(dead_code)]
46 pub(crate) fn from_primitive(primitive: $Primitive) -> Option<Self> {
47 match primitive {
48 $( _ if primitive == Self::$Variant as $Primitive => Some(Self::$Variant) ,)*
49 _ => None,
50 }
51 }
52
53 #[allow(dead_code)]
55 pub(crate) fn to_c(self) -> libc::c_int {
56 self as $Primitive as libc::c_int
57 }
58
59 #[allow(dead_code)]
61 pub(crate) fn from_c(c: impl core::convert::TryInto<$Primitive>) -> Option<Self> {
62 if let Ok(v) = c.try_into() {
63 Self::from_primitive(v)
64 } else {
65 None
66 }
67 }
68 }
69 };
70}
71
72macro_rules! c_enum_big {
73 (
74 $(#[$enum_meta:meta])*
75 $vis:vis enum $EnumName:ident: $Primitive:ident {
76 @Start = $StartVariant:ident,
77 @End = $EndVariant:ident,
78 $(
79 $(#[$variant_meta:meta])*
80 $Variant:ident $(= $Value:expr)?
81 ),*
82 $(,)?
83 }
84 ) => {
85 $(#[$enum_meta])*
86 #[repr($Primitive)]
87 $vis enum $EnumName {
88 $(
89 $(#[$variant_meta])*
90 $Variant $(= $Value)?
91 ),*
92 }
93
94 impl $EnumName {
95 #[allow(dead_code)]
97 pub(crate) fn to_primitive(self) -> $Primitive {
98 self as $Primitive
99 }
100
101 #[allow(dead_code)]
103 pub(crate) fn from_primitive(primitive: $Primitive) -> Option<Self> {
104 if primitive < $EnumName::$StartVariant as $Primitive || primitive >= $EnumName::$EndVariant as $Primitive {
105 return None;
106 }
107 Some(unsafe { core::mem::transmute::<$Primitive, $EnumName>(primitive) })
108 }
109
110 #[allow(dead_code)]
112 pub(crate) fn to_c(self) -> libc::c_int {
113 self as $Primitive as libc::c_int
114 }
115
116 #[allow(dead_code)]
118 pub(crate) fn from_c(c: impl core::convert::TryInto<$Primitive>) -> Option<Self> {
119 if let Ok(v) = c.try_into() {
120 Self::from_primitive(v)
121 } else {
122 None
123 }
124 }
125 }
126 };
127}