|
1 | | -/// Concatenates supplied slices into one continuous vector. |
2 | | -#[macro_export] |
3 | | -macro_rules! concat_slices { |
4 | | - ($($slice: expr),+) => { |
5 | | - [$(&$slice[..]),+].concat() |
6 | | - } |
7 | | -} |
8 | | - |
9 | | -/// Implements `Deref`/`DeferMut` traits for the supplied wrapper and type. |
10 | | -#[macro_export] |
11 | | -macro_rules! impl_deref { |
12 | | - ($wrapper: ident$(<$($ty: ident: $($by: path),+),*>)?($type: path)) => { |
13 | | - impl$(<$($ty: $($by)++),+>)* core::ops::Deref for $wrapper$(<$($ty),+>)* { |
14 | | - type Target = $type; |
15 | | - |
16 | | - fn deref(&self) -> &Self::Target { |
17 | | - &self.0 |
18 | | - } |
19 | | - } |
20 | | - |
21 | | - impl$(<$($ty: $($by)++),+>)* core::ops::DerefMut for $wrapper$(<$($ty),+>)* { |
22 | | - fn deref_mut(&mut self) -> &mut Self::Target { |
23 | | - &mut self.0 |
24 | | - } |
25 | | - } |
26 | | - }; |
27 | | -} |
28 | | - |
29 | 1 | /// Convert given slices to `OwnedPairs`, panics in case of error. |
30 | 2 | #[macro_export] |
31 | 3 | macro_rules! owned_pairs { |
@@ -61,185 +33,3 @@ macro_rules! try_pairs { |
61 | 33 | $crate::helpers::Pairs::try_from((&$left[..], &$right[..])) |
62 | 34 | }; |
63 | 35 | } |
64 | | - |
65 | | -/// Calculates the product of pairing for supplied pairs. |
66 | | -/// ```compile_fail |
67 | | -/// multi_pairing! { |
68 | | -/// a, c, |
69 | | -/// b, d |
70 | | -/// } |
71 | | -/// ``` |
72 | | -/// Will be transformed to: |
73 | | -/// ```compile_fail |
74 | | -/// E::multi_pairing([a, b], [c, d]) |
75 | | -/// ``` |
76 | | -#[macro_export] |
77 | | -macro_rules! multi_pairing { |
78 | | - ($($g1: expr, $g2: expr);+) => { |
79 | | - $crate::multi_pairing! { using E: $($g1, $g2);+ } |
80 | | - }; |
81 | | - (using $pairing_engine: path: $($g1: expr, $g2: expr);+) => { |
82 | | - <$pairing_engine>::multi_pairing( |
83 | | - [ |
84 | | - $($g1.into()),+ |
85 | | - ], |
86 | | - [ |
87 | | - $($g2.into()),+ |
88 | | - ] |
89 | | - ) |
90 | | - } |
91 | | -} |
92 | | - |
93 | | -/// Flattened `rayon::join(|| expr1, || rayon::join(|| expr2, || ...))` |
94 | | -#[cfg(feature = "parallel")] |
95 | | -#[macro_export] |
96 | | -macro_rules! join { |
97 | | - (@ $a: expr) => { $a }; |
98 | | - (@ $a: expr, $b: expr) => { |
99 | | - rayon::join(|| $a, || $b) |
100 | | - }; |
101 | | - (@ $a: expr, $b: expr, $($c: expr),+) => {{ |
102 | | - join!(@ $a, join!(@ $b, $($c),+)) |
103 | | - }}; |
104 | | - ($($e: expr),+) => {{ |
105 | | - $crate::unnest_tuple!( |
106 | | - $($e),+ |
107 | | - => |
108 | | - join!(@ $($e),+) |
109 | | - ) |
110 | | - }} |
111 | | -} |
112 | | - |
113 | | -/// `(expr1, expr2, expr3...)` |
114 | | -#[cfg(not(feature = "parallel"))] |
115 | | -#[macro_export] |
116 | | -macro_rules! join { |
117 | | - ($($e: expr),+) => { |
118 | | - ($($e),+) |
119 | | - }; |
120 | | -} |
121 | | - |
122 | | -/// `(a, (b, c)) => (a, b, c)` |
123 | | -#[macro_export] |
124 | | -macro_rules! unnest_tuple { |
125 | | - ($a: expr => $v: expr) => {{ |
126 | | - $v |
127 | | - }}; |
128 | | - ($a: expr, $b: expr => $v: expr) => {{ |
129 | | - let (_a, _b) = $v; |
130 | | - |
131 | | - (_a, _b) |
132 | | - }}; |
133 | | - ($a: expr, $b: expr, $c: expr => $v: expr) => {{ |
134 | | - let (_a, (_b, _c)) = $v; |
135 | | - |
136 | | - (_a, _b, _c) |
137 | | - }}; |
138 | | - ($a: expr, $b: expr, $c: expr, $d: expr => $v: expr) => {{ |
139 | | - let (_a, (_b, (_c, _d))) = $v; |
140 | | - |
141 | | - (_a, _b, _c, _d) |
142 | | - }}; |
143 | | - ($a: expr, $b: expr, $c: expr, $d: expr, $e: expr => $v: expr) => {{ |
144 | | - let (_a, (_b, (_c, (_d, _e)))) = $v; |
145 | | - |
146 | | - (_a, _b, _c, _d, _e) |
147 | | - }}; |
148 | | - ($a: expr, $b: expr, $c: expr, $d: expr, $e: expr, $f: expr => $v: expr) => {{ |
149 | | - let (_a, (_b, (_c, (_d, (_e, _f))))) = $v; |
150 | | - |
151 | | - (_a, _b, _c, _d, _e, _f) |
152 | | - }}; |
153 | | -} |
154 | | - |
155 | | -/// `impl Iterator` or `impl ParallelIterator` depending on the `parallel` feature. |
156 | | -#[macro_export] |
157 | | -#[cfg(feature = "parallel")] |
158 | | -macro_rules! impl_iter { |
159 | | - (<Item = $item: ty> $($tt: tt)*) => { impl rayon::prelude::ParallelIterator<Item = $item> $($tt)* } |
160 | | -} |
161 | | - |
162 | | -/// `impl Iterator` or `impl ParallelIterator` depending on the `parallel` feature. |
163 | | -#[macro_export] |
164 | | -#[cfg(not(feature = "parallel"))] |
165 | | -macro_rules! impl_iter { |
166 | | - (<Item = $item: ty> $($tt: tt)*) => { impl core::iter::Iterator<Item = $item> $($tt)* } |
167 | | -} |
168 | | - |
169 | | -/// `impl IntoIterator` or `impl IntoParallelIterator` depending on the `parallel` feature. |
170 | | -#[macro_export] |
171 | | -#[cfg(feature = "parallel")] |
172 | | -macro_rules! impl_into_iter { |
173 | | - (<Item = $item: ty> $($tt: tt)*) => { impl rayon::prelude::IntoParallelIterator<Item = $item> $($tt)* } |
174 | | -} |
175 | | - |
176 | | -/// `impl IntoIterator` or `impl IntoParallelIterator` depending on the `parallel` feature. |
177 | | -#[macro_export] |
178 | | -#[cfg(not(feature = "parallel"))] |
179 | | -macro_rules! impl_into_iter { |
180 | | - (<Item = $item: ty> $($tt: tt)*) => { impl core::iter::IntoIterator<Item = $item> $($tt)* } |
181 | | -} |
182 | | - |
183 | | -/// `impl DoubleEndedIterator + ExactSizeIterator` or `impl IndexedParallelIterator` depending on the `parallel` feature. |
184 | | -#[macro_export] |
185 | | -#[cfg(feature = "parallel")] |
186 | | -macro_rules! impl_indexed_iter { |
187 | | - (<Item = $item: ty> $($tt: tt)*) => { impl rayon::prelude::IndexedParallelIterator<Item = $item> $($tt)* } |
188 | | -} |
189 | | - |
190 | | -/// `impl DoubleEndedIterator + ExactSizeIterator` or `impl IndexedParallelIterator` depending on the `parallel` feature. |
191 | | -#[macro_export] |
192 | | -#[cfg(not(feature = "parallel"))] |
193 | | -macro_rules! impl_indexed_iter { |
194 | | - (<Item = $item: ty> $($tt: tt)*) => { impl $crate::helpers::DoubleEndedExactSizeIterator<Item = $item> $($tt)* } |
195 | | -} |
196 | | - |
197 | | -/// `impl IntoIterator` where `IntoIter: DoubleEndedIterator + ExactSizeIterator` or `impl IntoParallelIterator` where `Iter: IndexedParallelIterator` depending on the `parallel` feature. |
198 | | -#[macro_export] |
199 | | -#[cfg(feature = "parallel")] |
200 | | -macro_rules! impl_into_indexed_iter { |
201 | | - (<Item = $item: ty> $($tt: tt)*) => { impl rayon::prelude::IntoParallelIterator<Item = $item, Iter = impl rayon::prelude::IndexedParallelIterator<Item = $item> $($tt)*> $($tt)* } |
202 | | -} |
203 | | - |
204 | | -/// `impl IntoIterator` where `IntoIter: DoubleEndedIterator + ExactSizeIterator` or `impl IntoParallelIterator` where `Iter: IndexedParallelIterator` depending on the `parallel` feature. |
205 | | -#[macro_export] |
206 | | -#[cfg(not(feature = "parallel"))] |
207 | | -macro_rules! impl_into_indexed_iter { |
208 | | - (<Item = $item: ty> $($tt: tt)*) => { impl core::iter::IntoIterator<Item = $item, IntoIter = impl $crate::helpers::DoubleEndedExactSizeIterator<Item = $item> $($tt)*> $($tt)* } |
209 | | -} |
210 | | - |
211 | | -#[cfg(test)] |
212 | | -mod tests { |
213 | | - #[test] |
214 | | - fn unnest_tuple() { |
215 | | - let a = unnest_tuple!(1 => 1); |
216 | | - assert_eq!([a], [1]); |
217 | | - let (a, b) = unnest_tuple!(_a, _b => (1, 2)); |
218 | | - assert_eq!([a, b], [1, 2]); |
219 | | - let (a, b, c) = unnest_tuple!(_a, _b, _c => (1, (2, 3))); |
220 | | - assert_eq!([a, b, c], [1, 2, 3]); |
221 | | - let (a, b, c, d) = unnest_tuple!(_a, _b, _c, _d => (1, (2, (3, 4)))); |
222 | | - assert_eq!([a, b, c, d], [1, 2, 3, 4]); |
223 | | - let (a, b, c, d, e) = unnest_tuple!(_a, _b, _c, _d, _e => (1, (2, (3, (4, 5))))); |
224 | | - assert_eq!([a, b, c, d, e], [1, 2, 3, 4, 5]); |
225 | | - let (a, b, c, d, e, f) = |
226 | | - unnest_tuple!(_a, _b, _c, _d, _e, _f => (1, (2, (3, (4, (5, 6)))))); |
227 | | - assert_eq!([a, b, c, d, e, f], [1, 2, 3, 4, 5, 6]); |
228 | | - } |
229 | | - |
230 | | - #[test] |
231 | | - fn join() { |
232 | | - let a = join!(1); |
233 | | - assert_eq!([a], [1]); |
234 | | - let (a, b) = join!(1, 2); |
235 | | - assert_eq!([a, b], [1, 2]); |
236 | | - let (a, b, c) = join!(1, 2, 3); |
237 | | - assert_eq!([a, b, c], [1, 2, 3]); |
238 | | - let (a, b, c, d) = join!(1, 2, 3, 4); |
239 | | - assert_eq!([a, b, c, d], [1, 2, 3, 4]); |
240 | | - let (a, b, c, d, e) = join!(1, 2, 3, 4, 5); |
241 | | - assert_eq!([a, b, c, d, e], [1, 2, 3, 4, 5]); |
242 | | - let (a, b, c, d, e, f) = join!(1, 2, 3, 4, 5, 6); |
243 | | - assert_eq!([a, b, c, d, e, f], [1, 2, 3, 4, 5, 6]); |
244 | | - } |
245 | | -} |
0 commit comments