summaryrefslogtreecommitdiff
path: root/cli/tokio_util.rs
blob: 6ffc57b1a6bf3e53744be2e6cf8ff78e6e5198e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use futures::Future;

// TODO(ry) rename to run_local ?
pub fn run_basic<F, R>(future: F) -> R
where
  F: std::future::Future<Output = R> + 'static,
{
  let mut rt = tokio::runtime::Builder::new()
    .basic_scheduler()
    .enable_io()
    .enable_time()
    .build()
    .unwrap();
  rt.block_on(future)
}

pub fn spawn_thread<F, R>(f: F) -> impl Future<Output = R>
where
  F: 'static + Send + FnOnce() -> R,
  R: 'static + Send,
{
  let (sender, receiver) = tokio::sync::oneshot::channel::<R>();
  std::thread::spawn(move || {
    let result = f();
    sender.send(result)
  });
  async { receiver.await.unwrap() }
}