use std::future::Future;
pub use compio::net::{TcpListener, TcpStream, ToSocketAddrsAsync as ToSocketAddrs};
pub use compio::time::{sleep, timeout};
pub type OwnedReadHalf = TcpStream;
pub type OwnedWriteHalf = TcpStream;
#[cfg(unix)]
pub use compio::net::{UnixListener, UnixStream};
pub fn bind_reuseport(addr: std::net::SocketAddr) -> std::io::Result<TcpListener> {
TcpListener::from_std(crate::tcp::reuseport_listener(addr)?)
}
pub type JoinHandle<T> = compio::runtime::JoinHandle<T>;
#[inline]
pub fn spawn<F>(fut: F) -> JoinHandle<F::Output>
where
F: Future + 'static,
{
compio::runtime::spawn(fut)
}
#[inline]
pub fn spawn_detached<F>(fut: F)
where
F: Future + 'static,
F::Output: 'static,
{
compio::runtime::spawn(fut).detach();
}
#[inline]
pub async fn spawn_blocking<F, T>(f: F) -> T
where
F: FnOnce() -> T + Send + Sync + 'static,
T: Send + 'static,
{
compio::runtime::spawn_blocking(f)
.await
.expect("blocking task panicked")
}
#[inline]
pub async fn join<T>(handle: JoinHandle<T>) -> T {
handle
.await
.expect("spawned task panicked or was cancelled")
}
pub struct LocalRuntime {
inner: compio::runtime::Runtime,
}
impl LocalRuntime {
pub fn new() -> std::io::Result<Self> {
Ok(Self {
inner: compio::runtime::Runtime::new()?,
})
}
pub fn block_on<F: Future>(&self, fut: F) -> F::Output {
self.inner.block_on(fut)
}
}