1

Is there a better way to do something like below?

use futures::channel::oneshot; // 0.3.4
use std::thread;

pub async fn spawn<Y>(f: impl Fn() -> Y + Send + Sync + 'static) -> Y
where
    Y: Send + 'static,
{
    let (sender, receiver) = oneshot::channel::<Y>();
    thread::spawn(move || sender.send(f()));
    receiver.await.unwrap()
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 1
    Welcome to Stack Overflow! It looks like your question might be answered by the answers of [What is the best approach to encapsulate blocking I/O in future-rs?](https://stackoverflow.com/q/41932137/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Apr 13 '20 at 18:07
  • @Shepmaster thanks. ```tokio::task::spawn_blocking``` is what I was looking for. – David Taylor Apr 14 '20 at 12:00
  • 1
    Marking as a duplicate of [What is the best approach to encapsulate blocking I/O in future-rs?](https://stackoverflow.com/questions/41932137/what-is-the-best-approach-to-encapsulate-blocking-i-o-in-future-rs) – E_net4 Apr 14 '20 at 13:00

1 Answers1

0

tokio::task::spawn_blocking is what I was looking for.

  • Please do not answer duplicate questions. Instead, you can choose to mark them as duplicates (we have already done so at this point). – Shepmaster Apr 14 '20 at 13:19