-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e5c3ad
commit db70182
Showing
17 changed files
with
106 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/target | ||
/join_futures/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "join_futures" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[profile.dev] | ||
opt-level = 3 | ||
|
||
[lib] | ||
proc-macro = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use proc_macro::{TokenStream, TokenTree}; | ||
|
||
#[proc_macro] | ||
pub fn join_futures(input: TokenStream) -> TokenStream { | ||
let mut tokens = input.into_iter(); | ||
|
||
let variable = match tokens.find(|t| matches!(t, TokenTree::Ident(_))) { | ||
Some(TokenTree::Ident(variable)) => variable.to_string(), | ||
_ => panic!("You must provide a variable containing futures to join"), | ||
}; | ||
let flatten_amount = match tokens.find(|t| matches!(t, TokenTree::Literal(_))) { | ||
Some(TokenTree::Literal(flatten_amount)) => flatten_amount.to_string().trim().parse::<usize>().unwrap(), | ||
_ => 0, | ||
}; | ||
|
||
let tokens = tokens.skip_while(|t| matches!(t, TokenTree::Punct(_))); | ||
let value_type = match tokens.map(|t| t.to_string()).collect::<String>() { | ||
s if s.is_empty() => "Vec<Config>".to_string(), | ||
s => s, | ||
}; | ||
|
||
let mut value = format!("futures::future::join_all({variable}).await"); | ||
if flatten_amount > 0 { | ||
value.push_str(&format!( | ||
".into_iter(){}.collect::<{value_type}>()", | ||
".flatten()".repeat(flatten_amount), | ||
)); | ||
} | ||
value.parse().unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.