Skip to content

Commit

Permalink
bind a non-Copy value
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakuyume committed Jan 10, 2024
1 parent 9e65a3d commit 1d72343
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions tracing-attributes/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,27 @@ fn gen_block<B: ToTokens>(
}
);

// Bind a non-Copy value to make the closure FnOnce
// (https://github.com/tokio-rs/tracing/issues/2796).
let fn_once_init = quote!(
let __tracing_attr_fn_once = {
struct TracingAttrFnOnce;
TracingAttrFnOnce
};
);
let fn_once_bind = quote!(
let __tracing_attr_fn_once = __tracing_attr_fn_once;
);

match (err_event, ret_event) {
(Some(err_event), Some(ret_event)) => quote_spanned! {block.span()=>
#span
#fn_once_init
#[allow(clippy::redundant_closure_call)]
match (move || #block)() {
match (move || {
#fn_once_bind
#block
})() {
#[allow(clippy::unit_arg)]
Ok(x) => {
#ret_event;
Expand All @@ -368,8 +384,12 @@ fn gen_block<B: ToTokens>(
},
(Some(err_event), None) => quote_spanned!(block.span()=>
#span
#fn_once_init
#[allow(clippy::redundant_closure_call)]
match (move || #block)() {
match (move || {
#fn_once_bind
#block
})() {
#[allow(clippy::unit_arg)]
Ok(x) => Ok(x),
Err(e) => {
Expand All @@ -380,8 +400,12 @@ fn gen_block<B: ToTokens>(
),
(None, Some(ret_event)) => quote_spanned!(block.span()=>
#span
#fn_once_init
#[allow(clippy::redundant_closure_call)]
let x = (move || #block)();
let x = (move || {
#fn_once_bind
#block
})();
#ret_event;
x
),
Expand Down

0 comments on commit 1d72343

Please sign in to comment.