Skip to content

Improve extension_trait! #1005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -302,32 +302,24 @@ macro_rules! extension_trait {
};

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

// Parse the return type in an extension method.
(@doc ($($head:tt)*) -> impl Future<Output = $out:ty> + $lt:lifetime [$f:ty] $($tail:tt)*) => {
extension_trait!(@doc ($($head)* -> borrowed::ImplFuture<$lt, $out>) $($tail)*);
};
(@ext ($($head:tt)*) -> impl Future<Output = $out:ty> + $lt:lifetime [$f:ty] $($tail:tt)*) => {
extension_trait!(@ext ($($head)* -> $f) $($tail)*);
(@ext [-> impl Future<Output = $out:ty> $(+ $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)*) => {
Expand Down