From d5768c4c787b51b5c3599cd285fc6be38b2ae8cb Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 13 Jul 2017 01:40:14 +0200 Subject: [PATCH] echo_bot: unify send() --- examples/echo_bot.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/examples/echo_bot.rs b/examples/echo_bot.rs index 03053db..4af8e8e 100644 --- a/examples/echo_bot.rs +++ b/examples/echo_bot.rs @@ -14,19 +14,22 @@ fn main() { let (sink, stream) = client.split(); let mut sink = Some(sink); - let done = stream.for_each(move |event| { + let mut send = move |stanza| { + sink = Some( + sink.take(). + expect("sink") + .send(stanza) + .wait() + .expect("sink.send") + ); + }; + let done = stream.for_each(|event| { let result: Box> = match event { ClientEvent::Online => { println!("Online!"); let presence = make_presence(); - sink = Some( - sink.take(). - expect("sink") - .send(presence) - .wait() - .expect("sink.send") - ); + send(presence); Box::new( future::ok(()) ) @@ -42,13 +45,7 @@ fn main() { match (from.as_ref(), body) { (Some(from), Some(body)) => { let reply = make_reply(from, body); - sink = Some( - sink.take(). - expect("sink") - .send(reply) - .wait() - .expect("sink.send") - ); + send(reply); }, _ => (), };