Skip to main content

OrFailExt

Trait OrFailExt 

Source
pub trait OrFailExt<T> {
    // Required method
    fn or_fail(self) -> TestResult<T>;
}
Expand description

Provides an extension method for converting an arbitrary type into a Result.

A type can implement this trait to provide an easy way to return immediately from a test in conjunction with the ? operator. This is useful for Result types whose Result::Err variant does not implement std::error::Error.

There is an implementation of this trait for [anyhow::Error] (which does not implement std::error::Error) when the anyhow feature is enabled. Importing this trait allows one to easily map [anyhow::Error] to a test failure:

#[test]
fn should_work() -> Result<()> {
    let value = something_which_can_fail().or_fail()?;
    ...
}

fn something_which_can_fail() -> anyhow::Result<...> { ... }

Required Methods§

Source

fn or_fail(self) -> TestResult<T>

Converts this instance into a Result.

Typically, the Self type is itself a std::result::Result. This method should then map the Err variant to a [TestAssertionFailure] and leave the Ok variant unchanged.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§