Skip to content

update bench code #7

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
Sep 18, 2019
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
20 changes: 9 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,20 @@ pub fn test(_attr: TokenStream, item: TokenStream) -> TokenStream {
///
/// ```ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test was ignored

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah dang. Yep definitely missed that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, doc tests run with cargo build, so it seems that we can't actually test #[test] and #[bench] via doc tests...

/// #![feature(test)]
///
/// extern crate test;
///
/// use async_std::task;
///
/// #[async_attributes::test]
/// async fn spawn_and_await() {
/// task::spawn(async {
/// #[async_attributes::bench]
/// async fn bench_1(b: &mut test::Bencher) {
/// b.iter(|| {
/// println!("hello world");
/// }).await;
/// })
/// }
/// ```
#[proc_macro_attribute]
pub fn bench(_attr: TokenStream, item: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(item as syn::ItemFn);

let ret = &input.sig.output;
let args = &input.sig.inputs;
let name = &input.sig.ident;
let body = &input.block;
Expand All @@ -179,10 +177,10 @@ pub fn bench(_attr: TokenStream, item: TokenStream) -> TokenStream {
let result = quote! {
#[bench]
#(#attrs)*
fn #name(b: &mut test::Bencher) {
b.iter(|| {
let _ = async_std::task::block_on(async { #body });
});
fn #name(b: &mut test::Bencher) #ret {
task::block_on(task::spawn(async {
#body
}))
}
};

Expand Down