Skip to content

Commit

Permalink
Merge pull request #39 from palantir/fix-async-spanned
Browse files Browse the repository at this point in the history
Fix send-ness of spanned async functions
  • Loading branch information
sfackler authored May 7, 2021
2 parents 0ebdbf8 + b9e6ce8 commit 2fc7b84
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion zipkin-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zipkin-macros"
version = "0.1.0"
version = "0.1.1"
authors = ["Steven Fackler <sfackler@palantir.com>"]
edition = "2018"
license = "Apache-2.0"
Expand Down
20 changes: 13 additions & 7 deletions zipkin-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,20 @@ fn spanned_impl(args: AttributeArgs, mut func: ImplItemMethod) -> Result<TokenSt

if func.sig.asyncness.is_some() {
let stmts = &func.block.stmts;
let stmt = quote! {
zipkin::next_span()
func.block.stmts = vec![
syn::parse2(quote! {
let __macro_impl_span = zipkin::next_span()
.with_name(#name)
.detach()
.bind(async move { #(#stmts)* })
.await
};
func.block.stmts = vec![Stmt::Expr(syn::parse2(stmt).unwrap())];
.detach();
})
.unwrap(),
Stmt::Expr(
syn::parse2(quote! {
__macro_impl_span.bind(async move { #(#stmts)* }).await
})
.unwrap(),
),
];
} else {
let stmt = quote! {
let __macro_impl_span = zipkin::next_span().with_name(#name);
Expand Down
12 changes: 12 additions & 0 deletions zipkin/src/test/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ use crate as zipkin; // hack to get the macro codegen to work in the same crate
use crate::{spanned, test};
use futures::executor;

fn is_send<T>(_: T)
where
T: Send,
{
}

#[test]
fn blocking_free_function() {
#[spanned(name = "foobar")]
Expand Down Expand Up @@ -105,6 +111,8 @@ fn async_free_function() {
zipkin::next_span().with_name("fizzbuzz");
}

is_send(foo());

test::init();

let future = zipkin::next_span().with_name("root").detach().bind(foo());
Expand Down Expand Up @@ -133,6 +141,8 @@ fn async_associated_function() {
}
}

is_send(Foo::foo());

test::init();

let future = zipkin::next_span()
Expand Down Expand Up @@ -164,6 +174,8 @@ fn async_method() {
}
}

is_send(Foo.foo());

test::init();

let future = zipkin::next_span()
Expand Down

0 comments on commit 2fc7b84

Please sign in to comment.