Skip to content

Commit

Permalink
Extract combined backtrace-source case to separate match arm
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Aug 28, 2021
1 parent d49c5af commit 799bb53
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions impl/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,36 +240,38 @@ fn impl_enum(input: Enum) -> TokenStream {
}
}
}
(Some(backtrace_field), _) => {
(Some(backtrace_field), Some(source_field))
if backtrace_field.member == source_field.member =>
{
let backtrace = &backtrace_field.member;
if variant.source_field().map_or(false, |f| f.member == *backtrace) {
let varsource = quote!(source);
let source_backtrace = if type_is_option(backtrace_field.ty) {
quote_spanned! {backtrace.span()=>
#varsource.as_ref().and_then(|source| source.as_dyn_error().backtrace())
}
} else {
quote_spanned! {backtrace.span()=>
#varsource.as_dyn_error().backtrace()
}
};
quote! {
#ty::#ident {#backtrace: #varsource, ..} => {
use thiserror::private::AsDynError;
#source_backtrace
}
let varsource = quote!(source);
let source_backtrace = if type_is_option(source_field.ty) {
quote_spanned! {backtrace.span()=>
#varsource.as_ref().and_then(|source| source.as_dyn_error().backtrace())
}
} else {
let body = if type_is_option(backtrace_field.ty) {
quote!(backtrace.as_ref())
} else {
quote!(std::option::Option::Some(backtrace))
};
quote! {
#ty::#ident {#backtrace: backtrace, ..} => #body,
quote_spanned! {backtrace.span()=>
#varsource.as_dyn_error().backtrace()
}
};
quote! {
#ty::#ident {#backtrace: #varsource, ..} => {
use thiserror::private::AsDynError;
#source_backtrace
}
}
}
(Some(backtrace_field), _) => {
let backtrace = &backtrace_field.member;
let body = if type_is_option(backtrace_field.ty) {
quote!(backtrace.as_ref())
} else {
quote!(std::option::Option::Some(backtrace))
};
quote! {
#ty::#ident {#backtrace: backtrace, ..} => #body,
}
}
(None, _) => quote! {
#ty::#ident {..} => std::option::Option::None,
},
Expand Down

0 comments on commit 799bb53

Please sign in to comment.