Skip to content

Commit

Permalink
Merge pull request #184 from dtolnay/provide
Browse files Browse the repository at this point in the history
Directly call source.provide instead of going through dyn error
  • Loading branch information
dtolnay authored Sep 4, 2022
2 parents 2f093b5 + f924c25 commit 76c5568
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions impl/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ fn impl_struct(input: Struct) -> TokenStream {
let source_provide = if type_is_option(source_field.ty) {
quote_spanned! {source.span()=>
if let std::option::Option::Some(source) = &self.#source {
source.as_dyn_error().provide(#demand);
source.provide(#demand);
}
}
} else {
quote_spanned! {source.span()=>
self.#source.as_dyn_error().provide(#demand);
self.#source.provide(#demand);
}
};
let self_provide = if source == backtrace {
Expand All @@ -89,7 +89,8 @@ fn impl_struct(input: Struct) -> TokenStream {
})
};
quote! {
use thiserror::__private::AsDynError;
#[allow(unused_imports)]
use std::error::Error as _;
#source_provide
#self_provide
}
Expand Down Expand Up @@ -259,12 +260,12 @@ fn impl_enum(input: Enum) -> TokenStream {
let source_provide = if type_is_option(source_field.ty) {
quote_spanned! {source.span()=>
if let std::option::Option::Some(source) = #varsource {
source.as_dyn_error().provide(#demand);
source.provide(#demand);
}
}
} else {
quote_spanned! {source.span()=>
#varsource.as_dyn_error().provide(#demand);
#varsource.provide(#demand);
}
};
let self_provide = if type_is_option(backtrace_field.ty) {
Expand All @@ -284,7 +285,8 @@ fn impl_enum(input: Enum) -> TokenStream {
#source: #varsource,
..
} => {
use thiserror::__private::AsDynError;
#[allow(unused_imports)]
use std::error::Error as _;
#source_provide
#self_provide
}
Expand All @@ -298,17 +300,18 @@ fn impl_enum(input: Enum) -> TokenStream {
let source_provide = if type_is_option(source_field.ty) {
quote_spanned! {backtrace.span()=>
if let std::option::Option::Some(source) = #varsource {
source.as_dyn_error().provide(#demand);
source.provide(#demand);
}
}
} else {
quote_spanned! {backtrace.span()=>
#varsource.as_dyn_error().provide(#demand);
#varsource.provide(#demand);
}
};
quote! {
#ty::#ident {#backtrace: #varsource, ..} => {
use thiserror::__private::AsDynError;
#[allow(unused_imports)]
use std::error::Error as _;
#source_provide
}
}
Expand Down

0 comments on commit 76c5568

Please sign in to comment.