File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff 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 — 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) ]
607631pub mod private {
You can’t perform that action at this time.
0 commit comments