]>
git.proxmox.com Git - rustc.git/blob - vendor/nix/src/macros.rs
1 // Thanks to Tokio for this macro
9 #[cfg_attr(docsrs, doc(cfg($meta)))]
15 /// The `libc_bitflags!` macro helps with a common use case of defining a public bitflags type
16 /// with values from the libc crate. It is used the same way as the `bitflags!` macro, except
17 /// that only the name of the flag value has to be given.
19 /// The `libc` crate must be in scope with the name `libc`.
24 /// pub struct ProtFlags: libc::c_int {
27 /// /// PROT_WRITE enables write protect
30 /// #[cfg(any(target_os = "linux", target_os = "android"))]
32 /// #[cfg(any(target_os = "linux", target_os = "android"))]
38 /// Example with casting, due to a mistake in libc. In this example, the
39 /// various flags have different types, so we cast the broken ones to the right
44 /// pub struct SaFlags: libc::c_ulong {
45 /// SA_NOCLDSTOP as libc::c_ulong;
47 /// SA_NODEFER as libc::c_ulong;
49 /// SA_RESETHAND as libc::c_ulong;
50 /// SA_RESTART as libc::c_ulong;
55 macro_rules
! libc_bitflags
{
58 pub struct $BitFlags
:ident
: $T
:ty
{
60 $
(#[$inner:ident $($args:tt)*])*
61 $Flag
:ident $
(as $cast
:ty
)*;
65 ::bitflags
::bitflags
! {
67 pub struct $BitFlags
: $T
{
69 $
(#[$inner $($args)*])*
70 const $Flag
= libc
::$Flag $
(as $cast
)*;
77 /// The `libc_enum!` macro helps with a common use case of defining an enum exclusively using
78 /// values from the `libc` crate. This macro supports both `pub` and private `enum`s.
80 /// The `libc` crate must be in scope with the name `libc`.
85 /// pub enum ProtFlags {
90 /// #[cfg(any(target_os = "linux", target_os = "android"))]
92 /// #[cfg(any(target_os = "linux", target_os = "android"))]
97 // Some targets don't use all rules.
98 #[allow(unknown_lints)]
99 #[allow(unused_macro_rules)]
100 macro_rules
! libc_enum
{
103 name
: $BitFlags
:ident
,
106 attrs
: [$
($attrs
:tt
)*],
107 entries
: [$
($entries
:tt
)*],
111 #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
117 // Exit rule including TryFrom
119 name
: $BitFlags
:ident
,
122 attrs
: [$
($attrs
:tt
)*],
123 entries
: [$
($entries
:tt
)*],
124 from_type
: $repr
:path
,
125 try_froms
: [$
($try_froms
:tt
)*]
129 #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
133 impl ::std
::convert
::TryFrom
<$repr
> for $BitFlags
{
134 type Error
= $
crate::Error
;
135 #[allow(unused_doc_comments)]
136 fn try_from(x
: $repr
) -> $
crate::Result
<Self> {
139 _
=> Err($
crate::Error
::EINVAL
)
145 // Done accumulating.
147 name
: $BitFlags
:ident
,
166 // Done accumulating and want TryFrom
168 name
: $BitFlags
:ident
,
172 from_type
: $repr
:path
,
185 try_froms
: $try_froms
192 name
: $BitFlags
:ident
,
196 #[$attr:meta] $($tail:tt)*
214 // Munch last ident if not followed by a comma.
216 name
: $BitFlags
:ident
,
228 $entry
= libc
::$entry
,
232 libc
::$entry
=> Ok($BitFlags
::$entry
),
237 // Munch an ident; covers terminating comma case.
239 name
: $BitFlags
:ident
,
252 $entry
= libc
::$entry
,
256 libc
::$entry
=> Ok($BitFlags
::$entry
),
262 // Munch an ident and cast it to the given type; covers terminating comma.
264 name
: $BitFlags
:ident
,
268 $entry
:ident
as $ty
:ty
,
277 $entry
= libc
::$entry
as $ty
,
281 libc
::$entry
as $ty
=> Ok($BitFlags
::$entry
),
290 $v
:vis
enum $BitFlags
:ident
{
299 attrs
: [$
(#[$attr])*],
307 // Entry rule including TryFrom
310 $v
:vis
enum $BitFlags
:ident
{
313 impl TryFrom
<$repr
:path
>
320 attrs
: [$
(#[$attr])*],