Skip to content

Commit a113fde

Browse files
committed
don't re-parse ItemFn in MaybeItemFn conversion
1 parent 3dbb7d6 commit a113fde

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

tracing-attributes/src/expand.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::convert::TryInto;
21
use std::iter;
32

43
use proc_macro2::TokenStream;
@@ -682,7 +681,7 @@ impl<'block> AsyncInfo<'block> {
682681
out_stmts[iter] = match self.kind {
683682
// `Box::pin(immediately_invoked_async_fn())`
684683
AsyncKind::Function(fun) => {
685-
let fun: MaybeItemFn = fun.clone().try_into()?;
684+
let fun = MaybeItemFn::from(fun.clone());
686685
gen_function(
687686
fun.as_ref(),
688687
args,

tracing-attributes/src/lib.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@
8080
while_true
8181
)]
8282

83-
use std::convert::{TryFrom, TryInto};
84-
8583
use proc_macro2::TokenStream;
8684
use quote::ToTokens;
8785
use syn::parse::{Parse, ParseStream};
@@ -393,7 +391,7 @@ fn instrument_precise(
393391
return async_like.gen_async(args, instrumented_function_name.as_str());
394392
}
395393

396-
let input: MaybeItemFn = input.try_into()?;
394+
let input = MaybeItemFn::from(input);
397395

398396
Ok(expand::gen_function(
399397
input.as_ref(),
@@ -446,6 +444,28 @@ impl Parse for MaybeItemFn {
446444
}
447445
}
448446

447+
impl From<ItemFn> for MaybeItemFn {
448+
fn from(
449+
ItemFn {
450+
attrs,
451+
vis,
452+
sig,
453+
block,
454+
}: ItemFn,
455+
) -> Self {
456+
let (outer_attrs, inner_attrs) = attrs
457+
.into_iter()
458+
.partition(|attr| attr.style == syn::AttrStyle::Outer);
459+
Self {
460+
outer_attrs,
461+
inner_attrs,
462+
vis,
463+
sig,
464+
block: block.to_token_stream(),
465+
}
466+
}
467+
}
468+
449469
/// A generic reference type for `MaybeItemFn`,
450470
/// that takes a generic block type `B` that implements `ToTokens` (eg. `TokenStream`, `Block`).
451471
#[derive(Debug, Clone)]
@@ -456,11 +476,3 @@ struct MaybeItemFnRef<'a, B: ToTokens> {
456476
sig: &'a Signature,
457477
block: &'a B,
458478
}
459-
460-
impl TryFrom<ItemFn> for MaybeItemFn {
461-
type Error = syn::Error;
462-
463-
fn try_from(value: ItemFn) -> Result<Self, Self::Error> {
464-
syn::parse2(value.into_token_stream())
465-
}
466-
}

0 commit comments

Comments
 (0)