devela::all

Trait Chain

Source
pub trait Chain<R> {
    // Provided methods
    fn chain<F>(self, f: F) -> R
       where F: FnOnce(Self) -> R,
             Self: Sized { ... }
    fn chain_ref<F>(&self, f: F) -> R
       where F: FnOnce(&Self) -> R { ... }
    fn chain_mut<F>(&mut self, f: F) -> R
       where F: FnOnce(&mut Self) -> R { ... }
}
Expand description

Allows chaining transformations by passing values through a sequence of functions.

Use Chain to thread operations where each function takes ownership or references the value and returns a new result.

§Examples

use devela::Chain;

let x = 1
    .chain(|x| x * 2)
    .chain(|x| x + 1)
    .chain(|x: i32| x.to_string());
assert_eq![x, 3.to_string()];
use devela::Chain;

// We can sort it, but we don't receive the new vec.
let v: Vec<i32> = vec![3, 2, 1, 5].chain_mut(|it| it.sort());

See also the Hook trait.

Provided Methods§

Source

fn chain<F>(self, f: F) -> R
where F: FnOnce(Self) -> R, Self: Sized,

Chain a function which takes the parameter by value.

Source

fn chain_ref<F>(&self, f: F) -> R
where F: FnOnce(&Self) -> R,

Chain a function which takes the parameter by shared reference.

Source

fn chain_mut<F>(&mut self, f: F) -> R
where F: FnOnce(&mut Self) -> R,

Chain a function which takes the parameter by exclusive reference.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T: ?Sized, R> Chain<R> for T