Skip to content

feat: impl Error for TryFromError #519

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dannyhammer
Copy link

This implements the error::Error trait on the TryFromError enum.


The only other error type in uom is ParseQuantityError, which already implements Error. I copied the same structure for implementing Error on this type, modifying the error messages appropriately.

The reason I am proposing this change is because I recently found myself wanting to write a function like this:

fn uom_time_str_to_std_duration(s: &str) -> anyhow::Result<std::time::Duration> {
    let time: uom::si::f64::Time = s.parse()?;
    let duration = time.try_into()?;
    Ok(duration)
}

However, this function does not compile, and results in the following error:

error[E0277]: the trait bound `TryFromError: std::error::Error` is not satisfied
  --> src/main.rs:15:35
   |
15 |     let duration = time.try_into()?;
   |                                   ^ the trait `std::error::Error` is not implemented for `TryFromError`, which is required by `Result<Duration, anyhow::Error>: FromResidual<Result<Infallible, _>>`
   |
   = help: the trait `FromResidual<Result<Infallible, E>>` is implemented for `Result<T, F>`
   = note: required for `anyhow::Error` to implement `From<TryFromError>`
   = note: required for `Result<Duration, anyhow::Error>` to implement `FromResidual<Result<Infallible, TryFromError>>`

For more information about this error, try `rustc --explain E0277`.

This PR resolves this issue.


This is my first contribution to uom, so if I have missed anything important, please let me know.

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

Successfully merging this pull request may close these issues.

1 participant