fn_chain 0.1.2

Helper macro/function to create function chaining
Documentation
  • Coverage
  • 100%
    6 out of 6 items documented3 out of 6 items with examples
  • Size
  • Source code size: 29.7 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.89 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • NattapongSiri/fn_chain
    2 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • NattapongSiri

fn_trait library

This trait provide helper function and macro called chain to ease chaining of function where output of first function piped directly as input to another function and so forth.

Use case 1 - Macro chain

use fn_chain::chain;

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

fn pass_through(v : f64) -> f64 {
    v
}

assert_eq!(
    6f64, 
    chain!(
        simple_add(1, 2, 3), 
        |result: i32| {(result as f64).powi(2)}, 
        |sqr: f64| {sqr.powf(0.5)},
        pass_through,
        pass_through
    )
);

Use case 2 - function chain

use fn_chain::chain;

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

fn pass_through(v : f64) -> f64 {
    v
}

assert_eq!(6f64, *chain(simple_add(1, 2, 3))
                        .chain(|result| {(result as f64).powi(2)})
                        .chain(|sqr| sqr.powf(0.5))
                        .chain(pass_through)
                        .chain(pass_through));