beekeeper 0.3.0

A full-featured worker pool library for parallelizing tasks
Documentation
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Worker implementations that wrap callables (closures or function pointers that are `FnMut`).
use crate::bee::{
    ApplyError, ApplyRefError, Context, RefWorker, RefWorkerResult, Worker, WorkerResult,
};
use derive_more::Debug;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::{any, fmt};

/// Wraps a closure or function pointer and calls it when applied. For this `Callable` to be
/// useable by a `Worker`, the function must be `FnMut` *and* `Clone`able.
///
/// TODO: we could provide a better `Debug` implementation by providing a macro that can wrap a
/// closure and store the text of the function, and then change all the Workers to take a
/// `F: Deref<Target = Fn>`.
/// See https://users.rust-lang.org/t/is-it-possible-to-implement-debug-for-fn-type/14824/3
#[derive(Debug)]
struct Callable<I, O, E, F> {
    #[debug(skip)]
    f: F,
    #[debug("{}", any::type_name::<I>())]
    i: PhantomData<I>,
    #[debug("{}", any::type_name::<O>())]
    o: PhantomData<O>,
    #[debug("{}", any::type_name::<E>())]
    e: PhantomData<E>,
}

impl<I, O, E, F> Callable<I, O, E, F> {
    fn of(f: F) -> Self {
        Self {
            f,
            i: PhantomData,
            o: PhantomData,
            e: PhantomData,
        }
    }

    fn into_inner(self) -> F {
        self.f
    }
}

impl<I, O, E, F: Clone> Clone for Callable<I, O, E, F> {
    fn clone(&self) -> Self {
        Self::of(self.f.clone())
    }
}

impl<I, O, E, F> Deref for Callable<I, O, E, F> {
    type Target = F;

    fn deref(&self) -> &Self::Target {
        &self.f
    }
}

impl<I, O, E, F> DerefMut for Callable<I, O, E, F> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.f
    }
}

/// A `Caller` that executes its function once on the input and returns the output. The function
/// should not panic.
#[derive(Debug)]
pub struct Caller<I, O, F> {
    callable: Callable<I, O, (), F>,
}

impl<I, O, F> Caller<I, O, F> {
    /// Returns the wrapped callable.
    pub fn into_inner(self) -> F {
        self.callable.into_inner()
    }
}

impl<I, O, F> From<F> for Caller<I, O, F>
where
    I: Send + Sync + 'static,
    O: Send + Sync + 'static,
    F: FnMut(I) -> O + Clone + 'static,
{
    fn from(f: F) -> Self {
        Caller {
            callable: Callable::of(f),
        }
    }
}

impl<I, O, F: Clone> Clone for Caller<I, O, F> {
    fn clone(&self) -> Self {
        Self {
            callable: self.callable.clone(),
        }
    }
}

impl<I, O, F> Worker for Caller<I, O, F>
where
    I: Send + 'static,
    O: Send + 'static,
    F: FnMut(I) -> O + Clone + 'static,
{
    type Input = I;
    type Output = O;
    type Error = ();

    #[inline]
    fn apply(&mut self, input: Self::Input, _: &Context<Self::Input>) -> WorkerResult<Self> {
        Ok((self.callable)(input))
    }
}

/// A `Caller` that executes its function once on each input. The input value is consumed by the
/// function. If the function returns an error, it is wrapped in `ApplyError::Fatal`.
///
/// If ownership of the input value is not required, consider using `RefCaller` instead.
#[derive(Debug)]
pub struct OnceCaller<I, O, E, F> {
    callable: Callable<I, O, E, F>,
}

impl<I, O, E, F> OnceCaller<I, O, E, F> {
    /// Returns the wrapped callable.
    pub fn into_inner(self) -> F {
        self.callable.into_inner()
    }
}

impl<I, O, E, F> From<F> for OnceCaller<I, O, E, F>
where
    I: Send + Sync + 'static,
    O: Send + Sync + 'static,
    E: Send + Sync + fmt::Debug + 'static,
    F: FnMut(I) -> Result<O, E> + Clone + 'static,
{
    fn from(f: F) -> Self {
        OnceCaller {
            callable: Callable::of(f),
        }
    }
}

impl<I, O, E, F: Clone> Clone for OnceCaller<I, O, E, F> {
    fn clone(&self) -> Self {
        Self {
            callable: self.callable.clone(),
        }
    }
}

impl<I, O, E, F> Worker for OnceCaller<I, O, E, F>
where
    I: Send + 'static,
    O: Send + 'static,
    E: Send + fmt::Debug + 'static,
    F: FnMut(I) -> Result<O, E> + Clone + 'static,
{
    type Input = I;
    type Output = O;
    type Error = E;

    #[inline]
    fn apply(&mut self, input: Self::Input, _: &Context<Self::Input>) -> WorkerResult<Self> {
        (self.callable)(input).map_err(|error| ApplyError::Fatal { error, input: None })
    }
}

/// A `Caller` that executes its function once on a reference to the input. If the function
/// returns an error, it is wrapped in `ApplyError::Fatal`.
///
/// The benefit of using `RefCaller` over `OnceCaller` is that the `Fatal` error
/// contains the input value for later recovery.
#[derive(Debug)]
pub struct RefCaller<I, O, E, F> {
    callable: Callable<I, O, E, F>,
}

impl<I, O, E, F> RefCaller<I, O, E, F> {
    /// Returns the wrapped callable.
    pub fn into_inner(self) -> F {
        self.callable.into_inner()
    }
}

impl<I, O, E, F> From<F> for RefCaller<I, O, E, F>
where
    I: Send + Sync + 'static,
    O: Send + Sync + 'static,
    E: Send + Sync + fmt::Debug + 'static,
    F: FnMut(&I) -> Result<O, E> + Clone + 'static,
{
    fn from(f: F) -> Self {
        RefCaller {
            callable: Callable::of(f),
        }
    }
}

impl<I, O, E, F: Clone> Clone for RefCaller<I, O, E, F> {
    fn clone(&self) -> Self {
        Self {
            callable: self.callable.clone(),
        }
    }
}

impl<I, O, E, F> RefWorker for RefCaller<I, O, E, F>
where
    I: Send + 'static,
    O: Send + 'static,
    E: Send + fmt::Debug + 'static,
    F: FnMut(&I) -> Result<O, E> + Clone + 'static,
{
    type Input = I;
    type Output = O;
    type Error = E;

    #[inline]
    fn apply_ref(
        &mut self,
        input: &Self::Input,
        _: &Context<Self::Input>,
    ) -> RefWorkerResult<Self> {
        (self.callable)(input).map_err(|error| ApplyRefError::Fatal(error))
    }
}

/// A `Caller` that returns a `Result<O, ApplyError>`. A result of `Err(ApplyError::Retryable)`
/// can be returned to indicate the task should be retried.
#[derive(Debug)]
pub struct RetryCaller<I, O, E, F> {
    callable: Callable<I, O, E, F>,
}

impl<I, O, E, F> RetryCaller<I, O, E, F> {
    /// Returns the wrapped callable.
    pub fn into_inner(self) -> F {
        self.callable.into_inner()
    }
}

impl<I, O, E, F> From<F> for RetryCaller<I, O, E, F>
where
    I: Send + Sync + 'static,
    O: Send + Sync + 'static,
    E: Send + Sync + fmt::Debug + 'static,
    F: FnMut(I, &Context<I>) -> Result<O, ApplyError<I, E>> + Clone + 'static,
{
    fn from(f: F) -> Self {
        RetryCaller {
            callable: Callable::of(f),
        }
    }
}

impl<I, O, E, F: Clone> Clone for RetryCaller<I, O, E, F> {
    fn clone(&self) -> Self {
        Self {
            callable: self.callable.clone(),
        }
    }
}

impl<I, O, E, F> Worker for RetryCaller<I, O, E, F>
where
    I: Send + 'static,
    O: Send + 'static,
    E: Send + fmt::Debug + 'static,
    F: FnMut(I, &Context<I>) -> Result<O, ApplyError<I, E>> + Clone + 'static,
{
    type Input = I;
    type Output = O;
    type Error = E;

    #[inline]
    fn apply(&mut self, input: Self::Input, ctx: &Context<Self::Input>) -> WorkerResult<Self> {
        (self.callable)(input, ctx)
    }
}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    use super::*;
    use crate::bee::Context;

    #[test]
    fn test_call() {
        let mut worker = Caller::from(|input: u8| input + 1);
        assert!(matches!(worker.apply(5, &Context::empty()), Ok(6)))
    }

    #[test]
    fn test_clone() {
        let worker1 = Caller::from(|input: u8| input + 1);
        let worker2 = worker1.clone();
        let f = worker2.into_inner();
        assert_eq!(f(5), 6);
    }

    #[allow(clippy::type_complexity)]
    fn try_caller() -> RetryCaller<
        (bool, u8),
        u8,
        String,
        impl FnMut((bool, u8), &Context<(bool, u8)>) -> Result<u8, ApplyError<(bool, u8), String>>
        + Clone
        + 'static,
    > {
        RetryCaller::from(|input: (bool, u8), _: &Context<(bool, u8)>| {
            if input.0 {
                Ok(input.1 + 1)
            } else {
                Err(ApplyError::Fatal {
                    input: Some(input),
                    error: "failure".into(),
                })
            }
        })
    }

    #[test]
    fn test_try_call_ok() {
        let mut worker = try_caller();
        assert!(matches!(worker.apply((true, 5), &Context::empty()), Ok(6)));
    }

    #[test]
    fn test_clone_retry_caller() {
        let worker1 = try_caller();
        let worker2 = worker1.clone();
        let mut f = worker2.into_inner();
        assert!(matches!(f((true, 5), &Context::empty()), Ok(6)));
    }

    #[test]
    fn test_try_call_fail() {
        let mut worker = try_caller();
        let result = worker.apply((false, 5), &Context::empty());
        let _error = String::from("failure");
        assert!(matches!(
            result,
            Err(ApplyError::Fatal {
                input: Some((false, 5)),
                error: _error
            })
        ));
    }

    #[allow(clippy::type_complexity)]
    fn once_caller() -> OnceCaller<
        (bool, u8),
        u8,
        String,
        impl FnMut((bool, u8)) -> Result<u8, String> + Clone + 'static,
    > {
        OnceCaller::from(|input: (bool, u8)| {
            if input.0 {
                Ok(input.1 + 1)
            } else {
                Err("failure".into())
            }
        })
    }

    #[test]
    fn test_once_call_ok() {
        let mut worker = once_caller();
        assert!(matches!(worker.apply((true, 5), &Context::empty()), Ok(6)));
    }

    #[test]
    fn test_clone_once_caller() {
        let worker1 = once_caller();
        let worker2 = worker1.clone();
        let mut f = worker2.into_inner();
        assert!(matches!(f((true, 5)), Ok(6)));
    }

    #[test]
    fn test_once_call_fail() {
        let mut worker = once_caller();
        let result = worker.apply((false, 5), &Context::empty());
        let _error = String::from("failure");
        assert!(matches!(
            result,
            Err(ApplyError::Fatal {
                input: None,
                error: _error
            })
        ));
    }

    #[allow(clippy::type_complexity)]
    fn ref_caller() -> RefCaller<
        (bool, u8),
        u8,
        String,
        impl FnMut(&(bool, u8)) -> Result<u8, String> + Clone + 'static,
    > {
        RefCaller::from(|input: &(bool, u8)| {
            if input.0 {
                Ok(input.1 + 1)
            } else {
                Err("failure".into())
            }
        })
    }

    #[test]
    fn test_ref_call_ok() {
        let mut worker = ref_caller();
        assert!(matches!(worker.apply((true, 5), &Context::empty()), Ok(6)));
    }

    #[test]
    fn test_clone_ref_caller() {
        let worker1 = ref_caller();
        let worker2 = worker1.clone();
        let mut f = worker2.into_inner();
        assert!(matches!(f(&(true, 5)), Ok(6)));
    }

    #[test]
    fn test_ref_call_fail() {
        let mut worker = ref_caller();
        let result = worker.apply((false, 5), &Context::empty());
        let _error = String::from("failure");
        assert!(matches!(
            result,
            Err(ApplyError::Fatal {
                input: Some((false, 5)),
                error: _error
            })
        ));
    }
}