Factor out distinct backtrace logic

This commit is contained in:
David Tolnay 2021-08-28 13:14:31 -07:00
parent 9b542cef8f
commit ed396c2074
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82

View file

@ -14,16 +14,9 @@ impl Struct<'_> {
backtrace_field(&self.fields)
}
// The #[backtrace] field, if it is not the same as the #[from] field.
pub(crate) fn distinct_backtrace_field(&self) -> Option<&Field> {
let backtrace_field = self.backtrace_field()?;
if self.from_field().map_or(false, |from_field| {
from_field.member == backtrace_field.member
}) {
None
} else {
Some(backtrace_field)
}
distinct_backtrace_field(backtrace_field, self.from_field())
}
}
@ -69,13 +62,7 @@ impl Variant<'_> {
pub(crate) fn distinct_backtrace_field(&self) -> Option<&Field> {
let backtrace_field = self.backtrace_field()?;
if self.from_field().map_or(false, |from_field| {
from_field.member == backtrace_field.member
}) {
None
} else {
Some(backtrace_field)
}
distinct_backtrace_field(backtrace_field, self.from_field())
}
}
@ -123,6 +110,20 @@ fn backtrace_field<'a, 'b>(fields: &'a [Field<'b>]) -> Option<&'a Field<'b>> {
None
}
// The #[backtrace] field, if it is not the same as the #[from] field.
fn distinct_backtrace_field<'a, 'b>(
backtrace_field: &'a Field<'b>,
from_field: Option<&Field>,
) -> Option<&'a Field<'b>> {
if from_field.map_or(false, |from_field| {
from_field.member == backtrace_field.member
}) {
None
} else {
Some(backtrace_field)
}
}
fn type_is_backtrace(ty: &Type) -> bool {
let path = match ty {
Type::Path(ty) => &ty.path,