Skip to content

Commit

Permalink
Add eyre::Ok (#91)
Browse files Browse the repository at this point in the history
This change introduces the function eyre::Ok(), a backport of the convenience function anyhow::Ok() which avoids a lengthy turbofish when producing an Ok(()).
  • Loading branch information
kylewlacy committed Nov 26, 2023
1 parent a795c97 commit da84e8c
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions eyre/src/lib.rs
Expand Up @@ -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
Expand Down Expand Up @@ -831,7 +831,7 @@ impl EyreHandler for DefaultHandler {
}
}

Ok(())
Result::Ok(())
}

#[cfg(track_caller)]
Expand Down Expand Up @@ -1193,6 +1193,29 @@ pub trait ContextCompat<T>: 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 &mdash; 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<i32, E>`
/// --> 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<i32, E>`, where the type parameter `E` is specified
/// ```
#[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 da84e8c

Please sign in to comment.