derive_more
derive_more copied to clipboard
Make TryInto::Error return the original value instead of a str
When using the derived TryInto/TryFrom
instances, if the result is an Error
, the original value is consumed (if it is not a reference). This prevents the valid use case of chaining several try_from
or try_into
together, at least not without potentially expensive clones.
Returning a string error message is descriptive if the user program actually prints it, but IMHO that is not a very common use case because reliable programs should always handle errors. An additional reason against string error is that it is not friendly for localization/translation.
I could change the Error
type to be a tuple that contains both the string & the original value if people really must have the string back.
Thanks for this PR, a few initial comments:
- This can only be released as part of 1.0.0, since this is a breaking change.
- Can you add a test that does the chaining?
- The changed doctests do not compile. The easiest is usually to use
cargo expand
to get the right code.
And I'm a bit undecided if I'd like to keep the user friendly error messages too. They are quite helpful in cases of a programming error.
Took me a while, but managed to get it pass all the tests now.
They are quite helpful in cases of a programming error.
So would you rather prefer a tuple (str, T)
as the Error
type? Or other suggestions?
One possibility is to support returning the original value through Err as an optional feature. This will not break backward compatibility.
One possibility is to support returning the original value through Err as an optional feature. This will not break backward compatibility.
I think this is good idea. Could you implement that? I don't think most people will require the original type to be returned. Something like:
-
#[try_into(error_type(string))]
, this is the default -
#[try_into(error_type(input))]
, this would enable your desired behaviour
removing this from the 1.0 milestone as this doesn't need a breaking change
Added back 1.0 milestone, because I think returning an error by default that actually implements Error
would be really good.
Closing this since there was no response from the original author, and I don't think the approach is the right one. let's continue to discuss in #173