Trait eyre::OptionExt

source ·
pub trait OptionExt<T>: Sealed {
    // Required method
    fn ok_or_eyre<M>(self, message: M) -> Result<T>
       where M: Debug + Display + Send + Sync + 'static;
}
Expand description

Provides the ok_or_eyre method for Option.

This trait is sealed and cannot be implemented for types outside of eyre.

§Example

use eyre::OptionExt;

let option: Option<()> = None;

let result = option.ok_or_eyre("static str error");

assert_eq!(result.unwrap_err().to_string(), "static str error");

§ok_or_eyre vs ok_or_else

If string interpolation is required for the generated report, use ok_or_else instead, invoking eyre! to perform string interpolation:

use eyre::eyre;

let option: Option<()> = None;

let result = option.ok_or_else(|| eyre!("{} error", "dynamic"));

assert_eq!(result.unwrap_err().to_string(), "dynamic error");

ok_or_eyre incurs no runtime cost, as the error object is constructed from the provided static argument only in the None case.

Required Methods§

source

fn ok_or_eyre<M>(self, message: M) -> Result<T>
where M: Debug + Display + Send + Sync + 'static,

Transform the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Report.

ok_or_eyre allows for eyre Report error objects to be lazily created from static messages in the None case.

For dynamic error messages, use ok_or_else, invoking eyre! in the closure to perform string interpolation.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<T> OptionExt<T> for Option<T>

source§

fn ok_or_eyre<M>(self, message: M) -> Result<T>
where M: Debug + Display + Send + Sync + 'static,

Implementors§