use core::future::Future;
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 = NoTcp;
fn connect_tcp(
_host: &str,
_port: u16,
) -> impl Future<Output = Result<Self::Tcp, std::io::Error>> + Send {
core::future::ready(Err(std::io::Error::new(
std::io::ErrorKind::Unsupported,
"Inline cannot dial TCP: pick a runtime such as Tokio or Smol",
)))
}
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()) }
}
}
#[derive(Debug)]
pub enum NoTcp {}
impl embedded_io::ErrorType for NoTcp {
type Error = crate::transport::common::NoUsbError;
}
impl embedded_io_async::Read for NoTcp {
async fn read(&mut self, _buf: &mut [u8]) -> Result<usize, Self::Error> {
match *self {}
}
}
impl embedded_io_async::Write for NoTcp {
async fn write(&mut self, _buf: &[u8]) -> Result<usize, Self::Error> {
match *self {}
}
async fn flush(&mut self) -> Result<(), Self::Error> {
match *self {}
}
}
impl Splittable for NoTcp {
type ReadHalf = NoTcp;
type WriteHalf = NoTcp;
fn split(self) -> Result<(Self::ReadHalf, Self::WriteHalf), Self::Error> {
match self {}
}
}
impl crate::transport::ReadCancelSafety for NoTcp {
fn read_cancel_safe(&self) -> bool {
match *self {}
}
}
#[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_cannot_dial_tcp() {
let err = block_on(Inline::connect_tcp("127.0.0.1", 5555)).unwrap_err();
assert_eq!(err.kind(), std::io::ErrorKind::Unsupported);
}
#[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>();
}
}