6 releases (1 stable)
1.0.0 | May 26, 2023 |
---|---|
0.1.4 | May 26, 2023 |
#2576 in Rust patterns
58 downloads per month
9KB
145 lines
piping
piping
provides a pipe!
macro that allows you to use the pipeline operator in Rust.
let wrapped = orig_and_double(add(2, num)).1 as isize;
let piped = pipe! {
num |> add(2, __) |> orig_and_double(__),
(_, doubled) |> doubled as isize,
};
Features
- Hack-like syntax.
- Multiple pipelines in one statement.
- Destructuring of previous pipeline results.
Docs
Documentation is provided on docs.rs.
How does it work?
fn add(a: usize, b: usize) -> usize {
a + b
}
fn orig_and_double(num: usize) -> (usize, usize) {
(num, num * 2)
}
let num = 4;
let piped = pipe! {
num |> add(2, __) |> orig_and_double(__),
(_, doubled) |> doubled as isize,
};
// Expands to...
let piped = {
let __ = num;
let __ = add(2, __);
let (_, doubled) = orig_and_double(__);
doubled as isize
};
You can pass any expression in as the input.
Notice that you can chain pipelines with ,
s to destructure the result of the previous pipeline.
The macro also tries to optimize the generated code to minimize the amount of reassigning done.
Dependencies
~245–700KB
~17K SLoC