use std::future::Future;
#[cfg(feature = "control")]
use std::marker::PhantomData;
#[cfg(feature = "control")]
use std::pin::Pin;
#[cfg(feature = "control")]
use std::task::{Context, Poll};
pub trait TaskName: Sized + 'static {
type Output: Send + 'static;
const NAME: &'static str;
#[track_caller]
#[inline]
fn task<F>(future: F) -> impl NamedTask<Output = Self::Output> + Send + 'static
where
F: Future<Output = Self::Output> + Send + 'static,
{
#[cfg(feature = "control")]
{
Named::<Self, F>::new(future)
}
#[cfg(not(feature = "control"))]
{
future
}
}
}
pub trait NamedTask: Future + Sized {
#[must_use = "a named task does nothing unless it is awaited or passed to `Task::perform`"]
fn detail(self, detail: impl FnOnce() -> String + Send + 'static) -> Self;
#[must_use = "a named task does nothing unless it is awaited or passed to `Task::perform`"]
fn external(self) -> Self;
}
#[cfg(not(feature = "control"))]
impl<F: Future> NamedTask for F {
#[inline]
fn detail(self, detail: impl FnOnce() -> String + Send + 'static) -> Self {
let _ = detail;
self
}
#[inline]
fn external(self) -> Self {
self
}
}
pub trait Fallible: TaskName {
type Ok: Send + 'static;
type Err: Send + 'static;
fn from_ok(value: Self::Ok) -> Self::Output;
fn from_err(error: Self::Err) -> Self::Output;
}
impl<N, T, E> Fallible for N
where
N: TaskName<Output = Result<T, E>>,
T: Send + 'static,
E: Send + 'static,
{
type Ok = T;
type Err = E;
#[inline]
fn from_ok(value: T) -> Result<T, E> {
Ok(value)
}
#[inline]
fn from_err(error: E) -> Result<T, E> {
Err(error)
}
}
#[macro_export]
macro_rules! task_name {
($($(#[$meta:meta])* $vis:vis $name:ident : $output:ty);+ $(;)?) => {
$(
$(#[$meta])*
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
$vis struct $name;
impl $crate::TaskName for $name {
type Output = $output;
const NAME: &'static str = ::core::concat!(
::core::module_path!(),
"::",
::core::stringify!($name)
);
}
)+
};
}
#[cfg(feature = "control")]
pin_project_lite::pin_project! {
#[must_use = "a named task does nothing unless it is awaited or passed to `Task::perform`"]
pub struct Named<N, F> {
#[pin]
future: F,
probe: crate::control::Probe,
name: PhantomData<fn() -> N>,
}
impl<N, F> PinnedDrop for Named<N, F> {
fn drop(this: Pin<&mut Self>) {
this.project().probe.dropped();
}
}
}
#[cfg(feature = "control")]
impl<N: TaskName, F> std::fmt::Debug for Named<N, F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Named")
.field("name", &N::NAME)
.finish_non_exhaustive()
}
}
#[cfg(feature = "control")]
impl<N, F> Named<N, F> {
#[track_caller]
#[inline]
fn new(future: F) -> Self {
Self {
future,
probe: crate::control::Probe::new(std::panic::Location::caller()),
name: PhantomData,
}
}
}
#[cfg(feature = "control")]
impl<N, F> NamedTask for Named<N, F>
where
N: TaskName,
F: Future<Output = N::Output>,
{
#[inline]
fn detail(mut self, detail: impl FnOnce() -> String + Send + 'static) -> Self {
self.probe.detail = Some(Box::new(detail));
self
}
#[inline]
fn external(mut self) -> Self {
self.probe.external = true;
self
}
}
#[cfg(feature = "control")]
impl<N, F> Future for Named<N, F>
where
N: TaskName,
F: Future<Output = N::Output>,
{
type Output = N::Output;
#[inline]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<N::Output> {
let this = self.project();
this.probe.poll::<N, F>(this.future, cx)
}
}