title: Select from a list of sockets using futures
body: <p>I am trying out the yet-unstable async-await syntax in nightly Rust 1.38 with <code>futures-preview = "0.3.0-alpha.16"</code> and <code>runtime = "0.3.0-alpha.6"</code>. It feels really cool, but the docs are (yet) scarce and I got stuck.</p>
  
  <p>To go a bit beyond the <a href="https://docs.rs/crate/runtime/0.3.0-alpha.6/source/examples/" rel="noreferrer">basic examples</a> I would like to create an app that:</p>
  
  <ol>
  <li>Accepts TCP connections on a given port;</li>
  <li>Broadcasts all the data received from any connection to all active connections.</li>
  </ol>
  
  <p>Existing docs and examples got me this far:</p>
  
  <pre class="lang-rust prettyprint-override"><code>#![feature(async_await)]
  #![feature(async_closure)]
  
  use futures::{
      prelude::*,
      select,
      future::select_all,
      io::{ReadHalf, WriteHalf, Read},
  };
  
  use runtime::net::{TcpListener, TcpStream};
  
  use std::io;
  
  async fn read_stream(mut reader: ReadHalf&lt;TcpStream&gt;) -&gt; (ReadHalf&lt;TcpStream&gt;, io::Result&lt;Box&lt;[u8]&gt;&gt;) {
      let mut buffer: Vec&lt;u8&gt; = vec![0; 1024];
      match reader.read(&amp;mut buffer).await {
          Ok(len) =&gt; {
              buffer.truncate(len);
              (reader, Ok(buffer.into_boxed_slice()))
          },
          Err(err) =&gt; (reader, Err(err)),
      }
  }
  
  #[runtime::main]
  async fn main() -&gt; std::io::Result&lt;()&gt; {
      let mut listener = TcpListener::bind("127.0.0.1:8080")?;
      println!("Listening on {}", listener.local_addr()?);
  
      let mut incoming = listener.incoming().fuse();
      let mut writers: Vec&lt;WriteHalf&lt;TcpStream&gt;&gt; = vec![];
      let mut reads = vec![];
  
      loop {
          select! {
              maybe_stream = incoming.select_next_some() =&gt; {
                  let (mut reader, writer) = maybe_stream?.split();
                  writers.push(writer);
                  reads.push(read_stream(reader).fuse());
              },
              maybe_read = select_all(reads.iter()) =&gt; {
                  match maybe_read {
                      (reader, Ok(data)) =&gt; {
                          for writer in writers {
                              writer.write_all(data).await.ok(); // Ignore errors here
                          }
                          reads.push(read_stream(reader).fuse());
                      },
                      (reader, Err(err)) =&gt; {
                          let reader_addr = reader.peer_addr().unwrap();
                          writers.retain(|writer| writer.peer_addr().unwrap() != reader_addr);
                      },
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>This fails with:</p>
  
  <pre class="lang-none prettyprint-override"><code>error: recursion limit reached while expanding the macro `$crate::dispatch`
    --&gt; src/main.rs:36:9
     |
  36 | /         select! {
  37 | |             maybe_stream = incoming.select_next_some() =&gt; {
  38 | |                 let (mut reader, writer) = maybe_stream?.split();
  39 | |                 writers.push(writer);
  ...  |
  55 | |             }
  56 | |         }
     | |_________^
     |
     = help: consider adding a `#![recursion_limit="128"]` attribute to your crate
     = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
  </code></pre>
  
  <p>This is very confusing. Maybe I am using <code>select_all()</code> in a wrong way? Any help in making it work is appreciated!</p>
  
  <p>For completeness, my <code>Cargo.toml</code>:</p>
  
  <pre><code>[package]
  name = "async-test"
  version = "0.1.0"
  authors = ["xxx"]
  edition = "2018"
  
  [dependencies]
  runtime = "0.3.0-alpha.6"
  futures-preview = { version = "=0.3.0-alpha.16", features = ["async-await", "nightly"] }
  </code></pre>