pipei
pipei provides a zero-cost, type-safe way to chain multi-argument free functions using method syntax, without the need for closures or extension traits.
Intuitively, the .pipe() operator transforms a free function f(x, y, z) into a method call x.pipe(f)(y, z).
Enabling arities
Enable the arities you need via features to control compile times.
[]
= "*" # default: features = ["up_to_5"]
# pipei = { version = "*", features = ["up_to_10"] }
# pipei = { version = "*", features = ["0", "1", "3", "4"] }```
Note: This library currently requires the #![feature(impl_trait_in_assoc_type)] nightly feature.
Basic chaining
pipe passes the value into the function and returns the result. tap inspects or mutates the input, ignores the result, and returns the original value.
Unified Tap API: tap methods seamlessly accept functions taking either &Self (immutable) or &mut Self (mutable).
use ;
let maybe_num = 2
.pipe
.pipe
.pipe;
.pipe;
assert_eq!;
let val = 2
.tap // Immutable: passes &i32
.tap // Mutable: passes &mut i32
.tap;
assert_eq!;
Pipe for method binding
pipe can be used to bound methods by partially applying an object to a method.
use Pipe;
;
let scalar = Scalar;
// Extracting the bound method `scalar.lin` as a standalone function.
let method_as_function = scalar.pipe;
assert_eq!;
TapWith
While tap works great for direct access, tap_with separates the projection logic from the side-effect logic. This is necessary when the adaptation is non-trivial (e.g., calling a method instead of simple dereferencing) or when inspecting specific fields.
use TapWith;
let s = Stringfrom;
// "as_bytes" is a method, not a Deref, so automatic coercion won't work.
s.tap_with;
let cfg = Config ;
// Projects &Config -> &u16 to reuse a standard check function
cfg.tap_with;
PipeRef
pipe_ref allows extracting a sub-value (borrow) from a mutable parent for transformation chains without moving the parent.
use ;
let mut data = ;
// Start a pipe from &mut data, get a mutable reference to index 0
*data.pipe_ref = 99;
assert_eq!;
Comparison with the tap crate
The tap crate is the standard solution for continuous chaining. pipei extends this concept to multi-argument functions to address specific issues related to control flow, error handling, and nesting.
When function arguments are the results of other chains (nesting) and those chains involve fallible operations (using ?), standard closure-based chaining becomes difficult to manage.
Consider a workflow where we load a background, load and resize an overlay, composite them, and save the result. Both load and save are fallible (return Result).
Standard Rust:
The logic reads "inside-out": save is written first, but executes last.
save;
Using tap:
We can try to linearize it, but the secondary chain (overlay) must happen inside a closure. Because load("overlay")? uses the ? operator, the closure itself returns a Result.
load?
.pipe?
.pipe;
Using pipei:
The primary flow (load -> composite -> save) remains linear. The secondary flow (overlay -> resize) is handled inline. The ? operator works naturally without changing the pipeline types or requiring and_then.
load?
.pipe
.pipe;