2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-02-03 18:08:44 -05:00
|
|
|
use futures::Future;
|
2019-04-23 16:27:44 -04:00
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
// TODO(ry) rename to run_local ?
|
|
|
|
pub fn run_basic<F, R>(future: F) -> R
|
2019-03-14 19:17:52 -04:00
|
|
|
where
|
2020-02-03 18:08:44 -05:00
|
|
|
F: std::future::Future<Output = R> + 'static,
|
2019-03-14 19:17:52 -04:00
|
|
|
{
|
2020-01-30 11:28:51 -05:00
|
|
|
let mut rt = tokio::runtime::Builder::new()
|
2020-02-03 18:08:44 -05:00
|
|
|
.basic_scheduler()
|
|
|
|
.enable_io()
|
|
|
|
.enable_time()
|
2019-12-30 08:57:17 -05:00
|
|
|
.build()
|
2020-02-03 18:08:44 -05:00
|
|
|
.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() }
|
2019-04-14 16:07:24 -04:00
|
|
|
}
|