Trait wyz::pipe::Pipe

source · []
pub trait Pipe: Sized {
    fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
    where
        R: Sized
, { ... } }
Expand description

Permit suffixed call of any function on a value.

This trait provides a verbose, ugly version of the functional-language operator |>. For any value, calling .pipe(some_function) translates to some_function(value).

Because it takes self by value, this trait is only implemented on Sized types.

Provided methods

Pipes a value into a function that cannot ordinarily be called in suffix position.

Parameters
  • self: Any value
  • func: Any function, which will receive self as its first and only parameter. The return value of this function is then returned from pipe.

Because this is a library function, not a language feature, it is not able to use the method-call shorthand of function(...other_params). A suffix function with other params must be called as a closure:

use wyz::pipe::Pipe;

fn add(a: i32, b: i32) -> i32 { a + b }

assert_eq!(5.pipe(|a| add(a, 2)), 7);

This is more verbose than calling the function in prefix position; its only value is for fitting into suffix-call chains.

The .p method is a shorthand alias for .pipe.

Type Parameters
  • R: The return value of func, which is then returned from .pipe. This is placed as a function type parameter rather than a trait type parameter so that it can be specified in ambiguous call sites.

Implementors