Skip to main content

Crate pipei

Crate pipei 

Source
Expand description

§pipei

A zero-cost library for chaining multi-argument function calls in method syntax.

pipe allows writing x.pipe(f)(y, z) instead of f(x, y, z) by currying the receiver into the first argument. tap provides the same call form for side effects: it passes the value to a function for inspection or mutation and then returns the original value.

§Extension traits

  • Pipe::pipe: Curries self into the first argument of a function, returning the result.
  • Tap::tap: Passes self to a function for inspection or mutation, then returns the original (now possibly modified) value.
  • TapWith::tap_with: Like tap, but first applies a projection; the side effect only runs if the projection returns Some.
fn add(a: i32, b: i32) -> i32 { a + b }
fn log(x: &i32) { println!("{x}"); }

let result = 1
    .pipe(add)(2)
    .tap(log)()
    .pipe(Option::Some)();

assert_eq!(result, Some(3));

Traits§

Pipe
Extension trait for transforming values.
Tap
Extension trait for running side effects, returning the original value.
TapWith
Extension trait for running side effects on a projection of the value.