pipeop

Crates.iopipeop
lib.rspipeop
version0.2.0
sourcesrc
created_at2024-01-24 18:43:29.890595
updated_at2024-04-10 18:34:52.366028
descriptionAdding the pipe operator to Rust with a declarative macro.
homepagehttps://github.com/david-d-h/pipeop
repositoryhttps://github.com/david-d-h/pipeop
max_upload_size
id1112752
size10,363
David (david-d-h)

documentation

README

The pipe operator in Rust

This crate is exactly what you expect it to be, the pipe operator in Rust.

Usage

You can construct a "pipeline" by passing any expression and at least a single pipe into the ::pipeop::pipe! macro. There are some special things you can do but, in its most basic form the macro tries to literally call your pipe with the item in the pipeline.

const fn add_one(to: i32) -> i32 {
    to + 1
}

let result = pipe!(1 |> add_one |> add_one);
assert!(result, 3);

Invoking methods on the item in the pipeline

You can invoke methods on the item in the pipeline at any time by prefixing the pipe with a ..

This example calls the add method on the item in the pipelines with 1 as the single argument.

use std::ops::Add;
pipe!(1 |> .add(1));

Closure based pipes

You can also use closures as pipes, so you don't have to define a whole new function for every simple operation. Both types of closures are valid, you can have a closure that just evaluates an expression, or you can have a whole block of code.

pipe!("Hello!"
    |> .to_uppercase()
    |> |item| in println!("{}", item)
);

You can also make closure based pipes look a little nicer by using this syntax instead.

pipe!("Hello!"
    |> .to_uppercase()
    |> item in println!("{}", item)
);

You can of course accept a pattern in this "weird closure". This example extracts the inner bool value from the Test instance in the pipeline with pattern matching.

struct Test(bool);
let result = pipe!(Test(true) |> Test(it) in it);
assert_eq!(result, true);
Commit count: 33

cargo fmt