title: How to read subprocess output asynchronously
body: <p>I want to implement a <code>futures::Stream</code> for reading and parsing the standard output of a child subprocess.</p>
  
  <p>What I'm doing at the moment:</p>
  
  <ul>
  <li><p>spawn subprocess and obtain its stdout via <code>std::process</code> methods: <code>let child = Command::new(...).stdout(Stdio.pipe()).spawn().expect(...)</code></p></li>
  <li><p>add <code>AsyncRead</code> and <code>BufRead</code> to stdout:</p>
  
  <pre><code>let stdout = BufReader::new(tokio_io::io::AllowStdIo::new(
      child.stdout.expect("Failed to open stdout"),
  ));
  </code></pre></li>
  <li><p>declare a wrapper struct for stdout:</p>
  
  <pre><code>struct MyStream&lt;Io: AsyncRead + BufRead&gt; {
      io: Io,
  }
  </code></pre></li>
  <li><p>implement <code>Stream</code>:</p>
  
  <pre><code>impl&lt;Io: AsyncRead + BufRead&gt; Stream for MyStream&lt;Io&gt; {
      type Item = Message;
      type Error = Error;
  
      fn poll(&amp;mut self) -&gt; Poll&lt;Option&lt;Message&gt;, Error&gt; {
          let mut line = String::new();
          let n = try_nb!(self.io.read_line(&amp;mut line));
          if n == 0 {
              return Ok(None.into());
          }
          //...read &amp; parse further
      }
  }
  </code></pre></li>
  </ul>
  
  <p>The problem is that <code>AllowStdIo</code> doesn't make <code>ChildStdout</code> magically asynchronous and the <code>self.io.read_line</code> call still blocks.</p>
  
  <p>I guess I need to pass something different instead of <code>Stdio::pipe()</code> to have it asynchronous, but what? Or is there a different solution for that?</p>
  
  <p>This question is different from <a href="https://stackoverflow.com/questions/41932137/what-is-the-best-approach-to-encapsulate-blocking-i-o-in-future-rs">What is the best approach to encapsulate blocking I/O in future-rs?</a> because I  want to get asynchronous I/O for the specific case of a subprocess and not solve the problem of encapsulation of synchronous I/O.</p>
  
  <p>Update: I'm using <code>tokio = "0.1.3"</code> to leverage its runtime feature and using <code>tokio-process</code> is not an option at the moment (<a href="https://github.com/alexcrichton/tokio-process/issues/27" rel="nofollow noreferrer">https://github.com/alexcrichton/tokio-process/issues/27</a>)</p>