Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait Pipe
- where
- Self: Sized,
- {
- fn pipe<'a, F, R>(&'a self, f: F) -> R
- where
- F: Fn(&'a Self) -> R,
- {
- f(self)
- }
- fn pipe_mut<'a, F>(&'a mut self, f: F) -> &'a mut Self
- where
- F: Fn(&mut Self),
- {
- f(self);
- self
- }
- fn pipe_move<F, R>(self, f: F) -> R
- where
- F: Fn(Self) -> R,
- {
- f(self)
- }
- }
- impl<T> Pipe for T {}
- fn main() {
- assert!(1.pipe_move(inc).pipe(|x| x * x) == 4);
- let mut v = vec![240, 159, 146, 150];
- assert_eq!(v.pipe(std::str::from_utf8), Ok("💖"));
- v.pipe_mut(append_one).pipe_mut(append_one);
- assert_eq!(v, vec![240, 159, 146, 150, 1, 1]);
- }
- fn inc(i: i64) -> i64 {
- i + 1
- }
- fn append_one(v: &mut Vec<u8>) {
- v.push(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement