1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#![cfg_attr(rustfmt, rustfmt::skip)]
#![allow(unused_macros)]

macro_rules! use_prelude { () => (
    #[allow(unused_imports)]
    use crate::utils::prelude::*;
)}

/// I really don't get the complexity of `cfg_if!`…
macro_rules! cfg_match {
    (
        _ => { $($expansion:tt)* } $(,)?
    ) => (
        $($expansion)*
    );

    (
        $cfg:meta => $expansion:tt $(,
        $($($rest:tt)+)? )?
    ) => (
        #[cfg($cfg)]
        cfg_match! { _ => $expansion } $($(

        #[cfg(not($cfg))]
        cfg_match! { $($rest)+ } )?)?
    );

    // Bonus: expression-friendly syntax: `cfg_match!({ … })`
    ({
        $($input:tt)*
    }) => ({
        cfg_match! { $($input)* }
    });
}

cfg_match! {
    feature = "alloc" => {
        macro_rules! cfg_alloc {(
            $($item:item)*
        ) => (
            $(
                #[cfg_attr(all(feature = "docs"), doc(cfg(feature = "alloc")))]
                $item
            )*
        )}
    },
    _ => {
        macro_rules! cfg_alloc {(
            $($item:item)*
        ) => (
            // Nothing
        )}
    },
}

cfg_match! {
    feature = "std" => {
        macro_rules! cfg_std {(
            $($item:item)*
        ) => (
            $(
                #[cfg_attr(all(feature = "docs"), doc(cfg(feature = "std")))]
                $item
            )*
        )}
    },
    _ => {
        macro_rules! cfg_std {(
            $($item:item)*
        ) => (
            // Nothing
        )}
    },
}

macro_rules! cfg_wasm {( $($item:item)* ) => (
    $(
        #[cfg(target_arch = "wasm32")]
        $item
    )*
)}
macro_rules! cfg_not_wasm {( $($item:item)* ) => (
    $(
        #[cfg(not(target_arch = "wasm32"))]
        $item
    )*
)}

macro_rules! const_assert {
    (
        for [$($generics:tt)*]
        [$($($pre:tt)+)?] => [$($post:tt)*]
    ) => (
        const _: () = {
            fn check<$($generics)*> ()
            where
                $($($pre)+)?
            {
                fn const_assert<$($generics)*> ()
                where
                    $($($pre)* ,)?
                    $($post)*
                {}
                let _ = const_assert::<$($generics)*>;
            }
        };
    );

    (
        $cond:expr
    ) => (
        const _: [(); 1] = [(); {
            const COND: bool = $cond;
            COND
        } as usize];
    );
}

macro_rules! type_level_enum {(
    $( #[doc = $doc:tt] )*
    $pub:vis
    enum $EnumName:ident {
        $(
            $( #[doc = $doc_variant:tt] )*
            $Variant:ident
        ),* $(,)?
    }
) => (
    $( #[doc = $doc] )*
    ///
    /// ### Type-level `enum`
    ///
    /// Until `const_generics` can handle custom `enum`s, this pattern must be
    /// implemented at the type level.
    ///
    /// We thus end up with:
    ///
    /// ```rust,ignore
    /// #[type_level_enum]
    #[doc = ::core::concat!(
        " enum ", ::core::stringify!($EnumName), " {",
    )]
    $(
        #[doc = ::core::concat!(
            "         ", ::core::stringify!($Variant), ",",
        )]
    )*
    #[doc = " }"]
    /// ```
    ///
    #[doc = ::core::concat!(
        "With [`", ::core::stringify!($EnumName), "::T`](#reexports) \
        being the type-level \"enum type\":",
    )]
    ///
    /// ```rust,ignore
    #[doc = ::core::concat!(
        "<Param : ", ::core::stringify!($EnumName), "::T>"
    )]
    /// ```
    #[allow(warnings)]
    $pub mod $EnumName {
        #[doc(no_inline)]
        pub use $EnumName as T;

        #[doc = ::core::concat!(
            "See [`", ::core::stringify!($EnumName), "`]",
            "[super::", ::core::stringify!($EnumName), "]"
        )]
        pub
        trait $EnumName
        :
            __sealed::$EnumName +
            ::core::marker::Sized +
            'static +
        {
            const VALUE: __value::$EnumName;
        }

        mod __sealed { pub trait $EnumName {} }

        mod __value {
            #[derive(Debug, PartialEq, Eq)]
            pub enum $EnumName { $( $Variant ),* }
        }

        $(
            $( #[doc = $doc_variant] )*
            pub enum $Variant {}
            impl __sealed::$EnumName for $Variant {}
            impl $EnumName for $Variant {
                const VALUE: __value::$EnumName =
                    __value::$EnumName::$Variant
                ;
            }
            impl $Variant {
                pub const VALUE: __value::$EnumName =
                    __value::$EnumName::$Variant
                ;
            }
        )*
    }
)}

macro_rules! doc_test {
    ($name:ident :
        #![$attr:ident]
        $($code:tt)*
    ) => (
        const _: () = {
            #[doc = concat!(
                "```rust,", stringify!($attr), "\n",
                stringify!($($code)*), "\n",
            )]
            /// ```
            pub mod $name {}
        };
    );

    ($name:ident :
        $($code:tt)*
    ) => (
        const _: () = {
            #[doc = concat!(
                "```rust\n",
                stringify!($($code)*), "\n",
            )]
            /// ```
            pub mod $name {}
        };
    );
}

doc_test! { c_str:
    use ::safer_ffi::prelude::*;

    let _ = c!("Hello, World!");
}
doc_test! { c_str_inner_nul_byte:
    #![compile_fail]
    use ::safer_ffi::prelude::*;

    let _ = c!("Hell\0, World!");
}

/// Items exported through this macro are internal implementation details
/// that exported macros may need access to.
///
/// Users of this crate should not directly use them (unless the pinpoint the
/// version dependency), since they are considered to be semver-exempt and
/// could thus cause breakage.
macro_rules! hidden_export {
    (
        $(#[$attr:meta])*
        macro_rules! $($rest:tt)*
    ) => (
        $(#[$attr])*
        #[doc(hidden)] /** Not part of the public API **/ #[macro_export]
        macro_rules! $($rest)*
    );

    (
        @attrs[ $($attr:tt)* ]
        #[$current:meta]
        $($rest:tt)*
    ) => (
        hidden_export! {
            @attrs[ $($attr)* $current ]
            $($rest)*
        }
    );

    (
        @attrs[ $($attr:tt)* ]
        $($item:tt)*
    ) => (
        $(#[$attr])*
        #[doc(hidden)] /** Not part of the public API **/ pub
        $($item)*
    );

    (
        $($input:tt)*
    ) => (
        hidden_export! {
            @attrs[]
            $($input)*
        }
    )
}

macro_rules! match_ {(
    ( $($scrutinee:tt)* ) $rules:tt
) => (
    macro_rules! __recurse__ $rules
    __recurse__! { $($scrutinee)* }
)}