title: How do I stream a hyper Request's Body from a slow-processing side thread that produces chunks of data?
body: <p>I have a program which generates data slowly (we can say it's computationally intensive, like computing digits of pi). It produces a <em>lot</em> of data; each response can be 1GiB, will not fit in memory, and <strong>must</strong> be generated on demand. I'm using hyper to write a web service to generate the content when requested.</p>
  
  <p>Let's skip the boilerplate (<code>service_fn</code>, <code>Server::bind</code>).</p>
  
  <p>The API which generates the data slowly might be something like</p>
  
  <pre><code>use std::io;
  
  impl SlowData {
      fn new(initial: &amp;str) -&gt; SlowData {
          unimplemented!()
      }
  
      fn next_block(&amp;self) -&gt; io::Result&lt;&amp;[u8]&gt; {
          unimplemented!()
      }
  }
  
  type ResponseFuture = Box&lt;Future&lt;Item = Response, Error = GenericError&gt; + Send&gt;;
  
  fn run(req: Request) -&gt; ResponseFuture {
      // spawn a thread and:
      // initialize the generator
      // SlowData::new(&amp;req.uri().path());
  
      // spawn a thread and call slow.next_block() until len()==0
      // each byte which comes from next_block should go to the client
      // as part of the Body
  }
  </code></pre>
  
  <p>Note that <code>SlowData::new</code> is also computationally intensive.</p>
  
  <p>Optimally, we'd minimize the copies and send that <code>&amp;[u8]</code> directly to hyper without having to copy it into a <code>Vec</code> or something.</p>
  
  <p>How do I fulfill a hyper Request's body from a side thread?</p>