diff --git a/src/tools.rs b/src/tools.rs index e1909364..6346a136 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -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; diff --git a/src/tools/runtime.rs b/src/tools/runtime.rs new file mode 100644 index 00000000..6d944501 --- /dev/null +++ b/src/tools/runtime.rs @@ -0,0 +1,20 @@ +//! Helpers for quirks of the current tokio runtime. + +use std::future::Future; + +pub fn main(fut: F) -> T +where + F: Future + 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() + }) +} diff --git a/src/tools/wrapped_reader_stream.rs b/src/tools/wrapped_reader_stream.rs index 029b546e..100169a6 100644 --- a/src/tools/wrapped_reader_stream.rs +++ b/src/tools/wrapped_reader_stream.rs @@ -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);