aral_runtime_async_std/task/
mod.rs1use std::{
2 any::Any,
3 future::Future,
4 pin::{pin, Pin},
5 result,
6 task::{Context, Poll},
7 time::Duration,
8};
9
10#[inline]
11pub async fn sleep(duration: Duration) {
12 async_std::task::sleep(duration).await
13}
14
15pub struct JoinHandle<T>(async_std::task::JoinHandle<T>);
16
17impl<T> JoinHandle<T> {
18 #[inline]
19 pub async fn cancel(self) -> Option<T> {
20 self.0.cancel().await
21 }
22}
23
24impl<T> Future for JoinHandle<T> {
25 type Output = result::Result<T, Box<dyn Any + Send + 'static>>;
26
27 #[inline]
28 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
29 pin!(&mut self.0).poll(cx).map(|t| Ok(t))
30 }
31}
32
33#[inline]
34pub fn spawn<T: Send + 'static>(future: impl Future<Output = T> + Send + 'static) -> JoinHandle<T> {
35 JoinHandle(async_std::task::spawn(future))
36}
37
38#[inline]
39pub fn spawn_blocking<T: Send + 'static>(f: impl FnOnce() -> T + Send + 'static) -> JoinHandle<T> {
40 JoinHandle(async_std::task::spawn_blocking(f))
41}