1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::{Executor, TaskStream};
use async_task::Runnable;
use core::future::Future;
use core::marker::PhantomData;
use core::pin::Pin;
use core::task::{Context, Poll};
use futures_util::Stream;

pub struct LocalRunnable {
    runnable: Runnable,
    // Prevent Send, Sync
    _private: PhantomData<*mut ()>,
}
impl LocalRunnable {
    fn new(runnable: Runnable) -> Self {
        Self {
            runnable,
            _private: PhantomData,
        }
    }
    pub fn run(self) -> bool {
        self.runnable.run()
    }
}

pub struct LocalExecutor<const N: usize> {
    exec: Executor<N>,
    // Prevent Send, Sync
    _private: PhantomData<*mut ()>,
}
impl<const N: usize> LocalExecutor<N> {
    pub const fn new(exec: Executor<N>) -> Self {
        Self {
            exec,
            _private: PhantomData,
        }
    }

    pub fn spawn<F>(&self, future: F)
    where
        F: Future + 'static,
        F::Output: 'static,
    {
        unsafe { self.exec.spawn_local(future) }
    }

    pub fn stream(&self) -> LocalTaskStream<N> {
        LocalTaskStream {
            stream: self.exec.stream(),
            _private: PhantomData,
        }
    }
}

pub struct LocalTaskStream<'a, const N: usize> {
    stream: TaskStream<'a, N>,
    // Prevent Send, Sync
    _private: PhantomData<*mut ()>,
}
impl<'a, const N: usize> LocalTaskStream<'a, N> {
    pub fn get_task(&self) -> Option<LocalRunnable> {
        let runnable = self.stream.get_task()?;
        Some(LocalRunnable::new(runnable))
    }
}
impl<'a, const N: usize> Stream for LocalTaskStream<'a, N> {
    type Item = LocalRunnable;
    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        if let Poll::Ready(runnable) = Pin::new(&mut self.stream).poll_next(cx){
            if let Some(runnable) = runnable {
                Poll::Ready(Some(LocalRunnable::new(runnable)))
            }else{
                Poll::Ready(None)
            }
        }else{
            Poll::Pending
        }
    }
}