use std::any::Any;
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
type ErasedFuture = Pin<Box<dyn Future<Output = ()> + Send>>;
pub fn spawn(future: impl Future<Output = ()> + Send + 'static) -> tokio::task::JoinHandle<()> {
spawn_erased(Box::pin(future))
}
#[inline(never)]
fn spawn_erased(future: ErasedFuture) -> tokio::task::JoinHandle<()> {
tokio::spawn(future)
}
type ErasedJob = Box<dyn FnOnce() -> Box<dyn Any + Send> + Send>;
type ErasedOutput = Result<Box<dyn Any + Send>, tokio::task::JoinError>;
pub fn spawn_blocking<T: Send + 'static>(job: impl FnOnce() -> T + Send + 'static) -> Blocking<T> {
let erased: ErasedJob = Box::new(move || Box::new(job()) as Box<dyn Any + Send>);
Blocking { job: ErasedBlocking { job: Some(erased), handle: None }, output: PhantomData }
}
#[must_use = "a blocking job runs only once its future is polled"]
pub struct Blocking<T> {
job: ErasedBlocking,
output: PhantomData<fn() -> T>,
}
impl<T> std::fmt::Debug for Blocking<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Blocking").field("spawned", &self.job.handle.is_some()).finish_non_exhaustive()
}
}
struct ErasedBlocking {
job: Option<ErasedJob>,
handle: Option<tokio::task::JoinHandle<Box<dyn Any + Send>>>,
}
impl<T: 'static> Future for Blocking<T> {
type Output = Result<T, tokio::task::JoinError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let output = match poll_erased(&mut self.get_mut().job, cx) {
Poll::Ready(output) => output?,
Poll::Pending => return Poll::Pending,
};
match output.downcast::<T>() {
Ok(value) => Poll::Ready(Ok(*value)),
Err(_) => unreachable!("blocking job returned another type"),
}
}
}
#[inline(never)]
fn poll_erased(state: &mut ErasedBlocking, cx: &mut Context<'_>) -> Poll<ErasedOutput> {
if let Some(job) = state.job.take() {
state.handle = Some(tokio::task::spawn_blocking(job));
}
match &mut state.handle {
Some(handle) => Pin::new(handle).poll(cx),
None => Poll::Pending,
}
}
#[cfg(test)]
mod tests {
#[tokio::test]
async fn returns_the_value_and_reports_panics() {
assert_eq!(super::spawn_blocking(|| 6 * 7).await.unwrap(), 42);
assert_eq!(super::spawn_blocking(|| String::from("x")).await.unwrap(), "x");
let panicked = super::spawn_blocking(|| -> u8 { std::panic::panic_any("boom") }).await;
assert!(panicked.unwrap_err().is_panic());
let (sender, receiver) = tokio::sync::oneshot::channel();
super::spawn(async move { sender.send(7).unwrap() }).await.unwrap();
assert_eq!(receiver.await.unwrap(), 7);
}
#[tokio::test]
async fn the_job_starts_when_first_polled() {
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
let ran = Arc::new(AtomicBool::new(false));
let flag = ran.clone();
let job = super::spawn_blocking(move || flag.store(true, Ordering::SeqCst));
std::thread::sleep(std::time::Duration::from_millis(50));
assert!(!ran.load(Ordering::SeqCst), "the job ran before its future was polled");
job.await.unwrap();
assert!(ran.load(Ordering::SeqCst));
}
}