use std::{
future::Future,
io,
pin::Pin,
task::{Context, Poll},
};
use super::erased::ExternalTask;
#[derive(Debug)]
pub(crate) enum Task<T> {
#[cfg(feature = "default-rt")]
ZRuntime(super::zruntime::Task<T>),
#[cfg(feature = "tokio")]
Tokio(super::tokio_rt::TokioTask<T>),
External(ExternalTask<T>),
}
impl<T> Task<T>
where
T: Send + 'static,
{
pub(crate) fn detach(self) {
match self {
#[cfg(feature = "default-rt")]
Self::ZRuntime(task) => super::traits::TaskHandle::detach(task),
#[cfg(feature = "tokio")]
Self::Tokio(task) => super::traits::TaskHandle::detach(task),
Self::External(handle) => super::traits::TaskHandle::detach(handle),
}
}
}
impl<T> Future for Task<T>
where
T: Send + 'static,
{
type Output = io::Result<T>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.get_mut() {
#[cfg(feature = "default-rt")]
Self::ZRuntime(task) => Pin::new(task).poll(cx),
#[cfg(feature = "tokio")]
Self::Tokio(task) => Pin::new(task).poll(cx),
Self::External(handle) => Pin::new(handle).poll(cx),
}
}
}
impl<T> super::traits::TaskHandle<T> for Task<T>
where
T: Send + 'static,
{
fn detach(self) {
Task::detach(self)
}
}