use crate::fmt;
use alloc::sync::Arc;
use core::cell::UnsafeCell;
use core::fmt::{Debug, Formatter};
use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Waker};
type TaskFuture = Pin<Arc<UnsafeCell<dyn Future<Output = ()>>>>;
pub struct AsyncTask {
future: TaskFuture,
}
impl<'a> Debug for AsyncTask {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Task").field("future", &"omitted").finish()
}
}
impl<'a> Clone for AsyncTask {
fn clone(&self) -> Self {
Self {
future: self.future.clone(),
}
}
fn clone_from(&mut self, source: &Self) {
self.future.clone_from(&source.future);
}
}
impl AsyncTask {
pub(crate) fn new(future: impl Future<Output = ()> + 'static) -> Self {
Self {
future: Arc::pin(UnsafeCell::new(future)),
}
}
pub(crate) unsafe fn poll(&self, waker: &Waker) {
let _ = Pin::new_unchecked(&mut *self.future.as_ref().get())
.poll(&mut Context::from_waker(&waker));
}
}
unsafe impl Send for AsyncTask {}