| 1 |
<div class="title-block" style="text-align: center;" align="center"> |
| 2 |
|
| 3 |
# `tap` <!-- omit in toc --> |
| 4 |
|
| 5 |
## Suffix-Position Pipeline Behavior <!-- omit in toc --> |
| 6 |
|
| 7 |
[![Crate][crate_img]][crate] |
| 8 |
[![Documentation][docs_img]][docs] |
| 9 |
[![License][license_img]][license_file] |
| 10 |
|
| 11 |
[![Crate Downloads][downloads_img]][crate] |
| 12 |
[![Crate Size][loc_img]][loc] |
| 13 |
|
| 14 |
</div> |
| 15 |
|
| 16 |
This crate provides extension methods on all types that allow transparent, |
| 17 |
temporary, inspection/mutation (tapping), transformation (piping), or type |
| 18 |
conversion. These methods make it convenient for you to insert debugging or |
| 19 |
modification points into an expression without requiring you to change any other |
| 20 |
portions of your code. |
| 21 |
|
| 22 |
## Example Use |
| 23 |
|
| 24 |
### Tapping |
| 25 |
|
| 26 |
You can tap inside a method-chain expression for logging without requiring a |
| 27 |
rebind. For instance, you may write a complex expression without any |
| 28 |
intermediate debugging steps, and only later decide that you want them. |
| 29 |
Ordinarily, this transform would look like this: |
| 30 |
|
| 31 |
```rust |
| 32 |
extern crate reqwest; |
| 33 |
extern crate tracing; |
| 34 |
|
| 35 |
// old |
| 36 |
let body = reqwest::blocking::get("https://example.com")? |
| 37 |
.text()?; |
| 38 |
tracing::debug!("Response contents: {}", body); |
| 39 |
|
| 40 |
// new, with debugging |
| 41 |
let resp = reqwest::blocking::get("https://example.com")?; |
| 42 |
tracing::debug!("Response status: {}", resp.status()); |
| 43 |
let body = resp.text()?; |
| 44 |
tracing::debug!("Response contents: {}", body); |
| 45 |
``` |
| 46 |
|
| 47 |
while with tapping, you can plug the logging statement directly into the overall |
| 48 |
expression, without making any other changes: |
| 49 |
|
| 50 |
```rust |
| 51 |
extern crate reqwest; |
| 52 |
extern crate tracing; |
| 53 |
|
| 54 |
let body = reqwest::blocking::get("https://example.com")? |
| 55 |
// The only change is the insertion of this line |
| 56 |
.tap(|resp| tracing::debug!("Response status: {}", resp.status())) |
| 57 |
.text()?; |
| 58 |
tracing::debug!("Response contents: {}", body); |
| 59 |
``` |
| 60 |
|
| 61 |
### Mutable Tapping |
| 62 |
|
| 63 |
Some APIs are written to require mutable borrows, rather than value-to-value |
| 64 |
transformations, which can require temporary rebinding in order to create |
| 65 |
mutability in an otherwise-immutable context. For example, collecting data into |
| 66 |
a vector, sorting the vector, and then freezing it, might look like this: |
| 67 |
|
| 68 |
```rust |
| 69 |
let mut collection = stream().collect::<Vec<_>>(); |
| 70 |
collection.sort(); |
| 71 |
// potential error site: inserting other mutations here |
| 72 |
let collection = collection; // now immutable |
| 73 |
``` |
| 74 |
|
| 75 |
But with a mutable tap, you can avoid the duplicate binding *and* guard against |
| 76 |
future errors due to the presence of a mutable binding: |
| 77 |
|
| 78 |
```rust |
| 79 |
let collection = stream.collect::<Vec<_>>() |
| 80 |
.tap_mut(|v| v.sort()); |
| 81 |
``` |
| 82 |
|
| 83 |
The `.tap_mut()` and related methods provide a mutable borrow to their argument, |
| 84 |
and allow the final binding site to choose their own level of mutability without |
| 85 |
exposing the intermediate permission. |
| 86 |
|
| 87 |
### Piping |
| 88 |
|
| 89 |
In addition to transparent inspection or modification points, you may also wish |
| 90 |
to use suffix calls for subsequent operations. For example, the standard library |
| 91 |
offers the free function `fs::read` to convert `Path`-like objects into |
| 92 |
`Vec<u8>` of their filesystem contents. Ordinarily, free functions require use |
| 93 |
as: |
| 94 |
|
| 95 |
```rust |
| 96 |
use std::fs; |
| 97 |
|
| 98 |
let mut path = get_base_path(); |
| 99 |
path.push("logs"); |
| 100 |
path.push(&format!("{}.log", today())); |
| 101 |
let contents = fs::read(path)?; |
| 102 |
``` |
| 103 |
|
| 104 |
whereäs use of tapping (for path modification) and piping (for `fs::read`) could |
| 105 |
be expressed like this: |
| 106 |
|
| 107 |
```rust |
| 108 |
use std::fs; |
| 109 |
|
| 110 |
let contents = get_base_path() |
| 111 |
.tap_mut(|p| p.push("logs")) |
| 112 |
.tap_mut(|p| p.push(&format!("{}.log", today()))) |
| 113 |
.pipe(fs::read)?; |
| 114 |
``` |
| 115 |
|
| 116 |
As a clearer example, consider the syntax required to apply multiple free |
| 117 |
funtions without `let`-bindings looks like this: |
| 118 |
|
| 119 |
```rust |
| 120 |
let val = last( |
| 121 |
third( |
| 122 |
second( |
| 123 |
first(original_value), |
| 124 |
another_arg, |
| 125 |
) |
| 126 |
), |
| 127 |
another_arg, |
| 128 |
); |
| 129 |
``` |
| 130 |
|
| 131 |
which requires reading the expression in alternating, inside-out, order, to |
| 132 |
understand the full sequence of evaluation. With suffix calls, even free |
| 133 |
functions can be written in a point-free style that maintains a clear temporal |
| 134 |
and syntactic order: |
| 135 |
|
| 136 |
```rust |
| 137 |
let val = original_value |
| 138 |
.pipe(first) |
| 139 |
.pipe(|v| second(v, another_arg)) |
| 140 |
.pipe(third) |
| 141 |
.pipe(|v| last(v, another_arg)); |
| 142 |
``` |
| 143 |
|
| 144 |
As piping is an ordinary method, not a syntax transformation, it still requires |
| 145 |
that you write function-call expressions when using a function with multiple |
| 146 |
arguments in the pipeline. |
| 147 |
|
| 148 |
### Conversion |
| 149 |
|
| 150 |
The `conv` module is the simplest: it provides two traits, `Conv` and `TryConv`, |
| 151 |
which are sibling traits to `Into<T>` and `TryInto<T>`. Their methods, |
| 152 |
`Conv::conv::<T>` and `TryConv::try_conv::<T>`, call the corresponding |
| 153 |
trait implementation, and allow you to use `.into()`/`.try_into()` in |
| 154 |
non-terminal method calls of an expression. |
| 155 |
|
| 156 |
```rust |
| 157 |
let bytes = "hello".into().into_bytes(); |
| 158 |
``` |
| 159 |
|
| 160 |
does not compile, because Rust cannot decide the type of `"hello".into()`. |
| 161 |
Instead of rewriting the expression to use an intermediate `let` binding, you |
| 162 |
can write it as |
| 163 |
|
| 164 |
```rust |
| 165 |
let bytes = "hello".conv::<String>().into_bytes(); |
| 166 |
``` |
| 167 |
|
| 168 |
## Full Functionality |
| 169 |
|
| 170 |
The `Tap` and `Pipe` traits both provide a large number of methods, which use |
| 171 |
different parts of the Rust language’s facilities for well-typed value access. |
| 172 |
Rather than repeat the API documentation here, you should view the module items |
| 173 |
in the [documentation][docs]. |
| 174 |
|
| 175 |
As a summary, these traits provide methods that, upon receipt of a value, |
| 176 |
|
| 177 |
- apply no transformation |
| 178 |
- apply an `AsRef` or `AsMut` implementation |
| 179 |
- apply a `Borrow` or `BorrowMut` implementation |
| 180 |
- apply the `Deref` or `DerefMut` implementation |
| 181 |
|
| 182 |
before executing their effect argument. |
| 183 |
|
| 184 |
In addition, each `Tap` method `.tap_x` has a sibling method `.tap_x_dbg` that |
| 185 |
performs the same work, but only in debug builds; in release builds, the method |
| 186 |
call is stripped. This allows you to leave debugging taps in your source code, |
| 187 |
without affecting your project’s performance in true usage. |
| 188 |
|
| 189 |
Lastly, the `tap` module also has traits `TapOptional` and `TapFallible` which |
| 190 |
run taps on the variants of `Option` and `Result` enums, respectively, and do |
| 191 |
nothing when the variant does not match the method name. `TapOptional::tap_some` |
| 192 |
has no effect when called on a `None`, etc. |
| 193 |
|
| 194 |
<!-- Badges --> |
| 195 |
[crate]: https://crates.io/crates/tap "Crate Link" |
| 196 |
[crate_img]: https://img.shields.io/crates/v/tap.svg?logo=rust "Crate Page" |
| 197 |
[docs]: https://docs.rs/tap "Documentation" |
| 198 |
[docs_img]: https://docs.rs/tap/badge.svg "Documentation Display" |
| 199 |
[downloads_img]: https://img.shields.io/crates/dv/tap.svg?logo=rust "Crate Downloads" |
| 200 |
[license_file]: https://github.com/myrrlyn/tap/blob/master/LICENSE.txt "License File" |
| 201 |
[license_img]: https://img.shields.io/crates/l/tap.svg "License Display" |
| 202 |
[loc]: https://github.com/myrrlyn/tap "Repository" |
| 203 |
[loc_img]: https://tokei.rs/b1/github/myrrlyn/tap?category=code "Repository Size" |