Skip to content

Commit db7c194

Browse files
committed
Move the extension_trait! accumulator to the end of the rules.
That way, when the `-> impl Future` rules fail (which is most of the time), the cost of reparsing the accumulated tokens is avoided.
1 parent 21fb4ac commit db7c194

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

Diff for: src/utils.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ macro_rules! extension_trait {
281281
#[cfg(feature = "docs")]
282282
#[doc = $doc]
283283
pub trait $name {
284-
extension_trait!(@doc () $($body_base)* $($body_ext)*);
284+
extension_trait!(@doc [$($body_base)* $($body_ext)*] -> []);
285285
}
286286

287287
// When not rendering docs, re-export the base trait from the futures crate.
@@ -291,7 +291,7 @@ macro_rules! extension_trait {
291291
// The extension trait that adds methods to any type implementing the base trait.
292292
#[doc = $doc_ext]
293293
pub trait $ext: $name {
294-
extension_trait!(@ext () $($body_ext)*);
294+
extension_trait!(@ext [$($body_ext)*] -> []);
295295
}
296296

297297
// Blanket implementation of the extension trait for any type implementing the base trait.
@@ -302,24 +302,24 @@ macro_rules! extension_trait {
302302
};
303303

304304
// Parse the return type in an extension method.
305-
(@doc ($($head:tt)*) -> impl Future<Output = $out:ty> $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*) => {
306-
extension_trait!(@doc ($($head)* -> owned::ImplFuture<$out>) $($tail)*);
305+
(@doc [-> impl Future<Output = $out:ty> $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*] -> [$($accum:tt)*]) => {
306+
extension_trait!(@doc [$($tail)*] -> [$($accum)* -> owned::ImplFuture<$out>]);
307307
};
308-
(@ext ($($head:tt)*) -> impl Future<Output = $out:ty> $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*) => {
309-
extension_trait!(@ext ($($head)* -> $f) $($tail)*);
308+
(@ext [-> impl Future<Output = $out:ty> $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*] -> [$($accum:tt)*]) => {
309+
extension_trait!(@ext [$($tail)*] -> [$($accum)* -> $f]);
310310
};
311311

312312
// Parse a token.
313-
(@doc ($($head:tt)*) $token:tt $($tail:tt)*) => {
314-
extension_trait!(@doc ($($head)* $token) $($tail)*);
313+
(@doc [$token:tt $($tail:tt)*] -> [$($accum:tt)*]) => {
314+
extension_trait!(@doc [$($tail)*] -> [$($accum)* $token]);
315315
};
316-
(@ext ($($head:tt)*) $token:tt $($tail:tt)*) => {
317-
extension_trait!(@ext ($($head)* $token) $($tail)*);
316+
(@ext [$token:tt $($tail:tt)*] -> [$($accum:tt)*]) => {
317+
extension_trait!(@ext [$($tail)*] -> [$($accum)* $token]);
318318
};
319319

320320
// Handle the end of the token list.
321-
(@doc ($($head:tt)*)) => { $($head)* };
322-
(@ext ($($head:tt)*)) => { $($head)* };
321+
(@doc [] -> [$($accum:tt)*]) => { $($accum)* };
322+
(@ext [] -> [$($accum:tt)*]) => { $($accum)* };
323323

324324
// Parse imports at the beginning of the macro.
325325
($import:item $($tail:tt)*) => {

0 commit comments

Comments
 (0)