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
189
190
191
192
193
194
pub mod dynamic_pool;
pub mod static_pool;
use self::dynamic_pool::DynamicPool;
use self::static_pool::StaticPool;
use reducer::{CollectReducer, Reducer};
use std::fmt::Debug;
use std::iter::FromIterator;
use std::marker::PhantomData;
use worker::Worker;
pub trait Pool<I> {
type Output;
type WaitHandle: WaitHandle<Output = Self::Output>;
fn add(&self, input: I);
fn wait_handle(self) -> Self::WaitHandle;
fn wait(self) -> Self::Output;
}
pub trait WaitHandle: Clone {
type Output;
fn wait(&self) -> &Self::Output;
}
#[derive(Debug)]
pub struct Yes<T>(T);
#[derive(Debug)]
pub struct No;
pub trait ToAssign {}
pub trait Assigned: ToAssign {
type T;
fn value(self) -> Self::T;
}
pub trait NotAssigned: ToAssign {}
impl<T> ToAssign for Yes<T> {}
impl ToAssign for No {}
impl<T> Assigned for Yes<T> {
type T = T;
fn value(self) -> Self::T {
self.0
}
}
impl NotAssigned for No {}
#[derive(Debug)]
pub struct Builder<WorkerInputT, WorkerT, ReducerT, LimitT> {
worker_input: PhantomData<WorkerInputT>,
worker: WorkerT,
reducer: ReducerT,
concurrency_limit: LimitT,
}
pub fn new() -> Builder<No, No, No, No> {
Builder {
worker_input: PhantomData {},
worker: No,
reducer: No,
concurrency_limit: No,
}
}
impl<WorkerInputT, WorkerT, ReducerT, LimitT> Builder<WorkerInputT, WorkerT, ReducerT, LimitT>
where
WorkerInputT: ToAssign,
WorkerT: ToAssign,
ReducerT: ToAssign,
LimitT: ToAssign,
{
pub fn set_worker<I, W>(self, w: W) -> Builder<Yes<I>, Yes<W>, ReducerT, LimitT>
where
WorkerT: NotAssigned,
W: Worker<I>,
{
Builder {
worker_input: PhantomData::<Yes<I>> {},
worker: Yes(w),
reducer: self.reducer,
concurrency_limit: self.concurrency_limit,
}
}
pub fn set_reducer<R>(self, r: R) -> Builder<WorkerInputT, WorkerT, Yes<R>, LimitT>
where
ReducerT: NotAssigned,
{
Builder {
worker_input: PhantomData {},
worker: self.worker,
reducer: Yes(r),
concurrency_limit: self.concurrency_limit,
}
}
#[cfg_attr(feature = "cargo-clippy", allow(clippy::type_complexity))]
pub fn collect_into<C>(
self,
) -> Builder<
WorkerInputT,
WorkerT,
Yes<
CollectReducer<
<<WorkerT as Assigned>::T as Worker<<WorkerInputT as Assigned>::T>>::Output,
C,
>,
>,
LimitT,
>
where
WorkerInputT: Assigned,
WorkerT: Assigned,
WorkerT::T: Worker<WorkerInputT::T>,
ReducerT: NotAssigned,
<<WorkerT as Assigned>::T as Worker<<WorkerInputT as Assigned>::T>>::Output: Send + 'static,
C: FromIterator<
<<WorkerT as Assigned>::T as Worker<<WorkerInputT as Assigned>::T>>::Output,
> + Send
+ 'static,
{
self.set_reducer(CollectReducer::<
<<WorkerT as Assigned>::T as Worker<WorkerInputT::T>>::Output,
C,
>::new())
}
pub fn set_concurrency_limit(
self,
concurrency_limit: i64,
) -> Builder<WorkerInputT, WorkerT, ReducerT, Yes<i64>> {
Builder {
worker_input: PhantomData {},
worker: self.worker,
reducer: self.reducer,
concurrency_limit: Yes(concurrency_limit),
}
}
}
impl<I, W, R> Builder<Yes<I>, Yes<W>, Yes<R>, Yes<i64>> {
pub fn create_dynamic_pool(self) -> DynamicPool<I, W, R>
where
I: Send + Debug + 'static,
W: Worker<I> + Send + Sync + 'static,
W::Output: Send + Debug,
R: Reducer<W::Output> + Send + 'static,
R::Output: Send + Sync + Debug,
{
DynamicPool::create(
self.worker.value(),
self.reducer.value(),
self.concurrency_limit.value(),
)
}
pub fn create_static_pool(self) -> StaticPool<I, W, R>
where
I: Send + Debug + 'static,
W: Worker<I> + Send + Sync + 'static,
W::Output: Send + Debug,
R: Reducer<W::Output> + Send + 'static,
R::Output: Send + Sync + Debug,
{
StaticPool::create(
self.worker.value(),
self.reducer.value(),
self.concurrency_limit.value(),
)
}
}