-
Notifications
You must be signed in to change notification settings - Fork 35
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
Support custom error type for TryFromPrimitive #121
Comments
I really like this idea, and in particular I think it will also help solve #82 (which ideally would be solved by use of something like https://crates.io/crates/transitive rather than with num_enum-specific support)... I'll have a play with what the API could look like for this - the place I'm not 100% sure of is on how to take the |
I put together a draft in #123 - can you give it a go and see how it works for you? You can see an example of the syntax in the newly added tests in num_enum/tests/try_from_primitive.rs: #[derive(TryFromPrimitive)]
#[num_enum(error_type(name = CustomError, constructor = CustomError::new))]
#[repr(u8)]
enum FirstNumber {
Zero,
One,
Two,
} |
I just had a go with this in my code and it works well, thanks! Looking forward to seeing a release with this in it. |
Fabulous! I've just published 0.7.0 with this feature :) Thanks again for the feature request, and testing it out! |
It would be useful for the
TryFromPrimitive
derive macro to have an option to specify a custom error type to use when the conversion fails, rather than the suppliedTryFromPrimitiveError
. I'd like to useTryFromPrimitive
on some types which are part of my library's public API, and exposingTryFromPrimitiveError
as part of this isn't great. It could look something like:Where
MyError
is the error type to use for theTryFrom
implementation, anderror_constructor
is a function of type(u8) -> MyError
which the generatedTryFrom
implementation would call with the invalid input value to make an instance of the error type.The text was updated successfully, but these errors were encountered: