AssertErrExt

Trait AssertErrExt 

Source
pub trait AssertErrExt {
    // Required methods
    fn assert_err(self) -> Self;
    fn debug_assert_err(self) -> Self;
}
Expand description

An extension trait to add the assertion_err methods.

Required Methods§

Source

fn assert_err(self) -> Self

Asserts the Result is Err.

§Panics

The method panics if it is Ok and passthrough feature is disabled. Otherwise, the method return self as is.

§Examples
use chain_assertions::prelude::*;

let x: Result<&str, i32> = Err(21);
let x = x.assert_err().map_err(|x| x * 2);
assert_eq!(x, Err(42));
use chain_assertions::prelude::*;

let x: Result<&str, i32> = Ok("success");
let x = x.assert_err().map_err(|x| x * 2);
//        ^-- panics here
Source

fn debug_assert_err(self) -> Self

Asserts the Result is Err but check only in debug builds.

§Panics

The method panics if all following conditions are satisfied:

  • It is Err.
  • debug_assertions is enabled.
  • passthrough feature is disabled.

Otherwise, the method returns self as is.

§Examples
use chain_assertions::prelude::*;

let x: Result<&str, i32> = Err(21);
let x = x.debug_assert_err().map_err(|x| x * 2);
assert_eq!(x, Err(42), "Expected Err(42)");
use chain_assertions::prelude::*;

let x: Result<&str, i32> = Ok("success");
let _ = x.debug_assert_err();
//        ^-- panics here only in debug builds

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T, E> AssertErrExt for Result<T, E>
where T: Debug,

Source§

fn assert_err(self) -> Self

Source§

fn debug_assert_err(self) -> Self

Implementors§