2
0
Fork 1
mirror of https://github.com/async-rs/async-std.git synced 2025-04-26 10:16:49 +00:00

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.
This commit is contained in:
Nicholas Nethercote 2022-03-10 17:10:12 +11:00
parent 21fb4ac0fb
commit db7c1946c8

View file

@ -281,7 +281,7 @@ macro_rules! extension_trait {
#[cfg(feature = "docs")] #[cfg(feature = "docs")]
#[doc = $doc] #[doc = $doc]
pub trait $name { 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. // 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. // The extension trait that adds methods to any type implementing the base trait.
#[doc = $doc_ext] #[doc = $doc_ext]
pub trait $ext: $name { 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. // 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. // Parse the return type in an extension method.
(@doc ($($head:tt)*) -> impl Future<Output = $out:ty> $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*) => { (@doc [-> impl Future<Output = $out:ty> $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*] -> [$($accum:tt)*]) => {
extension_trait!(@doc ($($head)* -> owned::ImplFuture<$out>) $($tail)*); extension_trait!(@doc [$($tail)*] -> [$($accum)* -> owned::ImplFuture<$out>]);
}; };
(@ext ($($head:tt)*) -> impl Future<Output = $out:ty> $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*) => { (@ext [-> impl Future<Output = $out:ty> $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*] -> [$($accum:tt)*]) => {
extension_trait!(@ext ($($head)* -> $f) $($tail)*); extension_trait!(@ext [$($tail)*] -> [$($accum)* -> $f]);
}; };
// Parse a token. // Parse a token.
(@doc ($($head:tt)*) $token:tt $($tail:tt)*) => { (@doc [$token:tt $($tail:tt)*] -> [$($accum:tt)*]) => {
extension_trait!(@doc ($($head)* $token) $($tail)*); extension_trait!(@doc [$($tail)*] -> [$($accum)* $token]);
}; };
(@ext ($($head:tt)*) $token:tt $($tail:tt)*) => { (@ext [$token:tt $($tail:tt)*] -> [$($accum:tt)*]) => {
extension_trait!(@ext ($($head)* $token) $($tail)*); extension_trait!(@ext [$($tail)*] -> [$($accum)* $token]);
}; };
// Handle the end of the token list. // Handle the end of the token list.
(@doc ($($head:tt)*)) => { $($head)* }; (@doc [] -> [$($accum:tt)*]) => { $($accum)* };
(@ext ($($head:tt)*)) => { $($head)* }; (@ext [] -> [$($accum:tt)*]) => { $($accum)* };
// Parse imports at the beginning of the macro. // Parse imports at the beginning of the macro.
($import:item $($tail:tt)*) => { ($import:item $($tail:tt)*) => {