From da84e8c624804d99bf23c6e315eaf584722f6027 Mon Sep 17 00:00:00 2001 From: Kyle Lacy Date: Sat, 25 Nov 2023 20:49:47 -0800 Subject: [PATCH] Add `eyre::Ok` (#91) This change introduces the function eyre::Ok(), a backport of the convenience function anyhow::Ok() which avoids a lengthy turbofish when producing an Ok(()). --- eyre/src/lib.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/eyre/src/lib.rs b/eyre/src/lib.rs index 19c30a5..3d887ab 100644 --- a/eyre/src/lib.rs +++ b/eyre/src/lib.rs @@ -709,7 +709,7 @@ pub trait EyreHandler: core::any::Any + Send + Sync { } } - Ok(()) + Result::Ok(()) } /// Store the location of the caller who constructed this error report @@ -831,7 +831,7 @@ impl EyreHandler for DefaultHandler { } } - Ok(()) + Result::Ok(()) } #[cfg(track_caller)] @@ -1193,6 +1193,29 @@ pub trait ContextCompat: context::private::Sealed { F: FnOnce() -> D; } +/// Equivalent to Ok::<_, eyre::Error>(value). +/// +/// This simplifies creation of an eyre::Result in places where type inference +/// cannot deduce the `E` type of the result — without needing to write +/// `Ok::<_, eyre::Error>(value)`. +/// +/// One might think that `eyre::Result::Ok(value)` would work in such cases +/// but it does not. +/// +/// ```console +/// error[E0282]: type annotations needed for `std::result::Result` +/// --> src/main.rs:11:13 +/// | +/// 11 | let _ = eyre::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`, where the type parameter `E` is specified +/// ``` +#[allow(non_snake_case)] +pub fn Ok(t: T) -> Result { + Result::Ok(t) +} + // Not public API. Referenced by macro-generated code. #[doc(hidden)] pub mod private {