title: Dedicated rayon pool to submit work and asynchronously wait for completion
body: <p>I have a <code>rayon::ThreadPool</code> which I want to use to perform CPU bound tasks outside of Tokio's runtime context. The CPU bound tasks are synchronous tasks.</p>
  <p>The problem is <code>spawn</code> requires the closure to be <code>'static</code> but I want to use the borrowed data instead of making owned copies.</p>
  <p>Initially I thought <code>scope</code> would work (and the code compiles too), but it seems that it will block until the closure completes - which will defeat my purpose of using this pool i.e. to not block the tokio runtime.</p>
  <p>How can I achieve this with rayon or any other threadpool implementation?</p>
  <pre class="lang-rust prettyprint-override"><code>pub struct TaskPool {
      pool: ThreadPool,
  }
  
  impl TaskPool {
      pub fn new(num_threads: usize) -&gt; Self {
          Self {
              pool: ThreadPoolBuilder::new()
                  .num_threads(num_threads)
                  .build()
                  .unwrap(),
          }
      }
  
      pub async fn verify(&amp;self, hash: &amp;[u8], data: &amp;[u8]) -&gt; bool {
          let (tx, rx) = oneshot::channel();
          self.pool.spawn(|| {
              let ok = hash == calc_sha1(data);
              tx.send(ok).unwrap();
          });
          rx.await.unwrap()
      }
  }
  </code></pre>
  <p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=be7a7f77374129a9ae4b01871795282f" rel="nofollow noreferrer">playground</a></p>