1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-23 15:16:54 -05:00
denoland-deno/cli/tokio_util.rs

30 lines
695 B
Rust
Raw Normal View History

2020-01-02 15:13:47 -05:00
// 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,
{
2020-01-30 11:28:51 -05:00
let mut rt = tokio::runtime::Builder::new()
.basic_scheduler()
.enable_io()
.enable_time()
2019-12-30 08:57:17 -05:00
.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() }
}