Trait wyz::tap::TapBorrow [−][src]
pub trait TapBorrow<T: ?Sized>: Sized {
fn tap_borrow<F, R>(self, func: F) -> Self
where
Self: Borrow<T>,
F: FnOnce(&T) -> R,
R: Sized,
{ ... }
fn tap_borrow_dbg<F, R>(self, func: F) -> Self
where
Self: Borrow<T>,
F: FnOnce(&T) -> R,
R: Sized,
{ ... }
fn tap_borrow_mut<F, R>(self, func: F) -> Self
where
Self: BorrowMut<T>,
F: FnOnce(&mut T) -> R,
R: Sized,
{ ... }
fn tap_borrow_mut_dbg<F, R>(self, func: F) -> Self
where
Self: BorrowMut<T>,
F: FnOnce(&mut T) -> R,
R: Sized,
{ ... }
}Expand description
Borrowing Tap
This trait runs the Borrow or BorrowMut trait on the caller, and passes
the borrowed output of it to the action. This permits passing methods defined on
a supertype to the tap of a subtype.
Because a type may implement Borrow multiple times, the function passed to
.tap_borrow must specify its argument type. This can be done by providing the
name of an explicitly typed function, or by typing a closure’s argument.
Examples
use wyz::tap::{Tap, TapBorrow};
let v = vec![5, 1, 4, 2, 3]
.tap_borrow_mut(<[i32]>::sort)
.tap_mut(|v| v.reverse());
assert_eq!(&v, &[5, 4, 3, 2, 1]);tap_mut on a Vec<T> cannot call functions defined in impl [T];
tap_borrow_mut can, because Vec<T> implements Borrow<[T]>.
Provided methods
Provides immutable access to the borrow for inspection.
This calls <Self as Borrow<T>>::borrow on self, and calls func on
the resulting borrow.
Examples
use std::rc::Rc;
use wyz::tap::TapBorrow;
let mut len = 0;
let text = Rc::<str>::from("hello")
.tap_borrow(|s: &str| len = s.len());Calls tap_borrow in debug builds, and does nothing in release builds.
Provides mutable access to the borrow for modification.