Skip to content

Commit

Permalink
Add anyhow::Ok
Browse files Browse the repository at this point in the history
Closes #192.
  • Loading branch information
dtolnay committed Nov 27, 2021
1 parent 21a4e70 commit 2d4bb64
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/lib.rs
Expand Up @@ -602,6 +602,30 @@ pub trait Context<T, E>: context::private::Sealed {
F: FnOnce() -> C;
}

/// Equivalent to `Ok::<_, anyhow::Error>(value)`.
///
/// This simplifies creation of an anyhow::Result in places where type inference
/// cannot deduce the `E` type of the result &mdash; 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.
///
/// ```console
/// 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
/// ```
#[doc(hidden)]
#[allow(non_snake_case)]
pub fn Ok<T>(t: T) -> Result<T> {
Result::Ok(t)
}

// Not public API. Referenced by macro-generated code.
#[doc(hidden)]
pub mod private {
Expand Down

0 comments on commit 2d4bb64

Please sign in to comment.