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

Consider adding anyhow::Ok #192

Closed
dtolnay opened this issue Nov 22, 2021 · 0 comments
Closed

Consider adding anyhow::Ok #192

dtolnay opened this issue Nov 22, 2021 · 0 comments

Comments

@dtolnay
Copy link
Owner

dtolnay commented Nov 22, 2021

#[allow(non_snake_case)]
pub fn Ok<T>(t: T) -> anyhow::Result<T> {
    core::result::Result::Ok(t)
}

This simplifies creation of an anyhow::Result in places where type inference cannot deduce the E type of the result — without needing to write Ok::<_, anyhow::Error>(value).

One might think that anyhow::Result::Ok(value) would work in such cases but it does not.

error[E0282]: type annotations needed for `std::result::Result<i32, E>`
  --> src/main.rs:11:13
   |
11 |     let _ = anyhow::Result::Ok(1);
   |         -   ^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `E` declared on the enum `Result`
   |         |
   |         consider giving this pattern the explicit type `std::result::Result<i32, E>`, where the type parameter `E` is specified
Repository owner locked and limited conversation to collaborators Nov 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant