| 1 |
<div align="center"> |
| 2 |
|
| 3 |
# `wyz` <!-- omit in toc --> |
| 4 |
|
| 5 |
[![Crate][crate_img]][crate] |
| 6 |
[![Documentation][docs_img]][docs] |
| 7 |
[![License][license_img]][license_file] |
| 8 |
|
| 9 |
[![Continuous Integration][travis_img]][travis] |
| 10 |
[![Code Coverage][codecov_img]][codecov] |
| 11 |
[![Crate Downloads][downloads_img]][crate] |
| 12 |
[![Crate Size][loc_img]][loc] |
| 13 |
|
| 14 |
</div> |
| 15 |
|
| 16 |
I have developed a collection of utility and convenience Rust modules that are |
| 17 |
useful to me, and may be useful to you also. |
| 18 |
|
| 19 |
This crate is a collection of largely-independent small modules. I do not |
| 20 |
currently offer features to disable modules independently of each other, but |
| 21 |
their compilation cost is small enough to essentially not matter. |
| 22 |
|
| 23 |
## Modules <!-- omit in toc --> |
| 24 |
|
| 25 |
1. [`conv`](#conv) |
| 26 |
1. [`exit`](#exit) |
| 27 |
1. [`fmt`](#fmt) |
| 28 |
1. [`pipe`](#pipe) |
| 29 |
1. [`tap`](#tap) |
| 30 |
|
| 31 |
## `conv` |
| 32 |
|
| 33 |
This module provides a single trait, of the same name, with a single generic |
| 34 |
method, also of the same name. This trait is a sibling to `Into`, but rather |
| 35 |
than placing its type parameter in the trait (`Into::<T>::into`), `Conv` places |
| 36 |
it in the method: `Conv::conv::<T>`. |
| 37 |
|
| 38 |
By placing the type parameter in the method name, `.conv` can be called in |
| 39 |
suffix position in expressions where the result type cannot be inferred and must |
| 40 |
be explicitly stated. |
| 41 |
|
| 42 |
```rust |
| 43 |
use wyz::conv::Conv; |
| 44 |
|
| 45 |
let digits = 0xabcd.conv::<String>().len(); |
| 46 |
``` |
| 47 |
|
| 48 |
This is a trivial example, but writing a code context where `.conv` makes sense |
| 49 |
takes a lot more context than a `README` wants. |
| 50 |
|
| 51 |
## `exit` |
| 52 |
|
| 53 |
This is a macro that calls `std::process::exit`. It can return a status code, |
| 54 |
and also print a message to `stderr`. |
| 55 |
|
| 56 |
```rust |
| 57 |
use wyz::exit::exit; |
| 58 |
|
| 59 |
exit!(); |
| 60 |
exit!(2); |
| 61 |
exit!(3, "This is a {} message", "failure"); |
| 62 |
``` |
| 63 |
|
| 64 |
The default call is `std::process::exit(1)`; a call may provide its own exit |
| 65 |
code and, in addition, a set of arguments to pass directly to `eprintln!`. The |
| 66 |
error message is not guaranteed to be emitted, as `stderr` may be closed at time |
| 67 |
of `exit!`. |
| 68 |
|
| 69 |
## `fmt` |
| 70 |
|
| 71 |
Rust uses the `Debug` trait for automatic printing events in several parts of |
| 72 |
the standard library. This module provides wrapper types which forward their |
| 73 |
`Debug` implementation to a specified other formatting trait. It also implements |
| 74 |
extension methods on all types that have format trait implementations to wrap |
| 75 |
them in the corresponding shim type. |
| 76 |
|
| 77 |
```rust |
| 78 |
use wyz::fmt::FmtForward as _; |
| 79 |
|
| 80 |
let val = 6; |
| 81 |
let addr = &val as *const i32; |
| 82 |
println!("{:?}", addr.fmt_pointer()); |
| 83 |
``` |
| 84 |
|
| 85 |
This snippet uses the `Debug` format template, but will print the `Pointer` |
| 86 |
implementation of `*const i32`. |
| 87 |
|
| 88 |
This is useful for fitting your values into an error-handling framework that |
| 89 |
only uses `Debug`, such as the `fn main() -> Result` program layout. |
| 90 |
|
| 91 |
## `pipe` |
| 92 |
|
| 93 |
Rust does not permit universal suffix-position function call syntax. That is, |
| 94 |
you can *always* call a function with `Scope::name(arguments…)`, but only *some* |
| 95 |
functions can be called as `first_arg.name(other_args…)`. Working in “data |
| 96 |
pipelines” – flows where the return value of one function is passed directly as |
| 97 |
the first argument to the next – is common enough in our field that it has a |
| 98 |
name in languages that support it: *method chaining*. A *method* is a function |
| 99 |
that the language considers to be treated specially in regards to only its |
| 100 |
first argument, and permits changing the abstract token series |
| 101 |
`function arg1 args…` into the series `arg1 function args…`. |
| 102 |
|
| 103 |
Rust restricts that order transformation to only functions defined in scope for |
| 104 |
some type (either `impl Type` or `impl Trait for Type` blocks) and that take a |
| 105 |
first argument named `self`. |
| 106 |
|
| 107 |
Other languages permit calling *any* function, regardless of its definition site |
| 108 |
in source code, in this manner, as long as the first argument is of the correct |
| 109 |
type for the first parameter of the function. |
| 110 |
|
| 111 |
In languages like F♯ and Elixir, this uses the call operator `|>` rather than |
| 112 |
the C++ family’s `.` caller. This operator is pronounced `pipe`. |
| 113 |
|
| 114 |
Rust does not have a pipe operator. The dot-caller is restricted to only the |
| 115 |
implementation blocks listed above, and this is not likely to change because it |
| 116 |
also performs limited type transformation operations in order to find a name |
| 117 |
that fits. |
| 118 |
|
| 119 |
This module provides a `Pipe` trait whose method, `pipe`, passes its `self` |
| 120 |
first argument as the argument to its second-order function: |
| 121 |
|
| 122 |
```rust |
| 123 |
use wyz::pipe::Pipe; |
| 124 |
|
| 125 |
let final = 5 |
| 126 |
.pipe(|a| a + 10) |
| 127 |
.pipe(|a| a * 2); |
| 128 |
|
| 129 |
assert_eq!(final, 30); |
| 130 |
``` |
| 131 |
|
| 132 |
Without language-level syntax support, piping into closures always requires |
| 133 |
restating the argument, and functions cannot curry the argument they receive |
| 134 |
from `pipe` and arguments from the environment in the manner that dot-called |
| 135 |
methods can. |
| 136 |
|
| 137 |
```rust |
| 138 |
fn fma(a: i32, b: i32, c: i32) -> i32 { |
| 139 |
(a * b) + c |
| 140 |
} |
| 141 |
5.pipe(|a| fma(a, 2, 3)); |
| 142 |
|
| 143 |
let fma_2_3 = |a| fma(a, 2, 3); |
| 144 |
5.pipe(fma_2_3); |
| 145 |
``` |
| 146 |
|
| 147 |
These are the only ways to express `5 |> fma(2, 3)`. |
| 148 |
|
| 149 |
Sorry. |
| 150 |
|
| 151 |
Bug the language team. |
| 152 |
|
| 153 |
## `tap` |
| 154 |
|
| 155 |
Tapping is a cousin operation to piping, except that rather than pass the |
| 156 |
receiver by *value* into some function, and return the result of that function, |
| 157 |
it passes a *borrow* of a value into a function, and then returns the original |
| 158 |
value. |
| 159 |
|
| 160 |
It is useful for inserting an operation into an expression without changing the |
| 161 |
overall state (type or value) of the expression. |
| 162 |
|
| 163 |
```rust |
| 164 |
use wyz::tap::Tap; |
| 165 |
|
| 166 |
let result = complex_value() |
| 167 |
.tap(|v| log::info!("First stage: {}", v)) |
| 168 |
.transform(other, args) |
| 169 |
.tap(|v| log::info!("Second stage: {}", v)); |
| 170 |
``` |
| 171 |
|
| 172 |
The tap calls have no effect on the expression into which they are placed, |
| 173 |
except to induce side effects somewhere else in the system. Commenting out the |
| 174 |
two `.tap` calls does not change anything about `complex_value()`, `.transform`, |
| 175 |
or `result`; it only removes the log statements. |
| 176 |
|
| 177 |
This enables easily inserting or removing inspection points in an expression |
| 178 |
without otherwise altering it. |
| 179 |
|
| 180 |
[codecov]: https://codecov.io/gh/myrrlyn/wyz "Code Coverage" |
| 181 |
[codecov_img]: https://img.shields.io/codecov/c/github/myrrlyn/wyz.svg?logo=codecov "Code Coverage Display" |
| 182 |
[crate]: https://crates.io/crates/wyz "Crate Link" |
| 183 |
[crate_img]: https://img.shields.io/crates/v/wyz.svg?logo=rust "Crate Page" |
| 184 |
[docs]: https://docs.rs/wyz "Documentation" |
| 185 |
[docs_img]: https://docs.rs/wyz/badge.svg "Documentation Display" |
| 186 |
[downloads_img]: https://img.shields.io/crates/dv/wyz.svg?logo=rust "Crate Downloads" |
| 187 |
[license_file]: https://github.com/myrrlyn/wyz/blob/master/LICENSE.txt "License File" |
| 188 |
[license_img]: https://img.shields.io/crates/l/wyz.svg "License Display" |
| 189 |
[loc]: https://github.com/myrrlyn/wyz "Repository" |
| 190 |
[loc_img]: https://tokei.rs/b1/github/myrrlyn/wyz?category=code "Repository Size" |
| 191 |
[travis]: https://travis-ci.org/myrrlyn/wyz "Travis CI" |
| 192 |
[travis_img]: https://img.shields.io/travis/myrrlyn/wyz.svg?logo=travis "Travis CI Display" |