pub trait PipeRef {
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where
R: 'a + Sized,
{ ... }
fn pipe_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where
R: 'a + Sized,
{ ... }
}
Expand description
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
andBorrowMut
traits - the
AsRef
andAsMut
traits - the
Deref
andDerefMut
traits
Provided methods
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 ofself
.func
: Any function, which receives&self
as its first and only parameter. This function may return any value, including a borrow ofself
, as long as it has an equal or greater lifetime thanself
.
Type Parameters
R
: The return value offunc
. This must have a lifetime of at least'a
, up to and including'static
and unconstrained (borrow-less value).
Lifetimes
'a
: The lifetime of theself
value..pipe_ref
borrowsself
for the duration'a
, and extends it through the return value offunc
.
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 ofself
.func
: Any function, which receives&mut self
as its first and only parameter. This funtion may return any value, including a borrow ofself
, as long as it has an equal or greater lifetime thanself
.
Type Parameters
R
: The return value offunc
. This must have a lifetime of at least'a
, up to and including'static
and unconstrained (borrow-less value).
Lifetimes
'a
: The lifetime of theself
value..pipe_mut
borrowsself
for the duration'a
, and extends it through the return value offunc
.