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:
		@ -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
									
								
							
							
						
						
									
										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]
 | 
			
		||||
    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);
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user