AssertSomeAndExt

Trait AssertSomeAndExt 

Source
pub trait AssertSomeAndExt<T> {
    // Required methods
    fn assert_some_and(self, cond: impl FnOnce(&T) -> bool) -> Self;
    fn debug_assert_some_and(self, cond: impl FnOnce(&T) -> bool) -> Self;
}
Expand description

An extension trait to add the assertion_some_and methods.

Required Methods§

Source

fn assert_some_and(self, cond: impl FnOnce(&T) -> bool) -> Self

Asserts the Option is Some and satisfies the condition.

§Panics

If it is None or the condition is not satisfied, the method panics.

§Examples
use chain_assertions::prelude::*;

let x: Option<i32> = Some(21);
let x = x.assert_some_and(|x| x >= &20).map(|x| x * 2);
assert_eq!(x, Some(42));
use chain_assertions::prelude::*;

let x: Option<i32> = Some(19);
let _ = x.assert_some_and(|x| x >= &20).map(|x| x * 2);
//        ^-- panics here
Source

fn debug_assert_some_and(self, cond: impl FnOnce(&T) -> bool) -> Self

Asserts the Option is Some and satisfies the condition only in debug builds.

§Panics

The method panics if all following conditions are satisfied:

  • It is None or the condition is not satisfied
  • debug_assertions is enabled
  • passthrough feature is disabled

Otherwise, the method returns self as is.

§Examples
use chain_assertions::prelude::*;

let x: Option<i32> = Some(21);
let x = x.debug_assert_some_and(|x| x >= &20).map(|x| x * 2);
assert_eq!(x, Some(42));
use chain_assertions::prelude::*;

let x: Option<i32> = Some(19);
let _ = x.debug_assert_some_and(|x| x >= &20).map
//        ^-- panics here if debug_assertion is enabled

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> AssertSomeAndExt<T> for Option<T>
where T: Debug,

Source§

fn assert_some_and(self, cond: impl FnOnce(&T) -> bool) -> Self

Source§

fn debug_assert_some_and(self, _cond: impl FnOnce(&T) -> bool) -> Self

Implementors§