AssertErrAndExt

Trait AssertErrAndExt 

Source
pub trait AssertErrAndExt<T, E> {
    // Required methods
    fn assert_err_and(self, cond: impl FnOnce(&E) -> bool) -> Self;
    fn debug_assert_err_and(self, cond: impl FnOnce(&E) -> bool) -> Self;
}

Required Methods§

Source

fn assert_err_and(self, cond: impl FnOnce(&E) -> bool) -> Self

Asserts the Result is Err and satisfies the condition.

§Panics

The method panics if all following conditions are satisfied:

  • It is Ok, or Err but user-provided condition returns false
  • debug_assertions is enabled
  • 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_and(|x| x == &21).map_err(|x| x * 2);
use chain_assertions::prelude::*;

let x: Result<&str, i32> = Ok("debuggable");
let x = x.assert_err_and(|x| x == &42).map_err(|x| x + 1);
//        ^-- panics here
Source

fn debug_assert_err_and(self, cond: impl FnOnce(&E) -> bool) -> Self

Asserts the Result is Err and satisfies the condition only in debug builds.

§Panics

The method panics if all following conditions are satisfied:

  • It is Ok, or Err but user-provided condition returns false
  • debug_assertions is enabled
  • 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.debug_assert_err_and(|x| x == &21).map_err(|x| x * 2);
assert_eq!(x, Err(42), "Expected Err(42)");
use chain_assertions::prelude::*;

let x: Result<&str, i32> = Ok("debuggable");
let x = x.debug_assert_err_and(|x| x == &42).map_err(|x| x + 1);
//        ^-- 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> AssertErrAndExt<T, E> for Result<T, E>
where T: Debug, E: Debug,

Source§

fn assert_err_and(self, cond: impl FnOnce(&E) -> bool) -> Self

Source§

fn debug_assert_err_and(self, _cond: impl FnOnce(&E) -> bool) -> Self

Implementors§