1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
/*! Pipe objects into functions, even those not available for dot-call.
Rust restricts the `.method()` call syntax to be available only on functions
defined in `impl [Trait for] Type` blocks, with language-blessed `self`
receivers.
This module allows any function to be `.call()`ed, with as little overhead as
is possible in the language.
# Examples
```rust
use wyz::pipe::*;
fn double(x: i32) -> i32 {
x * 2
}
fn double_ref(x: &i32) -> i32 {
*x * 2
}
assert_eq!(5.pipe(double), 10);
assert_eq!(5.pipe_ref(double_ref), 10);
```
Rust’s automatic de/reference chasing only works for the signatures Rust already
permits in method-call syntax; the `Pipe` trait provides methods for certain
reference conversion chasing, but cannot otherwise take advantage of the
language builtin support for such behavior on ordinary methods.
Petition for a `|>` operator; sorry.
!*/
use core::{
borrow::{
Borrow,
BorrowMut,
},
ops::{
Deref,
DerefMut,
},
};
/** Permit suffixed call of any function on a value.
This trait provides a verbose, ugly version of the functional-language operator
`|>`. For any value, calling `.pipe(some_function)` translates to
`some_function(value)`.
Because it takes `self` by value, this trait is only implemented on `Sized`
types.
**/
pub trait Pipe: Sized {
/// Pipes a value into a function that cannot ordinarily be called in suffix
/// position.
///
/// # Parameters
///
/// - `self`: Any value
/// - `func`: Any function, which will receive `self` as its first and only
/// parameter. The return value of this function is then returned from
/// `pipe`.
///
/// Because this is a library function, not a language feature, it is not
/// able to use the method-call shorthand of `function(...other_params)`. A
/// suffix function with other params must be called as a closure:
///
/// ```rust
/// use wyz::pipe::Pipe;
///
/// fn add(a: i32, b: i32) -> i32 { a + b }
///
/// assert_eq!(5.pipe(|a| add(a, 2)), 7);
/// ```
///
/// This is *more* verbose than calling the function in prefix position; its
/// only value is for fitting into suffix-call chains.
///
/// The `.p` method is a shorthand alias for `.pipe`.
///
/// # Type Parameters
///
/// - `R`: The return value of `func`, which is then returned from `.pipe`.
/// This is placed as a function type parameter rather than a trait type
/// parameter so that it can be specified in ambiguous call sites.
#[inline(always)]
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where R: Sized {
func(self)
}
}
/** Referential piping.
The `Pipe` trait passes by value; the functions in this trait pass by reference.
As such, this trait is implemented on all types, not just `Sized`. The methods
in this trait operate by various mechanisms, including:
- normal `&`/`&mut` borrows
- the `Borrow` and `BorrowMut` traits
- the `AsRef` and `AsMut` traits
- the `Deref` and `DerefMut` traits
**/
pub trait PipeRef {
/// Pipes a reference into a function that cannot ordinarily be called in
/// suffix position.
///
/// # Parameters
///
/// - `&self`: A reference to any value. `.pipe_ref` takes it by reference
/// so that the `.pipe_ref` call will not artificially truncate the
/// lifetime of `self`.
/// - `func`: Any function, which receives `&self` as its first and only
/// parameter. This function may return any value, including a borrow of
/// `self`, as long as it has an equal or greater lifetime than `self`.
///
/// # Type Parameters
///
/// - `R`: The return value of `func`. This must have a lifetime of at least
/// `'a`, up to and including `'static` and unconstrained (borrow-less
/// value).
///
/// # Lifetimes
///
/// - `'a`: The lifetime of the `self` value. `.pipe_ref` borrows `self` for
/// the duration `'a`, and extends it through the return value of `func`.
#[inline(always)]
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a + Sized {
func(self)
}
/// Pipes a mutable reference into a function that cannot ordinarily be
/// called in suffix position.
///
/// # Parameters
///
/// - `&mut self`: A mutable reference to any value. `.pipe_mut` takes it by
/// reference so that the `.pipe_mut` call will not artificially truncate
/// the lifetime of `self`.
/// - `func`: Any function, which receives `&mut self` as its first and only
/// parameter. This funtion may return any value, including a borrow of
/// `self`, as long as it has an equal or greater lifetime than `self`.
///
/// # Type Parameters
///
/// - `R`: The return value of `func`. This must have a lifetime of at least
/// `'a`, up to and including `'static` and unconstrained (borrow-less
/// value).
///
/// # Lifetimes
///
/// - `'a`: The lifetime of the `self` value. `.pipe_mut` borrows `self` for
/// the duration `'a`, and extends it through the return value of `func`.
#[inline(always)]
fn pipe_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a + Sized {
func(self)
}
}
/// Calls the `Borrow` or `BorrowMut` traits before piping.
pub trait PipeBorrow {
/// Pipes a trait borrow into a function that cannot normally be called in
/// suffix position.
///
/// # Parameters
///
/// - `&self`: A borrow of the receiver. By automatically borrowing, this
/// pipe call ensures that the lifetime of the origin object is not
/// artificially truncated.
/// - `func`: A function which receives a trait-directed borrow of the
/// receiver’s value type. Before this function is called, `<Self as
/// Borrow<T>::borrow>` is called on the `self` reference, and the output
/// of that trait borrow is passed to `func`.
///
/// # Type Parameters
///
/// - `T`: The type to which `self` `Borrow`s. This is required to be
/// defined as a method type parameter so that you can disambiguate at
/// call time.
/// - `R`: The return type of `func`.
///
/// # Lifetimes
///
/// - `'a`: The lifetime of the `self` value. `.pipe_mut` borrows `self` for
/// the duration `'a`, and extends it through the return value of `func`.
#[inline(always)]
fn pipe_borrow<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where
Self: Borrow<T>,
T: 'a,
R: 'a + Sized,
{
func(Borrow::<T>::borrow(self))
}
/// Pipes a trait mutable borrow into a function that cannot normally be
/// called in suffix position.
///
/// # Parameters
///
/// - `&mut self`: A mutable borrow of the receiver. By automatically
/// borrowing, this pipe call ensures that the lifetime of the origin
/// object is not artificially truncated.
/// - `func`: A function which receives a trait-directed borrow of the
/// receiver’s value type. Before this function is called, `<Self as
/// BorrowMut<T>::borrow_mut>` is called on the `self` reference, and the
/// output of that trait borrow is passed to `func`.
///
/// # Type Parameters
///
/// - `T`: The type to which `self` borrows. This is required to be defined
/// as a method type parameter so that you can disambiguate at call time.
/// - `R`: The return type of `func`.
///
/// # Lifetimes
///
/// - `'a`: The lifetime of the `self` value. `.pipe_mut` borrows `self` for
/// the duration `'a`, and extends it through the return value of `func`.
#[inline(always)]
fn pipe_borrow_mut<'a, T, R>(
&'a mut self,
func: impl FnOnce(&'a mut T) -> R,
) -> R
where
Self: BorrowMut<T>,
T: 'a,
R: 'a + Sized,
{
func(BorrowMut::<T>::borrow_mut(self))
}
}
/// Calls the `AsRef` or `AsMut` traits before piping.
pub trait PipeAsRef {
/// Pipes a trait borrow into a function that cannot normally be called in
/// suffix position.
///
/// # Parameters
///
/// - `&self`: This borrows `self` for the same reasons as described in the
/// other methods.
/// - `func`: A function as described in the other methods. This receives
/// the result of `AsRef::<T>::as_ref`.
///
/// # Lifetimes
///
/// - `'a`: The lifetime of the `self` value. `.pipe_mut` borrows `self` for
/// the duration `'a`, and extends it through the return value of `func`.
#[inline(always)]
fn pipe_as_ref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where
Self: AsRef<T>,
T: 'a,
R: 'a + Sized,
{
func(AsRef::<T>::as_ref(self))
}
/// Pipes a trait mutable borrow into a function that cannot normally be
/// called in suffix position.
///
/// # Parameters
///
/// - `&mut self`: This borrows `self` for the same reasons as described in
/// the other methods.
/// - `func`: A function as described in the other methods. This receives
/// the result of `AsMut::<T>::as_mut`.
///
/// # Lifetimes
///
/// - `'a`: The lifetime of the `self` value. `.pipe_mut` borrows `self` for
/// the duration `'a`, and extends it through the return value of `func`.
#[inline(always)]
fn pipe_as_mut<'a, T, R>(
&'a mut self,
func: impl FnOnce(&'a mut T) -> R,
) -> R
where
Self: AsMut<T>,
T: 'a,
R: 'a + Sized,
{
func(AsMut::<T>::as_mut(self))
}
}
/// Calls the `Deref` or `DerefMut` traits before piping.
pub trait PipeDeref {
/// Pipes a dereference into a function that cannot normally be called in
/// suffix position.
///
/// # Parameters
///
/// - `&self`: This borrows `self` for the same reasons as described in the
/// - `func`: A function as described in the other methods. This receives
/// the result of `Deref::deref`.
///
/// # Lifetimes
///
/// - `'a`: The lifetime of the `self` value. `.pipe_mut` borrows `self` for
/// the duration `'a`, and extends it through the return value of `func`.
#[inline(always)]
fn pipe_deref<'a, R>(
&'a self,
func: impl FnOnce(&'a <Self as Deref>::Target) -> R,
) -> R
where
Self: Deref,
R: 'a + Sized,
{
func(Deref::deref(self))
}
/// Pipes a mutable dereference into a function that cannot normally be
/// called in suffix position.
///
/// # Parameters
///
/// - `&mut self`: This mutably borrows `self` for the same reasons as
/// described in the other methods.
/// - `func`: A function as described in the other methods. This receives
/// the result of `DerefMut::deref`.
///
/// # Lifetimes
///
/// - `'a`: The lifetime of the `self` value. `.pipe_mut` borrows `self` for
/// the duration `'a`, and extends it through the return value of `func`.
#[inline(always)]
fn pipe_deref_mut<'a, R>(
&'a mut self,
func: impl FnOnce(&'a mut <Self as Deref>::Target) -> R,
) -> R
where
Self: DerefMut,
R: 'a + Sized,
{
func(DerefMut::deref_mut(self))
}
}
impl<T: Sized> Pipe for T {
}
impl<T> PipeRef for T {
}
impl<T> PipeBorrow for T {
}
impl<T> PipeAsRef for T {
}
impl<T> PipeDeref for T {
}