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
// Full credit to @tesaguri who posted this gist under CC0 1.0 Universal licence
// https://gist.github.com/tesaguri/2a1c0790a48bbda3dd7f71c26d02a793

macro_rules! pin_project_cfg {
    ($(#[$($attr:tt)*])* $vis:vis enum $($rest:tt)+) => {
        pin_project_cfg! {
            @outer [$(#[$($attr)*])* $vis enum] $($rest)+
        }
    };
    // Accumulate type parameters and `where` clause.
    (@outer [$($accum:tt)*] $tt:tt $($rest:tt)+) => {
        pin_project_cfg! {
            @outer [$($accum)* $tt] $($rest)+
        }
    };
    (@outer [$($accum:tt)*] { $($body:tt)* }) => {
        pin_project_cfg! {
            @body #[cfg(all())] [$($accum)*] {} $($body)*
        }
    };
    // Process a variant with `cfg`.
    (
        @body
        #[cfg(all($($pred_accum:tt)*))]
        $outer:tt
        { $($accum:tt)* }

        #[cfg($($pred:tt)*)]
        $(#[$($attr:tt)*])* $variant:ident { $($body:tt)* },
        $($rest:tt)*
    ) => {
        // Create two versions of the enum with `cfg($pred)` and `cfg(not($pred))`.
        pin_project_cfg! {
            @variant_body
            { $($body)* }
            {}
            #[cfg(all($($pred_accum)* $($pred)*,))]
            $outer
            { $($accum)* $(#[$($attr)*])* $variant }
            $($rest)*
        }
        pin_project_cfg! {
            @body
            #[cfg(all($($pred_accum)* not($($pred)*),))]
            $outer
            { $($accum)* }
            $($rest)*
        }
    };
    // Process a variant without `cfg`.
    (
        @body
        #[cfg(all($($pred_accum:tt)*))]
        $outer:tt
        { $($accum:tt)* }

        $(#[$($attr:tt)*])* $variant:ident { $($body:tt)* },
        $($rest:tt)*
    ) => {
        pin_project_cfg! {
            @variant_body
            { $($body)* }
            {}
            #[cfg(all($($pred_accum)*))]
            $outer
            { $($accum)* $(#[$($attr)*])* $variant }
            $($rest)*
        }
    };
    // Process a variant field with `cfg`.
    (
        @variant_body
        {
            #[cfg($($pred:tt)*)]
            $(#[$($attr:tt)*])* $field:ident: $ty:ty,
            $($rest:tt)*
        }
        { $($accum:tt)* }
        #[cfg(all($($pred_accum:tt)*))]
        $($outer:tt)*
    ) => {
        pin_project_cfg! {
            @variant_body
            {$($rest)*}
            { $($accum)* $(#[$($attr)*])* $field: $ty, }
            #[cfg(all($($pred_accum)* $($pred)*,))]
            $($outer)*
        }
        pin_project_cfg! {
            @variant_body
            { $($rest)* }
            { $($accum)* }
            #[cfg(all($($pred_accum)* not($($pred)*),))]
            $($outer)*
        }
    };
    // Process a variant field without `cfg`.
    (
        @variant_body
        {
            $(#[$($attr:tt)*])* $field:ident: $ty:ty,
            $($rest:tt)*
        }
        { $($accum:tt)* }
        $($outer:tt)*
    ) => {
        pin_project_cfg! {
            @variant_body
            {$($rest)*}
            { $($accum)* $(#[$($attr)*])* $field: $ty, }
            $($outer)*
        }
    };
    (
        @variant_body
        {}
        $body:tt
        #[cfg(all($($pred_accum:tt)*))]
        $outer:tt
        { $($accum:tt)* }
        $($rest:tt)*
    ) => {
        pin_project_cfg! {
            @body
            #[cfg(all($($pred_accum)*))]
            $outer
            { $($accum)* $body, }
            $($rest)*
        }
    };
    (
        @body
        #[$cfg:meta]
        [$($outer:tt)*]
        $body:tt
    ) => {
        #[$cfg]
        pin_project_lite::pin_project! {
            $($outer)* $body
        }
    };
}

pub(crate) use pin_project_cfg;