Skip to content
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

FR: Custom error type in TryInto #315

Closed
mina86 opened this issue Nov 16, 2023 · 3 comments
Closed

FR: Custom error type in TryInto #315

mina86 opened this issue Nov 16, 2023 · 3 comments

Comments

@mina86
Copy link

mina86 commented Nov 16, 2023

I’d like to be able to use a custom error type with TryInto derive.
Ideally it would be possible to explicitly specify error type and
error constructor, for example something like:

#[derive(TryInto)]
#[try_into(error(MyError, MyError::new))]
enum Num { S(i32), U(u32) }

struct MyError { /* ... */ }

impl MyError {
    fn new(error: &str) -> Self { /* ... */ }
}

If constructor is not specified a From conversion would be used.

#170 is a related issue and it could be solved with similar approach
if the error constructor was given two arguments instead of just error
message. The example from that issue could look as follows:

#[derive(TryInto)]
#[try_into(error(Test, Test::from_try_into_failure))]
enum Test {
    A(A),
    B(B),
}

impl Test {
    fn from_try_into_failure(value: Self, _err: &str) -> Self { value }
}
@JelteF
Copy link
Owner

JelteF commented Dec 1, 2023

Could you explain a bit why you want to use your own error type. In the v1.0.0 beta releases a dedicated error type is returned instead of a &'static str. Is that enough for your use case? If not, why not?

@mina86
Copy link
Author

mina86 commented Dec 1, 2023

My use case is with generic types and traits which take a generic
TryInto<Foo> argument expecting error to be convertible to some
specific type.

TryIntoError will probably address this. It would be more convenient
if I could provide my own error type and constructor but at least with
TryIntoError I should be able to create a From conversion to the type
I want.

@JelteF
Copy link
Owner

JelteF commented Dec 18, 2023

Okay, so I thought about your usecase and I think it should be served just fine with a custom From implementation. And I don't think that that is really a lot more work/code than adding a dedicated method to your own type which takes the error string and Self as an argument. Because the From implementation would basically need the same except that these to arguments would be encapsulated in the TryIntoError type. So I'll close this as not planned. If I'm wrong and the amount of code needed to do what you want is actually a lot feel free to re-open and we can re-consider.

@JelteF JelteF closed this as not planned Won't fix, can't repro, duplicate, stale Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants