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:
parent
66fbf5bad0
commit
daef93f481
@ -22,6 +22,7 @@ pub mod borrow;
|
|||||||
pub mod daemon;
|
pub mod daemon;
|
||||||
pub mod fs;
|
pub mod fs;
|
||||||
pub mod futures;
|
pub mod futures;
|
||||||
|
pub mod runtime;
|
||||||
pub mod ticket;
|
pub mod ticket;
|
||||||
pub mod timer;
|
pub mod timer;
|
||||||
pub mod tty;
|
pub mod tty;
|
||||||
|
20
src/tools/runtime.rs
Normal file
20
src/tools/runtime.rs
Normal 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()
|
||||||
|
})
|
||||||
|
}
|
@ -52,16 +52,10 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_wrapped_stream_reader() -> Result<(), Error> {
|
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:
|
// This cannot be used currently, because it doesn't permit blocking() annotations:
|
||||||
//rt.block_on(run_wrapped_stream_reader_test());
|
crate::tools::runtime::main(async {
|
||||||
|
run_wrapped_stream_reader_test().await
|
||||||
rt.spawn(async {
|
})
|
||||||
run_wrapped_stream_reader_test().await.unwrap();
|
|
||||||
});
|
|
||||||
rt.shutdown_on_idle();
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DummyReader(usize);
|
struct DummyReader(usize);
|
||||||
|
Loading…
Reference in New Issue
Block a user