-
Notifications
You must be signed in to change notification settings - Fork 210
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
allow deriving of FromVariant
for uninhabitable enums
#962
allow deriving of FromVariant
for uninhabitable enums
#962
Conversation
Thanks a lot for this PR! 👍 [Edit]: discussion about usefulness is starting from here. bors try |
tryBuild succeeded: |
Adds the `#[variant(enum = "repr")]` and `#[variant(enum = "str")]` item-level attribute to the Variant conversion derive macros for fieldless enums. - `#[variant(enum = "repr")]` - convert through the numeric type specified using `#[repr]`. - `#[variant(enum = "str")]` - convert through string representations of the variant names. WIP - do not merge! FromVariant implementations would conflict with godot-rust#962. Close godot-rust#546.
Adds the `#[variant(enum = "repr")]` and `#[variant(enum = "str")]` item-level attribute to the Variant conversion derive macros for fieldless enums. - `#[variant(enum = "repr")]` - convert through the numeric type specified using `#[repr]`. - `#[variant(enum = "str")]` - convert through string representations of the variant names. WIP - do not merge! FromVariant implementations would conflict with godot-rust#962. Close godot-rust#546.
Adds the `#[variant(enum = "repr")]` and `#[variant(enum = "str")]` item-level attribute to the Variant conversion derive macros for fieldless enums. - `#[variant(enum = "repr")]` - convert through the numeric type specified using `#[repr]`. - `#[variant(enum = "str")]` - convert through string representations of the variant names. WIP - do not merge! FromVariant implementations would conflict with godot-rust#962. Close godot-rust#546.
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.
Just one minor comment.
I think FromVariantError::UnknownEnumVariant
might be good for now? We can still change this post-PR.
{ | ||
let __dict = ::gdnative::core_types::Dictionary::from_variant(#input_ident) | ||
.map_err(|__err| FVE::InvalidEnumRepr { | ||
expected: VariantEnumRepr::ExternallyTagged, | ||
error: std::boxed::Box::new(__err), | ||
})?; | ||
|
||
let __keys = __dict.keys(); | ||
if __keys.len() != 1 { | ||
Err(FVE::InvalidEnumRepr { | ||
expected: VariantEnumRepr::ExternallyTagged, | ||
error: std::boxed::Box::new(FVE::InvalidLength { | ||
expected: 1, | ||
len: __keys.len() as usize, | ||
}), | ||
}) | ||
} | ||
else { | ||
let __key = String::from_variant(&__keys.get(0)) | ||
.map_err(|__err| FVE::InvalidEnumRepr { | ||
expected: VariantEnumRepr::ExternallyTagged, | ||
error: std::boxed::Box::new(__err), | ||
})?; | ||
match __key.as_str() { | ||
#( | ||
#ref_var_ident_string_literals => { | ||
let #var_input_ident_iter = &__dict.get_or_nil(&__keys.get(0)); | ||
(#var_from_variants).map_err(|err| FVE::InvalidEnumVariant { | ||
variant: #ref_var_ident_string_literals, | ||
error: std::boxed::Box::new(err), | ||
}) | ||
}, | ||
)* | ||
variant => Err(FVE::UnknownEnumVariant { | ||
variant: variant.to_string(), | ||
expected: &[#(#ref_var_ident_string_literals),*], | ||
}), | ||
} | ||
} |
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.
This {}
block is no longer needed?
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.
Yeah, it's almost identical to L68 ~ L105. (expected the early_return
statement)
There are so many lines of change due to indentation. The outermost {}
pair is removed because return_expr
is the only expression used inside generated from_variant
implementation. Should I keep it?
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 see, thanks. No it's fine, you can leave your changes like this 🙂
FromVariant
for uninhabitable enumsFromVariant
for uninhabitable enums
Thanks! 🚀 bors r+ |
Build succeeded: |
Fixs #444
What type of error should be returned might need further discussion.