Skip to content

Commit

Permalink
Disambiguate Error type in TryFrom (#29)
Browse files Browse the repository at this point in the history
* disambiguate Error type in TryFrom

* add testcase for enum with error variant
  • Loading branch information
cmrschwarz authored Mar 12, 2024
1 parent 93d83cd commit bd8c572
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl Enum {
impl #parent_impl core::convert::TryFrom<#parent_ident #parent_ty> for #child_ident #child_ty #parent_where {
type Error = #error;

fn try_from(parent: #parent_ident #parent_ty) -> Result<Self, Self::Error> {
fn try_from(parent: #parent_ident #parent_ty) -> Result<Self, <Self as core::convert::TryFrom<#parent_ident #parent_ty>>::Error> {
match parent {
#(#try_from_parent_arms),*,
_ => Err(#error)
Expand Down
15 changes: 15 additions & 0 deletions tests/it.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,18 @@ enum Whew<'a: 'b, 'b, 'c, T, U> {
#[subenum(Phew)]
B(&'b [&'c [U; 7]]),
}

#[subenum(SubEnumWithErrorVariant)]
#[derive(Debug, Clone, Copy, PartialEq)]
enum EnumWithErrorVariant {
#[subenum(SubEnumWithErrorVariant)]
Error,
}

#[test]
fn test_enum_with_error_variant() {
let a = EnumWithErrorVariant::Error;
let b = SubEnumWithErrorVariant::try_from(a).unwrap();

assert_eq!(a, b);
}

0 comments on commit bd8c572

Please sign in to comment.