tower_http/compression/
pin_project_cfg.rs

1// Full credit to @tesaguri who posted this gist under CC0 1.0 Universal licence
2// https://gist.github.com/tesaguri/2a1c0790a48bbda3dd7f71c26d02a793
3
4macro_rules! pin_project_cfg {
5    ($(#[$($attr:tt)*])* $vis:vis enum $($rest:tt)+) => {
6        pin_project_cfg! {
7            @outer [$(#[$($attr)*])* $vis enum] $($rest)+
8        }
9    };
10    // Accumulate type parameters and `where` clause.
11    (@outer [$($accum:tt)*] $tt:tt $($rest:tt)+) => {
12        pin_project_cfg! {
13            @outer [$($accum)* $tt] $($rest)+
14        }
15    };
16    (@outer [$($accum:tt)*] { $($body:tt)* }) => {
17        pin_project_cfg! {
18            @body #[cfg(all())] [$($accum)*] {} $($body)*
19        }
20    };
21    // Process a variant with `cfg`.
22    (
23        @body
24        #[cfg(all($($pred_accum:tt)*))]
25        $outer:tt
26        { $($accum:tt)* }
27
28        #[cfg($($pred:tt)*)]
29        $(#[$($attr:tt)*])* $variant:ident { $($body:tt)* },
30        $($rest:tt)*
31    ) => {
32        // Create two versions of the enum with `cfg($pred)` and `cfg(not($pred))`.
33        pin_project_cfg! {
34            @variant_body
35            { $($body)* }
36            {}
37            #[cfg(all($($pred_accum)* $($pred)*,))]
38            $outer
39            { $($accum)* $(#[$($attr)*])* $variant }
40            $($rest)*
41        }
42        pin_project_cfg! {
43            @body
44            #[cfg(all($($pred_accum)* not($($pred)*),))]
45            $outer
46            { $($accum)* }
47            $($rest)*
48        }
49    };
50    // Process a variant without `cfg`.
51    (
52        @body
53        #[cfg(all($($pred_accum:tt)*))]
54        $outer:tt
55        { $($accum:tt)* }
56
57        $(#[$($attr:tt)*])* $variant:ident { $($body:tt)* },
58        $($rest:tt)*
59    ) => {
60        pin_project_cfg! {
61            @variant_body
62            { $($body)* }
63            {}
64            #[cfg(all($($pred_accum)*))]
65            $outer
66            { $($accum)* $(#[$($attr)*])* $variant }
67            $($rest)*
68        }
69    };
70    // Process a variant field with `cfg`.
71    (
72        @variant_body
73        {
74            #[cfg($($pred:tt)*)]
75            $(#[$($attr:tt)*])* $field:ident: $ty:ty,
76            $($rest:tt)*
77        }
78        { $($accum:tt)* }
79        #[cfg(all($($pred_accum:tt)*))]
80        $($outer:tt)*
81    ) => {
82        pin_project_cfg! {
83            @variant_body
84            {$($rest)*}
85            { $($accum)* $(#[$($attr)*])* $field: $ty, }
86            #[cfg(all($($pred_accum)* $($pred)*,))]
87            $($outer)*
88        }
89        pin_project_cfg! {
90            @variant_body
91            { $($rest)* }
92            { $($accum)* }
93            #[cfg(all($($pred_accum)* not($($pred)*),))]
94            $($outer)*
95        }
96    };
97    // Process a variant field without `cfg`.
98    (
99        @variant_body
100        {
101            $(#[$($attr:tt)*])* $field:ident: $ty:ty,
102            $($rest:tt)*
103        }
104        { $($accum:tt)* }
105        $($outer:tt)*
106    ) => {
107        pin_project_cfg! {
108            @variant_body
109            {$($rest)*}
110            { $($accum)* $(#[$($attr)*])* $field: $ty, }
111            $($outer)*
112        }
113    };
114    (
115        @variant_body
116        {}
117        $body:tt
118        #[cfg(all($($pred_accum:tt)*))]
119        $outer:tt
120        { $($accum:tt)* }
121        $($rest:tt)*
122    ) => {
123        pin_project_cfg! {
124            @body
125            #[cfg(all($($pred_accum)*))]
126            $outer
127            { $($accum)* $body, }
128            $($rest)*
129        }
130    };
131    (
132        @body
133        #[$cfg:meta]
134        [$($outer:tt)*]
135        $body:tt
136    ) => {
137        #[$cfg]
138        pin_project_lite::pin_project! {
139            $($outer)* $body
140        }
141    };
142}
143
144pub(crate) use pin_project_cfg;