tools: add tokio::main() replacement

to deal with block_on() not allowing blocking()

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-09-03 11:16:29 +02:00
parent 66fbf5bad0
commit daef93f481
3 changed files with 24 additions and 9 deletions

View File

@ -22,6 +22,7 @@ pub mod borrow;
pub mod daemon;
pub mod fs;
pub mod futures;
pub mod runtime;
pub mod ticket;
pub mod timer;
pub mod tty;

20
src/tools/runtime.rs Normal file
View File

@ -0,0 +1,20 @@
//! Helpers for quirks of the current tokio runtime.
use std::future::Future;
pub fn main<F, T>(fut: F) -> T
where
F: Future<Output = T> + Send + 'static,
T: std::fmt::Debug + Send + 'static,
{
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let (tx, rx) = tokio::sync::oneshot::channel();
tokio::spawn(async move {
tx.send(fut.await).unwrap()
});
rx.await.unwrap()
})
}

View File

@ -52,16 +52,10 @@ mod test {
#[test]
fn test_wrapped_stream_reader() -> Result<(), Error> {
let rt = tokio::runtime::Runtime::new()?;
// This cannot be used currently, because it doesn't permit blocking() annotations:
//rt.block_on(run_wrapped_stream_reader_test());
rt.spawn(async {
run_wrapped_stream_reader_test().await.unwrap();
});
rt.shutdown_on_idle();
Ok(())
crate::tools::runtime::main(async {
run_wrapped_stream_reader_test().await
})
}
struct DummyReader(usize);