use crate::block_on;
use std::{future::Future, thread};
pub struct JoinHandle<T> {
inner: thread::JoinHandle<T>,
}
pub fn spawn<F>(future: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
JoinHandle {
inner: thread::spawn(move || block_on(future)),
}
}
impl<T> JoinHandle<T> {
pub fn join(self) -> thread::Result<T> {
self.inner.join()
}
}
#[cfg(test)]
mod tests {
use super::spawn;
#[test]
fn spawn_runs_future_on_worker_thread() {
let handle = spawn(async { 5_u32 + 7 });
assert_eq!(handle.join().unwrap(), 12);
}
}