pub trait TapAsRef<T: ?Sized>: Sized {
fn tap_ref<F, R>(self, func: F) -> Self
where
Self: AsRef<T>,
F: FnOnce(&T) -> R,
{ ... }
fn tap_ref_dbg<F, R>(self, func: F) -> Self
where
Self: AsRef<T>,
F: FnOnce(&T) -> R,
{ ... }
fn tap_ref_mut<F, R>(self, func: F) -> Self
where
Self: AsMut<T>,
F: FnOnce(&mut T) -> R,
{ ... }
fn tap_ref_mut_dbg<F, R>(self, func: F) -> Self
where
Self: AsMut<T>,
F: FnOnce(&mut T) -> R,
{ ... }
}
Expand description
Referential Tap
This trait runs the AsRef
or AsMut
trait on the caller, and passes the
referred output of it to the action. This permits passing methods defined on a
member value’s type to the tap of an aggregate value.
Due to restrictions in the Rust type system, using these taps on types which
have multiple implementations of AsRef
or AsMut
must specify which
implementation is desired by setting the type of the receiver of the called
function.
Examples
use wyz::tap::{Tap, TapAsRef};
let v = vec![5, 1, 4, 2, 3]
.tap_ref_mut(<[i32]>::sort)
.tap_mut(|v| v.reverse());
assert_eq!(&v, &[5, 4, 3, 2, 1]);
This example demonstrates disambiguating among multiple implementations.
use wyz::tap::TapAsRef;
struct Example {
a: [u8; 8],
b: [u16; 4],
}
impl AsRef<[u8]> for Example {
fn as_ref(&self) -> &[u8] {
&self.a
}
}
impl AsRef<[u16]> for Example {
fn as_ref(&self) -> &[u16] {
&self.b
}
}
impl AsMut<[u8]> for Example {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.a
}
}
impl AsMut<[u16]> for Example {
fn as_mut(&mut self) -> &mut [u16] {
&mut self.b
}
}
let mut sum = 0usize;
let e = Example {
a: [0, 1, 2, 3, 4, 5, 6, 7],
b: [8, 9, 10, 11],
}
.tap_ref(|a: &[u8]| sum += a.iter().sum::<u8>() as usize)
.tap_ref(|b: &[u16]| sum += b.iter().sum::<u16>() as usize)
.tap_ref_mut(|a: &mut [u8]| a.iter_mut().for_each(|e| *e *= 2))
.tap_ref_mut(|b: &mut [u16]| b.iter_mut().for_each(|e| *e *= 2));
assert_eq!(sum, 66);
assert_eq!(e.a, [0, 2, 4, 6, 8, 10, 12, 14]);
assert_eq!(e.b, [16, 18, 20, 22]);
Provided methods
Provides immutable access to the reference for inspection.
fn tap_ref_dbg<F, R>(self, func: F) -> Self where
Self: AsRef<T>,
F: FnOnce(&T) -> R,
fn tap_ref_dbg<F, R>(self, func: F) -> Self where
Self: AsRef<T>,
F: FnOnce(&T) -> R,
Calls tap_ref
in debug builds, and does nothing in release builds.
fn tap_ref_mut<F, R>(self, func: F) -> Self where
Self: AsMut<T>,
F: FnOnce(&mut T) -> R,
fn tap_ref_mut<F, R>(self, func: F) -> Self where
Self: AsMut<T>,
F: FnOnce(&mut T) -> R,
Provides mutable access to the reference for modification.
fn tap_ref_mut_dbg<F, R>(self, func: F) -> Self where
Self: AsMut<T>,
F: FnOnce(&mut T) -> R,
fn tap_ref_mut_dbg<F, R>(self, func: F) -> Self where
Self: AsMut<T>,
F: FnOnce(&mut T) -> R,
Calls tap_ref_mut
in debug builds, and does nothing in release builds.