async-rt 0.1.10

A small library designed to utilize async executors through an common API while extending features.
Documentation
use crate::{Executor, ExecutorBlocking, JoinHandle};
use std::fmt::Debug;
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
use std::task::{Context, Poll};

/// Track running tasks.
///
/// Note that there is no guarantee that the runtime would drop the future after it is done; therefore,
/// this should only be used for purely approx statistics and not actual numbers. Additionally,
/// it does not track any tasks spawned directly by the runtime but only by [`Executor::spawn`] through
/// this struct.
pub struct TrackerExecutor<E> {
    executor: E,
    counter: Arc<AtomicUsize>,
}

impl<E> Debug for TrackerExecutor<E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TrackerExecutor").finish()
    }
}

impl<E: Executor> TrackerExecutor<E> {
    pub fn new(executor: E) -> Self {
        Self {
            executor,
            counter: Arc::default(),
        }
    }

    /// Number of active tasks.
    pub fn count(&self) -> usize {
        self.counter.load(std::sync::atomic::Ordering::Relaxed)
    }
}

struct FutureCounter<F> {
    future: F,
    counter: Arc<AtomicUsize>,
}

impl<F> FutureCounter<F> {
    pub fn new(future: F, counter: Arc<AtomicUsize>) -> Self {
        counter.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        Self { future, counter }
    }
}

impl<F> Future for FutureCounter<F>
where
    F: Future + 'static + Unpin,
{
    type Output = F::Output;
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        Pin::new(&mut self.future).poll(cx)
    }
}

impl<F> Drop for FutureCounter<F> {
    fn drop(&mut self) {
        self.counter
            .fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
    }
}

impl<E: Executor> Executor for TrackerExecutor<E> {
    fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
    where
        F: Future + Send + 'static,
        F::Output: Send + 'static,
    {
        let counter = self.counter.clone();
        let future = Box::pin(future);
        let future = FutureCounter::new(future, counter);
        self.executor.spawn(future)
    }
}

impl<E: ExecutorBlocking> ExecutorBlocking for TrackerExecutor<E> {
    fn spawn_blocking<F, R>(&self, f: F) -> JoinHandle<R>
    where
        F: FnOnce() -> R + Send + 'static,
        R: Send + 'static,
    {
        struct AtomicCounterDrop(Arc<AtomicUsize>);

        impl AtomicCounterDrop {
            pub fn new(counter: Arc<AtomicUsize>) -> Self {
                counter.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                Self(counter)
            }
        }

        impl Drop for AtomicCounterDrop {
            fn drop(&mut self) {
                self.0.fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
            }
        }

        let counter = AtomicCounterDrop::new(self.counter.clone());

        self.executor.spawn_blocking(move || {
            let _counter = counter;
            f()
        })
    }
}

#[cfg(test)]
mod tests {
    use super::TrackerExecutor;
    use crate::rt::tokio::TokioExecutor;
    use crate::Executor;

    #[tokio::test]
    async fn test_tracker_executor() {
        let executor = TrackerExecutor::new(TokioExecutor);
        let handle = executor.spawn(futures::future::pending::<()>());
        assert_eq!(executor.count(), 1);
        handle.abort();
        // We yield back to the runtime to allow progress to be made after aborting the task.
        crate::task::yield_now().await;
        assert_eq!(executor.count(), 0);
    }
}