From 6bdfa159a7082d9b7c441d06ba3c6ebca7bb668a Mon Sep 17 00:00:00 2001 From: Denis Date: Thu, 22 Mar 2018 18:02:01 +0100 Subject: Fix `connect` example for UDP (#241) Close #241 --- examples/connect.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/connect.rs b/examples/connect.rs index f3ea6970..f44a5c1e 100644 --- a/examples/connect.rs +++ b/examples/connect.rs @@ -198,24 +198,29 @@ mod udp { // All bytes from `stdin` will go to the `addr` specified in our // argument list. Like with TCP this is spawned concurrently - tokio::spawn(stdin.map(move |chunk| { + let forward_stdin = stdin.map(move |chunk| { (chunk, addr) }).forward(sink).then(|result| { if let Err(e) = result { panic!("failed to write to socket: {}", e) } Ok(()) - })); + }); // With UDP we could receive data from any source, so filter out // anything coming from a different address - Box::new(stream.filter_map(move |(chunk, src)| { + let receive = stream.filter_map(move |(chunk, src)| { if src == addr { Some(chunk.into()) } else { None } - })) + }); + + Box::new(future::lazy(|| { + tokio::spawn(forward_stdin); + future::ok(receive) + }).flatten_stream()) } } -- cgit v1.2.3