#tap #traits #chain

no-std tap-trait

Inspect and mutate values without leaving the method chain

2 releases (1 stable)

1.0.0 Jul 26, 2021
0.0.0 Jul 26, 2021

#1814 in Rust patterns

24 downloads per month

MIT license

6KB
93 lines

Tap Trait

Test Crates.io Version

Inspect and mutate values without leaving the method chain.

Example

use tap_trait::Tap;
use pipe_trait::Pipe;
let result = 2i32
    .tap(|x| assert_eq!(x, 2))
    .tap_mut(|x| *x += 1)
    .tap(|x| assert_eq!(x, 3))
    .tap_mut(|x| *x *= 3)
    .tap(|x| assert_eq!(x, 9))
    .pipe(|x| -x)
    .tap(|x| assert_eq!(x, -9))
    .pipe_ref(ToString::to_string)
    .tap_ref(|x| assert_eq!(x, "-9"))
    .tap_mut(|x| *x += ".0")
    .tap_ref(|x| assert_eq!(x, "-9.0"));
assert_eq!(result, "-9.0");

License

MIT © Hoàng Văn Khải.


lib.rs:

Inspect and mutate values without leaving the method chain.

Example:

use tap_trait::Tap;
use pipe_trait::Pipe;
let result = 2i32
    .tap(|x| assert_eq!(x, 2))
    .tap_mut(|x| *x += 1)
    .tap(|x| assert_eq!(x, 3))
    .tap_mut(|x| *x *= 3)
    .tap(|x| assert_eq!(x, 9))
    .pipe(|x| -x)
    .tap(|x| assert_eq!(x, -9))
    .pipe_ref(ToString::to_string)
    .tap_ref(|x| assert_eq!(x, "-9"))
    .tap_mut(|x| *x += ".0")
    .tap_ref(|x| assert_eq!(x, "-9.0"));
assert_eq!(result, "-9.0");

No runtime deps