use core::future::Future;
use crate::transport::tcp::BlockingTcp;
use crate::transport::Splittable;
pub trait Runtime {
type Tcp: Splittable + Send + 'static;
fn connect_tcp(
host: &str,
port: u16,
) -> impl Future<Output = Result<Self::Tcp, std::io::Error>> + Send;
fn run_blocking<T, F>(f: F) -> impl Future<Output = Result<T, BlockingError>> + Send
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BlockingError;
impl core::fmt::Display for BlockingError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("blocking task failed")
}
}
impl core::error::Error for BlockingError {}
pub struct Inline;
#[allow(clippy::manual_async_fn)]
impl Runtime for Inline {
type Tcp = BlockingTcp;
fn connect_tcp(
host: &str,
port: u16,
) -> impl Future<Output = Result<Self::Tcp, std::io::Error>> + Send {
let host = alloc::string::String::from(host);
async move {
let stream = std::net::TcpStream::connect((host.as_str(), port))?;
stream.set_nodelay(true)?;
Ok(BlockingTcp::new(stream))
}
}
fn run_blocking<T, F>(f: F) -> impl Future<Output = Result<T, BlockingError>> + Send
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
async move { Ok(f()) }
}
}
#[cfg(feature = "tokio")]
pub struct Tokio;
#[allow(clippy::manual_async_fn)]
#[cfg(feature = "tokio")]
impl Runtime for Tokio {
type Tcp = crate::transport::tcp::TokioTcp;
fn connect_tcp(
host: &str,
port: u16,
) -> impl Future<Output = Result<Self::Tcp, std::io::Error>> + Send {
let host = alloc::string::String::from(host);
async move {
let stream = tokio::net::TcpStream::connect((host.as_str(), port)).await?;
stream.set_nodelay(true)?;
Ok(crate::transport::tcp::TokioTcp::new(stream))
}
}
fn run_blocking<T, F>(f: F) -> impl Future<Output = Result<T, BlockingError>> + Send
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
async move {
let Ok(handle) = tokio::runtime::Handle::try_current() else {
return Ok(f());
};
handle.spawn_blocking(f).await.map_err(|e| {
log::warn!("tokio blocking task failed: {e}");
BlockingError
})
}
}
}
#[cfg(feature = "smol")]
pub struct Smol;
#[allow(clippy::manual_async_fn)]
#[cfg(feature = "smol")]
impl Runtime for Smol {
type Tcp = crate::transport::tcp::SmolTcp;
fn connect_tcp(
host: &str,
port: u16,
) -> impl Future<Output = Result<Self::Tcp, std::io::Error>> + Send {
let host = alloc::string::String::from(host);
async move {
let stream = smol::net::TcpStream::connect((host.as_str(), port)).await?;
stream.set_nodelay(true)?;
Ok(crate::transport::tcp::SmolTcp::new(stream))
}
}
fn run_blocking<T, F>(f: F) -> impl Future<Output = Result<T, BlockingError>> + Send
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
async move {
match smol::unblock(|| std::panic::catch_unwind(std::panic::AssertUnwindSafe(f))).await
{
Ok(t) => Ok(t),
Err(_) => {
log::warn!("smol blocking task panicked");
Err(BlockingError)
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn block_on<F: Future>(fut: F) -> F::Output {
let mut fut = core::pin::pin!(fut);
let mut cx = core::task::Context::from_waker(core::task::Waker::noop());
match fut.as_mut().poll(&mut cx) {
core::task::Poll::Ready(v) => v,
core::task::Poll::Pending => panic!("Inline never parks"),
}
}
#[test]
fn inline_runs_the_closure_on_this_thread() {
let here = std::thread::current().id();
let ran_on = block_on(Inline::run_blocking(std::thread::current)).unwrap();
assert_eq!(ran_on.id(), here);
}
#[test]
fn a_blocking_failure_says_what_it_was() {
assert_eq!(
std::format!("{BlockingError}"),
"blocking task failed",
"the message is what a caller sees when a pool drops the work"
);
}
#[test]
fn inline_defers_the_closure_until_the_future_is_polled() {
use core::sync::atomic::{AtomicBool, Ordering};
let ran = std::sync::Arc::new(AtomicBool::new(false));
let fut = Inline::run_blocking({
let ran = ran.clone();
move || ran.store(true, Ordering::SeqCst)
});
assert!(!ran.load(Ordering::SeqCst), "ran before the first poll");
block_on(fut).unwrap();
assert!(ran.load(Ordering::SeqCst));
}
#[test]
fn inline_dials_tcp_with_nagle_off() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
let tcp = block_on(Inline::connect_tcp("127.0.0.1", port)).unwrap();
assert_eq!(tcp.get_ref().peer_addr().unwrap().port(), port);
assert!(
tcp.get_ref().nodelay().unwrap(),
"Nagle would hold every small packet back for an ACK"
);
}
#[test]
fn inline_reports_a_refused_dial() {
let port = {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
listener.local_addr().unwrap().port()
};
let err = block_on(Inline::connect_tcp("127.0.0.1", port)).unwrap_err();
assert_eq!(err.kind(), std::io::ErrorKind::ConnectionRefused);
}
#[test]
fn inline_defers_the_dial_until_the_future_is_polled() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
listener.set_nonblocking(true).unwrap();
let port = listener.local_addr().unwrap().port();
let fut = Inline::connect_tcp("127.0.0.1", port);
assert_eq!(
listener.accept().unwrap_err().kind(),
std::io::ErrorKind::WouldBlock,
"dialled before the first poll"
);
let _tcp = block_on(fut).unwrap();
assert!(listener.accept().is_ok());
}
#[cfg(all(feature = "tokio", feature = "smol"))]
#[test]
fn every_runtime_marker_is_usable_in_one_build() {
fn takes<R: Runtime>() {}
takes::<Tokio>();
takes::<Smol>();
takes::<Inline>();
}
}