derive_more icon indicating copy to clipboard operation
derive_more copied to clipboard

Make TryInto::Error return the original value instead of a str

Open ninegua opened this issue 4 years ago • 6 comments

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.

ninegua avatar Jun 03 '20 06:06 ninegua

Thanks for this PR, a few initial comments:

  1. This can only be released as part of 1.0.0, since this is a breaking change.
  2. Can you add a test that does the chaining?
  3. The changed doctests do not compile. The easiest is usually to use cargo expand to get the right code.

JelteF avatar Jun 03 '20 09:06 JelteF

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.

JelteF avatar Jun 03 '20 09:06 JelteF

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?

ninegua avatar Jun 03 '20 16:06 ninegua

One possibility is to support returning the original value through Err as an optional feature. This will not break backward compatibility.

ninegua avatar Jun 03 '20 16:06 ninegua

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:

  1. #[try_into(error_type(string))], this is the default
  2. #[try_into(error_type(input))], this would enable your desired behaviour

JelteF avatar Jun 16 '20 16:06 JelteF

removing this from the 1.0 milestone as this doesn't need a breaking change

JelteF avatar Jun 10 '22 08:06 JelteF

Added back 1.0 milestone, because I think returning an error by default that actually implements Error would be really good.

JelteF avatar Oct 15 '22 22:10 JelteF

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

JelteF avatar Oct 15 '22 23:10 JelteF