use crate::error::JoinError;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
pub struct JoinHandle<T>(tokio::task::JoinHandle<T>);
impl<T> Future for JoinHandle<T> {
type Output = Result<T, JoinError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
tokio::task::JoinHandle::poll(Pin::new(&mut self.0), cx).map_err(Into::into)
}
}
impl<T> From<tokio::task::JoinHandle<T>> for JoinHandle<T> {
fn from(h: tokio::task::JoinHandle<T>) -> Self {
Self(h)
}
}
#[track_caller]
pub fn spawn<T>(task: T) -> JoinHandle<T::Output>
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
tokio::task::spawn(task).into()
}
pub async fn sleep(duration: std::time::Duration) {
tokio::time::sleep(duration).await;
}
pub async fn timeout<F, T>(
duration: std::time::Duration,
f: F,
) -> std::result::Result<T, Box<dyn std::error::Error>>
where
F: Future<Output = T>,
{
let result = tokio::time::timeout(duration, f).await?;
Ok(result)
}