use core::future::Future;
use core::pin::Pin;
use super::id_and_context::TaskContext;
use super::traits::Task;
#[allow(clippy::module_name_repetitions)]
pub struct TaskFuture<T> {
task: Option<T>,
context: TaskContext,
}
impl<T> TaskFuture<T>
where
T: Task,
{
pub fn new(task: T, context: TaskContext) -> Self {
Self {
task: Some(task),
context,
}
}
pub fn context(&self) -> &TaskContext {
&self.context
}
}
impl<T> Future for TaskFuture<T>
where
T: Task + Unpin,
{
type Output = T::Output;
fn poll(
self: Pin<&mut Self>,
_cx: &mut core::task::Context<'_>,
) -> core::task::Poll<Self::Output> {
let task = self
.get_mut()
.task
.take()
.expect("TaskFuture polled after completion");
core::task::Poll::Ready(task.execute())
}
}