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

Use dedicated error type for TryInto errors #220

Merged
merged 15 commits into from
Nov 15, 2022
Merged

Conversation

JelteF
Copy link
Owner

@JelteF JelteF commented Nov 14, 2022

Instead of returning a string as the error, this returns a dedicated
error type. This has the big benefit that in case of an error it's
possible to get back the original value by using error.input.
Previously the original value would be dropped in case of an error.

Fixes #173
Fixes #130

Instead of returning a string as the error, this returns a dedicated
error type. This has the big benefit that in case of an error it's
possible to get back the original value by using `error.input`.
Previously the original value would be dropped in case of an error.

Fixes #173
Fixes #130
@JelteF JelteF force-pushed the error-type-for-try-into branch from 53df546 to 27c50fe Compare November 14, 2022 15:54
src/errors.rs Outdated
@@ -0,0 +1,32 @@
use core::fmt;

#[derive(Debug)]
Copy link
Owner Author

@JelteF JelteF Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open question 1: Do we to derive Debug? Maybe not, because it requires all types for which TryFrom is derived to also have a Debug implementation. Can we make this conditionally derive Debug only when T implements Debug itself?

Open question 2: Do we want to implement the Error trait for this error type? This doesn't work for no_std currently, but we could make std an optional default enabled feature.

Edit: Actually it's probably fine to require a Debug implementation, if people complain about it we can reconsider. Having the error type implement Debug is also a prerequisite for implementing the Error trait.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JelteF

Open question 1: Do we to derive Debug?

Yes. We do want to derive Debug (and Clone too), because it's so unpleasant situation when third-party lib doesn't provide a Debug impl for a type, and you should squat-around it with wrappers in case you just want to put dbg!(), and I hit it so many times 🤬

Maybe not, because it requires all types for which TryFrom is derived to also have a Debug implementation. Can we make this conditionally derive Debug only when T implements Debug itself?

Yup. Event standard #[derive(Debug)] will generate a proper impl<T: Debug> Debug for MyError<T> in this situation. We don't need to do something additional here.

Open question 2: Do we want to implement the Error trait for this error type? This doesn't work for no_std currently, but we could make std an optional default enabled feature.

Yes. Definitely.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added deriving Clone and Copy in a6e68df. These are conditional in the same way as #[derive(Debug)].

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Event standard #[derive(Debug)] will generate a proper impl<T: Debug> Debug for MyError in this situation. We don't need to do something additional here.

Yes of course, it seems I was misreading the following error message when removing Debug from MixedData:

error[E0277]: `MixedData` doesn't implement `Debug`
    --> src/lib.rs:250:42
     |
14   | assert_eq!(123u32, mixed_int1.try_into().unwrap());
     |                                          ^^^^^^ `MixedData` cannot be formatted using `{:?}`
     |
     = help: the trait `Debug` is not implemented for `MixedData`
     = note: add `#[derive(Debug)]` to `MixedData` or manually `impl Debug for MixedData`
     = help: the trait `Debug` is implemented for `TryIntoError<T>`
     = note: required because of the requirements on the impl of `Debug` for `TryIntoError<MixedData>`

the reason it's complaining here is because unwrap requires an E that implements Debug

@JelteF JelteF requested a review from tyranron November 14, 2022 16:16
src/errors.rs Outdated
}

impl<T> TryIntoError<T> {
pub fn new(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be const.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 55a4268.

use core::fmt;

#[derive(Debug)]
pub struct TryIntoError<T> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to add docs for this error type as it's part of public API now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in ae3d5c0.

@@ -32,3 +32,6 @@

#[doc(inline)]
pub use derive_more_impl::*;

mod errors;
pub use crate::errors::TryIntoError;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think this should be feature-gated by "try_into".

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 77043d1.

@tyranron tyranron added this to the 1.0.0 milestone Nov 14, 2022
Copy link
Collaborator

@tyranron tyranron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@@ -75,6 +75,25 @@ jobs:

- run: cargo test --workspace --features testing-helpers

no_std:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JelteF I've added this CI check, so we can track possible no_std regressions on CI.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm supprised it works, because this issue looks like it would impact this crate: hobofan/cargo-nono#47

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JelteF the reason is explained:

there is just no logic in place yet to track and evaluate compiler switches on individual use items (as evaluating arbitrary compiler switches was a rabbit hole I didn't want to go into).

We just have no compiler switches on use statements at the moment. So, we're lucky.

@tyranron tyranron enabled auto-merge (squash) November 15, 2022 10:39
@tyranron tyranron merged commit d1fe839 into master Nov 15, 2022
@tyranron tyranron deleted the error-type-for-try-into branch November 15, 2022 10:39
@tyranron tyranron linked an issue Dec 1, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make TryInto use better error type TryInto with input type as error
2 participants