Skip to main content

Tap

Trait Tap 

Source
pub trait Tap<const ARITY: usize, State> {
    // Provided method
    fn tap<R, F, Args>(self, f: F) -> F::Curry
       where F: Curry<ARITY, Args, State, Own, TapMark, Self, R>,
             Self: Sized { ... }
}
Expand description

Extension trait for running side effects, returning the original value.

Provided Methods§

Source

fn tap<R, F, Args>(self, f: F) -> F::Curry
where F: Curry<ARITY, Args, State, Own, TapMark, Self, R>, Self: Sized,

Passes self into f for inspection or mutation, then returns the original (possibly modified) value. The function receives self by shared or exclusive reference depending on its signature.

§Examples
fn log(x: &i32) { println!("val: {x}"); }
fn assert_between(x: &i32, lo: i32, hi: i32) { assert!(*x >= lo && *x <= hi); }
fn add_assign(x: &mut i32, y: i32) { *x += y; }

let result = 15
    .tap(log)()
    .tap(assert_between)(0, 100)
    .tap(add_assign)(3);

assert_eq!(result, 18);

struct State { count: i32 }

let s = State { count: 0 }
    .tap(|s: &mut State| s.count += 1)()
    .tap(|s: &mut State| s.count *= 10)();

assert_eq!(s.count, 10);

Implementors§

Source§

impl<const ARITY: usize, State, T> Tap<ARITY, State> for T