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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::fmt::Debug;
use std::hash::Hash;
use std::marker::Unpin;
use std::ops::{Deref, DerefMut};
use std::pin::Pin;
use std::task::{Context, Poll};

use futures::channel::mpsc;

use super::{assert_future, LocalSpawner, LocalTaskExecQueue, LocalTaskType};

impl<T: ?Sized> LocalSpawnExt for T where T: futures::Future {}

pub trait LocalSpawnExt: futures::Future {
    #[inline]
    fn spawn<Tx, G>(self, queue: &LocalTaskExecQueue<Tx, G>) -> LocalSpawner<Self, Tx, G, ()>
        where
            Self: Sized + 'static,
            Self::Output: 'static,
            Tx: Clone + Unpin + futures::Sink<((), LocalTaskType)> + Sync + 'static,
            G: Hash + Eq + Clone + Debug + Sync + 'static,
    {
        let f = LocalSpawner::new(queue, self, ());
        assert_future::<_, _>(f)
    }

    #[inline]
    fn spawn_with<Tx, G, D>(
        self,
        queue: &LocalTaskExecQueue<Tx, G, D>,
        name: D,
    ) -> LocalSpawner<Self, Tx, G, D>
        where
            Self: Sized + 'static,
            Self::Output: 'static,
            Tx: Clone + Unpin + futures::Sink<(D, LocalTaskType)> + Sync + 'static,
            G: Hash + Eq + Clone + Debug + Sync + 'static,
    {
        let f = LocalSpawner::new(queue, self, name);
        assert_future::<_, _>(f)
    }
}

pub struct LocalBuilder {
    workers: usize,
    queue_max: usize,
}

impl Default for LocalBuilder {
    fn default() -> Self {
        Self {
            workers: 100,
            queue_max: 100_000,
        }
    }
}

impl LocalBuilder {
    #[inline]
    pub fn workers(mut self, workers: usize) -> Self {
        self.workers = workers;
        self
    }

    #[inline]
    pub fn queue_max(mut self, max: usize) -> Self {
        self.queue_max = max;
        self
    }

    #[inline]
    pub fn with_channel<Tx, Rx, D>(self, tx: Tx, rx: Rx) -> ChannelLocalBuilder<Tx, Rx, D>
        where
            Tx: Clone + futures::Sink<(D, LocalTaskType)> + Unpin + Sync + 'static,
            Rx: futures::Stream<Item=(D, LocalTaskType)> + Unpin,
    {
        ChannelLocalBuilder {
            builder: self,
            tx,
            rx,
            _d: std::marker::PhantomData,
        }
    }

    #[inline]
    pub fn build(self) -> (LocalTaskExecQueue, impl futures::Future<Output=()>) {
        let (tx, rx) = futures::channel::mpsc::channel(self.queue_max);
        LocalTaskExecQueue::with_channel(self.workers, self.queue_max, SyncSender(tx), rx)
    }
}

pub struct ChannelLocalBuilder<Tx, Rx, D> {
    builder: LocalBuilder,
    tx: Tx,
    rx: Rx,
    _d: std::marker::PhantomData<D>,
}

impl<Tx, Rx, D> ChannelLocalBuilder<Tx, Rx, D>
    where
        Tx: Clone + futures::Sink<(D, LocalTaskType)> + Unpin + Sync + 'static,
        Rx: futures::Stream<Item=(D, LocalTaskType)> + Unpin,
{
    #[inline]
    pub fn build(
        self,
    ) -> (
        LocalTaskExecQueue<Tx, (), D>,
        impl futures::Future<Output=()>,
    ) {
        LocalTaskExecQueue::with_channel(
            self.builder.workers,
            self.builder.queue_max,
            self.tx,
            self.rx,
        )
    }

    #[inline]
    pub fn group(self) -> GroupChannelLocalBuilder<Tx, Rx, D> {
        GroupChannelLocalBuilder { builder: self }
    }
}

pub struct GroupChannelLocalBuilder<Tx, Rx, D> {
    builder: ChannelLocalBuilder<Tx, Rx, D>,
}

impl<Tx, Rx, D> GroupChannelLocalBuilder<Tx, Rx, D>
    where
        Tx: Clone + futures::Sink<((), LocalTaskType)> + Unpin + Sync + 'static,
        Rx: futures::Stream<Item=((), LocalTaskType)> + Unpin,
{
    #[inline]
    pub fn build<G>(self) -> (LocalTaskExecQueue<Tx, G>, impl futures::Future<Output=()>)
        where
            G: Hash + Eq + Clone + Debug + Sync + 'static,
    {
        LocalTaskExecQueue::with_channel(
            self.builder.builder.workers,
            self.builder.builder.queue_max,
            self.builder.tx,
            self.builder.rx,
        )
    }
}

type DataType = ((), LocalTaskType);

#[derive(Clone)]
pub struct SyncSender(pub mpsc::Sender<DataType>);

unsafe impl Sync for SyncSender {}

impl Deref for SyncSender {
    type Target = mpsc::Sender<DataType>;
    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for SyncSender {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl futures::Sink<DataType> for SyncSender {
    type Error = mpsc::SendError;

    fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.0.poll_ready(cx)
    }

    fn start_send(mut self: Pin<&mut Self>, msg: DataType) -> Result<(), Self::Error> {
        self.0.start_send(msg)
    }

    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Pin::new(&mut self.0).poll_flush(cx)
    }

    fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Pin::new(&mut self.0).poll_close(cx)
    }
}