Skip to content

Commit 2d4bb64

Browse files
committed
Add anyhow::Ok
Closes #192.
1 parent 21a4e70 commit 2d4bb64

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,30 @@ pub trait Context<T, E>: context::private::Sealed {
602602
F: FnOnce() -> C;
603603
}
604604

605+
/// Equivalent to `Ok::<_, anyhow::Error>(value)`.
606+
///
607+
/// This simplifies creation of an anyhow::Result in places where type inference
608+
/// cannot deduce the `E` type of the result &mdash; without needing to write
609+
/// `Ok::<_, anyhow::Error>(value)`.
610+
///
611+
/// One might think that `anyhow::Result::Ok(value)` would work in such cases
612+
/// but it does not.
613+
///
614+
/// ```console
615+
/// error[E0282]: type annotations needed for `std::result::Result<i32, E>`
616+
/// --> src/main.rs:11:13
617+
/// |
618+
/// 11 | let _ = anyhow::Result::Ok(1);
619+
/// | - ^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `E` declared on the enum `Result`
620+
/// | |
621+
/// | consider giving this pattern the explicit type `std::result::Result<i32, E>`, where the type parameter `E` is specified
622+
/// ```
623+
#[doc(hidden)]
624+
#[allow(non_snake_case)]
625+
pub fn Ok<T>(t: T) -> Result<T> {
626+
Result::Ok(t)
627+
}
628+
605629
// Not public API. Referenced by macro-generated code.
606630
#[doc(hidden)]
607631
pub mod private {

0 commit comments

Comments
 (0)