use std::future::Future; use std::pin::Pin; use std::task::{Context, Poll}; use crate::error::Error; pub struct Task { #[cfg(feature = "smol")] inner_task: smol::Task, #[cfg(feature = "tokio")] inner_task: tokio::task::JoinHandle, } impl Task { pub async fn cancel(self) { #[cfg(feature = "smol")] self.inner_task.cancel().await; #[cfg(feature = "tokio")] self.inner_task.abort(); } } impl Future for Task { type Output = Result; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { #[cfg(feature = "smol")] let result = smol::Task::poll(Pin::new(&mut self.inner_task), cx); #[cfg(feature = "tokio")] let result = tokio::task::JoinHandle::poll(Pin::new(&mut self.inner_task), cx); #[cfg(feature = "smol")] return result.map(Ok); #[cfg(feature = "tokio")] return result.map_err(|e| e.into()); } } #[cfg(feature = "smol")] impl From> for Task { fn from(t: smol::Task) -> Task { Task { inner_task: t } } } #[cfg(feature = "tokio")] impl From> for Task { fn from(t: tokio::task::JoinHandle) -> Task { Task { inner_task: t } } }