Movatterモバイル変換


[0]ホーム

URL:


Docs.rs

Pipe

TraitPipe 

Source
pub trait Pipe {    // Provided methods    fnpipe<R>(self, func: implFnOnce(Self) -> R) -> Rwhere Self:Sized,             R:Sized { ... }    fnpipe_ref<'a, R>(&'a self, func: implFnOnce(&'a Self) -> R) -> Rwhere R: 'a +Sized { ... }    fnpipe_ref_mut<'a, R>(        &'a mut self,        func: implFnOnce(&'a mut Self) -> R,    ) -> Rwhere R: 'a +Sized { ... }    fnpipe_borrow<'a, B, R>(&'a self, func: implFnOnce(&'a B) -> R) -> Rwhere Self:Borrow<B>,             B: 'a + ?Sized,             R: 'a +Sized { ... }    fnpipe_borrow_mut<'a, B, R>(        &'a mut self,        func: implFnOnce(&'a mut B) -> R,    ) -> Rwhere Self:BorrowMut<B>,             B: 'a + ?Sized,             R: 'a +Sized { ... }    fnpipe_as_ref<'a, U, R>(&'a self, func: implFnOnce(&'a U) -> R) -> Rwhere Self:AsRef<U>,             U: 'a + ?Sized,             R: 'a +Sized { ... }    fnpipe_as_mut<'a, U, R>(        &'a mut self,        func: implFnOnce(&'a mut U) -> R,    ) -> Rwhere Self:AsMut<U>,             U: 'a + ?Sized,             R: 'a +Sized { ... }    fnpipe_deref<'a, T, R>(&'a self, func: implFnOnce(&'a T) -> R) -> Rwhere Self:Deref<Target = T>,             T: 'a + ?Sized,             R: 'a +Sized { ... }    fnpipe_deref_mut<'a, T, R>(        &'a mut self,        func: implFnOnce(&'a mut T) -> R,    ) -> Rwhere Self:DerefMut +Deref<Target = T>,             T: 'a + ?Sized,             R: 'a +Sized { ... }}
Expand description

Provides universal suffix-position call syntax for any function.

This trait provides methods that allow any closure or free function to be placedas a suffix-position call, by writing them as

fnnot_a_method(x: i32) -> u8 { xasu8 }receiver.pipe(not_a_method);

Piping into functions that take more than one argument still requires writing aclosure with ordinary function-call syntax. This is after all only a library,not a syntax transformation:

usetap::pipe::Pipe;fnadd(x: i32, y: i32) -> i32 { x + y }letout =5.pipe(|x| add(x,10));assert_eq!(out,15);

Like tapping, piping is useful for cases where you want to write a sequence ofprocessing steps without introducing many intermediate bindings, and your stepscontain functions which are not eligible for dot-call syntax.

The main difference between piping and tapping is that tapping always returnsthe value that was passed into the tap, while piping forwards the value into theeffect function, and returns the output of evaluating the effect function withthe value. Piping is a transformation, not merely an inspection or modification.

Provided Methods§

Source

fnpipe<R>(self, func: implFnOnce(Self) -> R) -> R
where Self:Sized, R:Sized,

Pipes by value. This is generally the method you want to use.

§Examples
usetap::pipe::Pipe;fntriple(x: i32) -> i64 {  xasi64 *3}assert_eq!(10.pipe(triple),30,);
Source

fnpipe_ref<'a, R>(&'a self, func: implFnOnce(&'a Self) -> R) -> R
where R: 'a +Sized,

Borrowsself and passes that borrow into the pipe function.

§Examples
usetap::pipe::Pipe;fnfold(v:&Vec<i32>) -> i32 {  v.iter().copied().sum()}letvec =vec![1,2,3,4,5];letsum = vec.pipe_ref(fold);assert_eq!(sum,15);assert_eq!(vec.len(),5);
Source

fnpipe_ref_mut<'a, R>(&'a mut self, func: implFnOnce(&'a mut Self) -> R) -> R
where R: 'a +Sized,

Mutably borrowsself and passes that borrow into the pipe function.

§Examples
usetap::pipe::Pipe;letmutvec =vec![false,true];letlast = vec  .pipe_ref_mut(Vec::pop)  .pipe(Option::unwrap);assert!(last);

Both of these functions are eligible for method-call syntax, and shouldnot be piped. Writing out non-trivial examples for these is a lot ofboilerplate.

Source

fnpipe_borrow<'a, B, R>(&'a self, func: implFnOnce(&'a B) -> R) -> R
where Self:Borrow<B>, B: 'a + ?Sized, R: 'a +Sized,

Borrowsself, then passesself.borrow() into the pipe function.

§Examples
usestd::borrow::Cow;usetap::pipe::Pipe;letlen = Cow::<'static, str>::from("hello, world")  .pipe_borrow(str::len);assert_eq!(len,12);
Source

fnpipe_borrow_mut<'a, B, R>( &'a mut self, func: implFnOnce(&'a mut B) -> R,) -> R
where Self:BorrowMut<B>, B: 'a + ?Sized, R: 'a +Sized,

Mutably borrowsself, then passesself.borrow_mut() into the pipefunction.

usetap::pipe::Pipe;letmuttxt ="hello, world".to_string();letptr = txt  .pipe_borrow_mut(str::as_mut_ptr);

This is a very contrived example, but theBorrowMut trait has almostno implementors in the standard library, and of the implementationsavailable, there are almost no methods that fit this API.

Source

fnpipe_as_ref<'a, U, R>(&'a self, func: implFnOnce(&'a U) -> R) -> R
where Self:AsRef<U>, U: 'a + ?Sized, R: 'a +Sized,

Borrowsself, then passesself.as_ref() into the pipe function.

Source

fnpipe_as_mut<'a, U, R>(&'a mut self, func: implFnOnce(&'a mut U) -> R) -> R
where Self:AsMut<U>, U: 'a + ?Sized, R: 'a +Sized,

Mutably borrowsself, then passesself.as_mut() into the pipefunction.

Source

fnpipe_deref<'a, T, R>(&'a self, func: implFnOnce(&'a T) -> R) -> R
where Self:Deref<Target = T>, T: 'a + ?Sized, R: 'a +Sized,

Borrowsself, then passesself.deref() into the pipe function.

Source

fnpipe_deref_mut<'a, T, R>( &'a mut self, func: implFnOnce(&'a mut T) -> R,) -> R
where Self:DerefMut +Deref<Target = T>, T: 'a + ?Sized, R: 'a +Sized,

Mutably borrowsself, then passesself.deref_mut() into the pipefunction.

Dyn Compatibility§

This trait isnotdyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T>Pipe for T
where T: ?Sized,


[8]ページ先頭

©2009-2025 Movatter.jp