https://github.com/kyza/piping
https://github.com/kyza/piping
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/kyza/piping
- Owner: Kyza
- License: mit
- Created: 2023-05-24T07:10:13.000Z (about 3 years ago)
- Default Branch: trunk
- Last Pushed: 2023-06-06T06:35:03.000Z (about 3 years ago)
- Last Synced: 2025-02-28T14:44:29.940Z (over 1 year ago)
- Language: Rust
- Size: 20.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# piping
`piping` provides a `pipe!` macro that allows you to use the pipeline operator in Rust.
```rs
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
- [x] [Hack-like syntax.](https://docs.hhvm.com/hack/expressions-and-operators/pipe)
- [x] Multiple pipelines in one statement.
- [x] Destructuring of previous pipeline results.
## Docs
Documentation is provided on [docs.rs](https://docs.rs/piping).
## How does it work?
```rs
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.