#chaining #pipe #pipeline #simple

no-std ufcs

Helper trait to call free functions using method call syntax

1 unstable release

0.1.0 Dec 10, 2019

#787 in Rust patterns

34 downloads per month

Unlicense OR MIT

4KB

ufcs.rs

Crates.io License Build Status

ufcs::Pipe β€” helper trait to call free functions using method call syntax.

Rust already allows calling methods using function call syntax, but not the other way around. This crate fills the gap by providing a simple helper trait Pipe.

Usage

Cargo.toml:

[dependencies]
ufcs = "0.1.0"

Rust code:

use ufcs::Pipe;

Examples

// Write this
foo().pipe(bar).pipe(baz);

// Instead of this
baz(bar(foo()));
// Write this
let text: String = reqwest::get("http://httpbin.org/get")
    .await?
    .json::<serde_json::Value>()
    .await?
    .pipe(toml::Value::try_from)?
    .pipe(|x| toml::to_string(&x))?;

// Instead of this
let text: String = toml::to_string(&toml::Value::try_from(
    reqwest::get("http://httpbin.org/get")
        .await?
        .json::<serde_json::Value>()
        .await?,
)?)?;

See tests for more examples.

See also

Roughtly the same feature is either implemented or proposed in various languages.

Rust

Other languages

No runtime deps

”το]