From 21fb4ac0fb8341f490b094253d4c3649623d07e2 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 10 Mar 2022 17:09:33 +1100 Subject: [PATCH 1/2] Remove two useless rules from `extension_trait!`. They never run because they are subsumed by the two rules immediately above. --- src/utils.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index d8052444..a54d2525 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -309,14 +309,6 @@ macro_rules! extension_trait { extension_trait!(@ext ($($head)* -> $f) $($tail)*); }; - // Parse the return type in an extension method. - (@doc ($($head:tt)*) -> impl Future + $lt:lifetime [$f:ty] $($tail:tt)*) => { - extension_trait!(@doc ($($head)* -> borrowed::ImplFuture<$lt, $out>) $($tail)*); - }; - (@ext ($($head:tt)*) -> impl Future + $lt:lifetime [$f:ty] $($tail:tt)*) => { - extension_trait!(@ext ($($head)* -> $f) $($tail)*); - }; - // Parse a token. (@doc ($($head:tt)*) $token:tt $($tail:tt)*) => { extension_trait!(@doc ($($head)* $token) $($tail)*); From db7c1946c89e119ea37966803735cdcc0601058e Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 10 Mar 2022 17:10:12 +1100 Subject: [PATCH 2/2] 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. --- src/utils.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index a54d2525..6ae49115 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -281,7 +281,7 @@ macro_rules! extension_trait { #[cfg(feature = "docs")] #[doc = $doc] pub trait $name { - extension_trait!(@doc () $($body_base)* $($body_ext)*); + extension_trait!(@doc [$($body_base)* $($body_ext)*] -> []); } // When not rendering docs, re-export the base trait from the futures crate. @@ -291,7 +291,7 @@ macro_rules! extension_trait { // The extension trait that adds methods to any type implementing the base trait. #[doc = $doc_ext] pub trait $ext: $name { - extension_trait!(@ext () $($body_ext)*); + extension_trait!(@ext [$($body_ext)*] -> []); } // Blanket implementation of the extension trait for any type implementing the base trait. @@ -302,24 +302,24 @@ macro_rules! extension_trait { }; // Parse the return type in an extension method. - (@doc ($($head:tt)*) -> impl Future $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*) => { - extension_trait!(@doc ($($head)* -> owned::ImplFuture<$out>) $($tail)*); + (@doc [-> impl Future $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*] -> [$($accum:tt)*]) => { + extension_trait!(@doc [$($tail)*] -> [$($accum)* -> owned::ImplFuture<$out>]); }; - (@ext ($($head:tt)*) -> impl Future $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*) => { - extension_trait!(@ext ($($head)* -> $f) $($tail)*); + (@ext [-> impl Future $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*] -> [$($accum:tt)*]) => { + extension_trait!(@ext [$($tail)*] -> [$($accum)* -> $f]); }; // Parse a token. - (@doc ($($head:tt)*) $token:tt $($tail:tt)*) => { - extension_trait!(@doc ($($head)* $token) $($tail)*); + (@doc [$token:tt $($tail:tt)*] -> [$($accum:tt)*]) => { + extension_trait!(@doc [$($tail)*] -> [$($accum)* $token]); }; - (@ext ($($head:tt)*) $token:tt $($tail:tt)*) => { - extension_trait!(@ext ($($head)* $token) $($tail)*); + (@ext [$token:tt $($tail:tt)*] -> [$($accum:tt)*]) => { + extension_trait!(@ext [$($tail)*] -> [$($accum)* $token]); }; // Handle the end of the token list. - (@doc ($($head:tt)*)) => { $($head)* }; - (@ext ($($head:tt)*)) => { $($head)* }; + (@doc [] -> [$($accum:tt)*]) => { $($accum)* }; + (@ext [] -> [$($accum:tt)*]) => { $($accum)* }; // Parse imports at the beginning of the macro. ($import:item $($tail:tt)*) => {