veilid/veilid-server/src/tools.rs

54 lines
2.2 KiB
Rust
Raw Normal View History

2023-06-08 18:07:09 +00:00
pub use cfg_if::*;
pub use color_eyre::eyre::{bail, ensure, eyre, Result as EyreResult, WrapErr};
pub use core::future::Future;
pub use parking_lot::*;
pub use tracing::*;
2022-06-28 03:46:29 +00:00
cfg_if! {
if #[cfg(feature="rt-async-std")] {
2023-06-07 21:39:10 +00:00
// pub use async_std::task::JoinHandle;
2022-06-28 03:46:29 +00:00
pub use async_std::net::TcpListener;
2023-06-07 21:39:10 +00:00
pub use async_std::net::TcpStream;
pub use async_std::io::BufReader;
2022-06-28 03:46:29 +00:00
//pub use async_std::future::TimeoutError;
2023-06-07 21:39:10 +00:00
//pub fn spawn_detached<F: Future<Output = T> + Send + 'static, T: Send + 'static>(f: F) -> JoinHandle<T> {
//async_std::task::spawn(f)
//}
// pub fn spawn_local<F: Future<Output = T> + 'static, T: 'static>(f: F) -> JoinHandle<T> {
// async_std::task::spawn_local(f)
// }
2022-06-29 14:13:49 +00:00
// pub fn spawn_detached_local<F: Future<Output = T> + 'static, T: 'static>(f: F) {
// let _ = async_std::task::spawn_local(f);
// }
2023-06-07 21:39:10 +00:00
//pub use async_std::task::sleep;
//pub use async_std::future::timeout;
2022-06-28 03:46:29 +00:00
pub fn block_on<F: Future<Output = T>, T>(f: F) -> T {
async_std::task::block_on(f)
}
} else if #[cfg(feature="rt-tokio")] {
2023-06-07 21:39:10 +00:00
//pub use tokio::task::JoinHandle;
2022-06-28 03:46:29 +00:00
pub use tokio::net::TcpListener;
2023-06-07 21:39:10 +00:00
pub use tokio::net::TcpStream;
pub use tokio::io::BufReader;
2022-06-28 03:46:29 +00:00
//pub use tokio_util::compat::*;
2022-07-10 21:36:50 +00:00
//pub use tokio::time::error::Elapsed as TimeoutError;
2023-06-07 21:39:10 +00:00
//pub fn spawn_detached<F: Future<Output = T> + Send + 'static, T: Send + 'static>(f: F) -> JoinHandle<T> {
//tokio::task::spawn(f)
//}
// pub fn spawn_local<F: Future<Output = T> + 'static, T: 'static>(f: F) -> JoinHandle<T> {
// tokio::task::spawn_local(f)
// }
2022-06-29 14:13:49 +00:00
// pub fn spawn_detached_local<F: Future<Output = T> + 'static, T: 'static>(f: F) {
// let _ = tokio::task::spawn_local(f);
// }
2023-06-07 21:39:10 +00:00
//pub use tokio::time::sleep;
//pub use tokio::time::timeout;
2022-06-28 03:46:29 +00:00
pub fn block_on<F: Future<Output = T>, T>(f: F) -> T {
let rt = tokio::runtime::Runtime::new().unwrap();
let local = tokio::task::LocalSet::new();
local.block_on(&rt, f)
}
}
}