-
Notifications
You must be signed in to change notification settings - Fork 742
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
attributes: implement #[instrument(ret)]
#1716
Changes from all commits
eccd0ea
f7cd4c4
16e8bf1
d3a0a21
d049d80
a12814d
f513f4a
9cc53b1
47fc691
0382e4a
17b0ca5
4798f64
255170c
887e764
55209a4
e26c10f
8973a9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,8 @@ pub(crate) struct InstrumentArgs { | |
target: Option<LitStr>, | ||
pub(crate) skips: HashSet<Ident>, | ||
pub(crate) fields: Option<Fields>, | ||
pub(crate) err_mode: Option<ErrorMode>, | ||
pub(crate) err_mode: Option<FormatMode>, | ||
pub(crate) ret_mode: Option<FormatMode>, | ||
/// Errors describing any unrecognized parse inputs that we skipped. | ||
parse_warnings: Vec<syn::Error>, | ||
} | ||
|
@@ -139,8 +140,12 @@ impl Parse for InstrumentArgs { | |
args.fields = Some(input.parse()?); | ||
} else if lookahead.peek(kw::err) { | ||
let _ = input.parse::<kw::err>(); | ||
let mode = ErrorMode::parse(input)?; | ||
let mode = FormatMode::parse(input)?; | ||
args.err_mode = Some(mode); | ||
} else if lookahead.peek(kw::ret) { | ||
let _ = input.parse::<kw::ret>()?; | ||
let mode = FormatMode::parse(input)?; | ||
args.ret_mode = Some(mode); | ||
} else if lookahead.peek(Token![,]) { | ||
let _ = input.parse::<Token![,]>()?; | ||
} else { | ||
|
@@ -199,29 +204,30 @@ impl Parse for Skips { | |
} | ||
|
||
#[derive(Clone, Debug, Hash, PartialEq, Eq)] | ||
pub(crate) enum ErrorMode { | ||
pub(crate) enum FormatMode { | ||
Default, | ||
Display, | ||
Debug, | ||
} | ||
|
||
impl Default for ErrorMode { | ||
impl Default for FormatMode { | ||
fn default() -> Self { | ||
ErrorMode::Display | ||
FormatMode::Default | ||
} | ||
} | ||
|
||
impl Parse for ErrorMode { | ||
impl Parse for FormatMode { | ||
fn parse(input: ParseStream<'_>) -> syn::Result<Self> { | ||
if !input.peek(syn::token::Paren) { | ||
return Ok(ErrorMode::default()); | ||
return Ok(FormatMode::default()); | ||
} | ||
let content; | ||
let _ = syn::parenthesized!(content in input); | ||
let maybe_mode: Option<Ident> = content.parse()?; | ||
maybe_mode.map_or(Ok(ErrorMode::default()), |ident| { | ||
maybe_mode.map_or(Ok(FormatMode::default()), |ident| { | ||
match ident.to_string().as_str() { | ||
"Debug" => Ok(ErrorMode::Debug), | ||
"Display" => Ok(ErrorMode::Display), | ||
"Debug" => Ok(FormatMode::Debug), | ||
"Display" => Ok(FormatMode::Display), | ||
_ => Err(syn::Error::new( | ||
ident.span(), | ||
"unknown error mode, must be Debug or Display", | ||
|
@@ -356,4 +362,5 @@ mod kw { | |
syn::custom_keyword!(target); | ||
syn::custom_keyword!(name); | ||
syn::custom_keyword!(err); | ||
syn::custom_keyword!(ret); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. personally, my preference would be to use also, the field name is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, that's a good point; I hadn't considered that... |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think
Display
should be the default for return values, personally. Most return values will likely not implementDisplay
---Display
was the default for errors simply because most errors implement thestd::error::Error
trait, which requires them to beDisplay
. If we're always going to format return values, we should probably default to usingDebug
, and makeDisplay
be the one that the user opts in to.With that said, I think we may actually want to change so that the default behavior is to record the return value as a
tracing
primitive value if it's one of the primitiveValue
types, and fall back toDisplay
if it's not. We can look at the function signature's return type, and we already have code for determining whether a type is a primitiveValue
:tracing/tracing-attributes/src/expand.rs
Lines 345 to 398 in f513f4a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we are special-casing
Result<T, E>
s, in order to do fallback'ing we need to extractT
from theResult
ofsyn::Type
being returned, which I can find the way to do it. Therefore I'd go with the former change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that's fair; I think it should be possible to find
Result
s and get the type parameter usingsyn
, but it might be fairly complex. We can always add that in a future change.