Git Product home page Git Product logo

tap-rs's Introduction

tap

Documentation crates.io version Language (Rust)

A simple crate exposing tapping functionality for all types, and extended functionality for Option, Result & Future. Often useful for logging.

The tap operation takes, and then returns, full ownership of the variable being tapped. This means that the closure may have mutable access to the variable, even if the variable was previously immutable. This prevents accidental mutation.

Features

  • future - Exposes the TapFutureOps trait, providing tap_ready, tap_not_ready & tap_err (requires the futures crate).

    Futures do not provide mutable access for tap closures.

  • nom3 - Exposes the TapNomOps trait, which provides tap_done, tap_error, and tap_incomplete on their respective variants of nom::IResult.

Example

extern crate tap;

use tap::*;

fn filter_map() {
    let values: &[Result<i32, &str>] = &[Ok(3), Err("foo"), Err("bar"), Ok(8)];

    let _ = values.iter().filter_map(|result| {
        // It is especially useful in filter maps, allowing error information to
        // be logged/printed before the information is discarded.
        result.tap_err(|error| eprintln!("Invalid entry: {}", error)).ok()
    });
}

fn basic() {
    let mut foo = 5;

    // The `tap` extension can be used on all types
    if 10.tap(|v| foo += *v) > 0 {
        assert_eq!(foo, 15);
    }

    // Results have `tap_err` & `tap_ok` available.
    let _: Result<i32, i32> = Err(5).tap_err(|e| foo = *e);
    assert_eq!(foo, 5);

    // Options have `tap_some` & `tap_none` available.
    let _: Option<i32> = None.tap_none(|| foo = 10);
    assert_eq!(foo, 10);
}

fn mutable() {
    let base = [1, 2, 3];
    let mutated = base.tap(|arr| for elt in arr.iter_mut() {
        *elt *= 2;
    });
    // base was consumed and is out of scope.
    assert_eq!(mutated, [2, 4, 6]);
}

tap-rs's People

Contributors

darfink avatar lcnr avatar myrrlyn avatar tov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.