datum-core 0.3.0

Rust stream-processing library mirroring Akka/Pekko Streams Typed, built on Ractor actors
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! Context-aware stream wrappers.
//!
//! `SourceWithContext` and `FlowWithContext` are thin wrappers around regular pair
//! streams that keep an explicit context element adjacent to each data element.
//!
//! The operator set mirrors Akka's `SourceWithContext` / `FlowWithContext`: only
//! one-to-one operators are exposed by design so `(data, context)` alignment cannot be
//! broken by this API.
//!
//! This is intentional. Reordering, arbitrarily dropping, or expanding to
//! variable-length sequences can detach context from its intended element, which can
//! silently break downstream sinks that rely on commit/trace/offset semantics. Keeping
//! the API wrapper-centric preserves the one-to-one contract by construction while
//! still enabling downstream composition.

use futures::future::FutureExt;
use std::future::Future;

use crate::stream::{Flow, NotUsed, RunnableGraph, Sink, Source, StreamResult};

#[derive(Clone)]
pub struct SourceWithContext<Out, Ctx, Mat = NotUsed> {
    pub(crate) delegate: Source<(Out, Ctx), Mat>,
}

#[derive(Clone)]
pub struct FlowWithContext<In, CtxIn, Out, CtxOut, Mat = NotUsed> {
    pub(crate) delegate: Flow<(In, CtxIn), (Out, CtxOut), Mat>,
}

impl<Out: Send + 'static, Ctx: Send + 'static, Mat: Send + 'static>
    SourceWithContext<Out, Ctx, Mat>
{
    pub(crate) fn from_source(delegate: Source<(Out, Ctx), Mat>) -> Self {
        Self { delegate }
    }

    pub fn as_source(self) -> Source<(Out, Ctx), Mat> {
        self.delegate
    }

    pub fn run_collect(self) -> StreamResult<Vec<(Out, Ctx)>> {
        self.delegate.run_collect()
    }

    pub fn map<Next, F>(self, f: F) -> SourceWithContext<Next, Ctx, Mat>
    where
        Next: Send + 'static,
        F: Fn(Out) -> Next + Send + Sync + 'static,
    {
        SourceWithContext::from_source(self.delegate.map(move |(out, ctx)| (f(out), ctx)))
    }

    pub fn filter<F>(self, predicate: F) -> SourceWithContext<Out, Ctx, Mat>
    where
        F: Fn(&Out) -> bool + Send + Sync + 'static,
    {
        SourceWithContext::from_source(self.delegate.filter_map(move |(out, ctx)| {
            if predicate(&out) {
                Some((out, ctx))
            } else {
                None
            }
        }))
    }

    pub fn filter_not<F>(self, predicate: F) -> SourceWithContext<Out, Ctx, Mat>
    where
        F: Fn(&Out) -> bool + Send + Sync + 'static,
    {
        self.filter(move |out| !predicate(out))
    }

    pub fn filter_map<Next, F>(self, f: F) -> SourceWithContext<Next, Ctx, Mat>
    where
        Next: Send + 'static,
        F: Fn(Out) -> Option<Next> + Send + Sync + 'static,
    {
        SourceWithContext::from_source(
            self.delegate
                .filter_map(move |(out, ctx)| f(out).map(|item| (item, ctx))),
        )
    }

    pub fn map_concat<Next, F, I>(self, f: F) -> SourceWithContext<Next, Ctx, Mat>
    where
        Next: Send + 'static,
        F: Fn(Out) -> I + Send + Sync + 'static,
        I: IntoIterator<Item = Next>,
        I::IntoIter: Send + 'static,
        Ctx: Clone,
    {
        SourceWithContext::from_source(self.delegate.map_concat(move |(out, ctx)| {
            let ctx = ctx.clone();
            f(out).into_iter().map(move |next| (next, ctx.clone()))
        }))
    }

    pub fn map_async<Next, F, Fut>(
        self,
        parallelism: usize,
        f: F,
    ) -> SourceWithContext<Next, Ctx, Mat>
    where
        Next: Send + 'static,
        F: Fn(Out) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = StreamResult<Next>> + Send + 'static,
    {
        SourceWithContext::from_source(self.delegate.map_async(parallelism, move |(out, ctx)| {
            f(out).map(|next| next.map(|next| (next, ctx)))
        }))
    }

    pub fn map_context<CtxOut, F>(self, f: F) -> SourceWithContext<Out, CtxOut, Mat>
    where
        CtxOut: Send + 'static,
        F: Fn(Ctx) -> CtxOut + Send + Sync + 'static,
    {
        SourceWithContext::from_source(self.delegate.map(move |(out, ctx)| (out, f(ctx))))
    }

    pub fn grouped(self, size: usize) -> SourceWithContext<Vec<Out>, Vec<Ctx>, Mat> {
        SourceWithContext::from_source(self.delegate.grouped(size).map(unzip_pairs))
    }

    pub fn sliding(self, size: usize, step: usize) -> SourceWithContext<Vec<Out>, Vec<Ctx>, Mat>
    where
        Out: Clone,
        Ctx: Clone,
    {
        SourceWithContext::from_source(self.delegate.sliding(size, step).map(unzip_pairs))
    }

    pub fn via<Out2, Ctx2, FlowMat>(
        self,
        flow: FlowWithContext<Out, Ctx, Out2, Ctx2, FlowMat>,
    ) -> SourceWithContext<Out2, Ctx2, Mat>
    where
        Out2: Send + 'static,
        Ctx2: Send + 'static,
        FlowMat: Send + 'static,
    {
        SourceWithContext::from_source(self.delegate.via(flow.delegate))
    }

    pub fn via_mat<Out2, Ctx2, FlowMat, Combined, F>(
        self,
        flow: FlowWithContext<Out, Ctx, Out2, Ctx2, FlowMat>,
        combine: F,
    ) -> SourceWithContext<Out2, Ctx2, Combined>
    where
        Out2: Send + 'static,
        Ctx2: Send + 'static,
        FlowMat: Send + 'static,
        Combined: Send + 'static,
        F: Fn(Mat, FlowMat) -> Combined + Send + Sync + 'static,
    {
        SourceWithContext::from_source(self.delegate.via_mat(flow.delegate, combine))
    }

    pub fn to<SinkMat>(self, sink: Sink<(Out, Ctx), SinkMat>) -> RunnableGraph<Mat>
    where
        SinkMat: Send + 'static,
    {
        self.delegate.to(sink)
    }

    pub fn to_mat<SinkMat, Combined, F>(
        self,
        sink: Sink<(Out, Ctx), SinkMat>,
        combine: F,
    ) -> RunnableGraph<Combined>
    where
        SinkMat: Send + 'static,
        Combined: Send + 'static,
        F: Fn(Mat, SinkMat) -> Combined + Send + Sync + 'static,
    {
        self.delegate.to_mat(sink, combine)
    }
}

impl<In: Send + 'static, CtxIn: Send + 'static> FlowWithContext<In, CtxIn, In, CtxIn, NotUsed> {
    pub fn identity() -> Self {
        FlowWithContext::from_flow(Flow::identity())
    }
}

impl<
    In: Send + 'static,
    CtxIn: Send + 'static,
    Out: Send + 'static,
    CtxOut: Send + 'static,
    Mat: Send + 'static,
> FlowWithContext<In, CtxIn, Out, CtxOut, Mat>
{
    pub(crate) fn from_flow(
        delegate: Flow<(In, CtxIn), (Out, CtxOut), Mat>,
    ) -> FlowWithContext<In, CtxIn, Out, CtxOut, Mat> {
        FlowWithContext { delegate }
    }

    pub fn as_flow(self) -> Flow<(In, CtxIn), (Out, CtxOut), Mat> {
        self.delegate
    }

    pub fn map<Next, F>(self, f: F) -> FlowWithContext<In, CtxIn, Next, CtxOut, Mat>
    where
        Next: Send + 'static,
        F: Fn(Out) -> Next + Send + Sync + 'static,
    {
        FlowWithContext::from_flow(self.delegate.map(move |(out, ctx)| (f(out), ctx)))
    }

    pub fn filter<F>(self, predicate: F) -> FlowWithContext<In, CtxIn, Out, CtxOut, Mat>
    where
        F: Fn(&Out) -> bool + Send + Sync + 'static,
    {
        FlowWithContext::from_flow(self.delegate.filter(move |(out, _)| predicate(out)))
    }

    pub fn filter_not<F>(self, predicate: F) -> FlowWithContext<In, CtxIn, Out, CtxOut, Mat>
    where
        F: Fn(&Out) -> bool + Send + Sync + 'static,
    {
        self.filter(move |out| !predicate(out))
    }

    pub fn filter_map<Next, F>(self, f: F) -> FlowWithContext<In, CtxIn, Next, CtxOut, Mat>
    where
        Next: Send + 'static,
        F: Fn(Out) -> Option<Next> + Send + Sync + 'static,
    {
        FlowWithContext::from_flow(
            self.delegate
                .filter_map(move |(out, ctx)| f(out).map(|item| (item, ctx))),
        )
    }

    pub fn map_concat<Next, F, I>(self, f: F) -> FlowWithContext<In, CtxIn, Next, CtxOut, Mat>
    where
        Next: Send + 'static,
        F: Fn(Out) -> I + Send + Sync + 'static,
        I: IntoIterator<Item = Next>,
        I::IntoIter: Send + 'static,
        CtxOut: Clone,
    {
        FlowWithContext::from_flow(self.delegate.map_concat(move |(out, ctx)| {
            let ctx = ctx.clone();
            f(out).into_iter().map(move |next| (next, ctx.clone()))
        }))
    }

    pub fn map_async<Next, F, Fut>(
        self,
        parallelism: usize,
        f: F,
    ) -> FlowWithContext<In, CtxIn, Next, CtxOut, Mat>
    where
        Next: Send + 'static,
        F: Fn(Out) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = StreamResult<Next>> + Send + 'static,
    {
        FlowWithContext::from_flow(self.delegate.map_async(parallelism, move |(out, ctx)| {
            f(out).map(|next| next.map(|next| (next, ctx)))
        }))
    }

    pub fn map_context<CtxOut2, F>(self, f: F) -> FlowWithContext<In, CtxIn, Out, CtxOut2, Mat>
    where
        CtxOut2: Send + 'static,
        F: Fn(CtxOut) -> CtxOut2 + Send + Sync + 'static,
    {
        FlowWithContext::from_flow(self.delegate.map(move |(out, ctx)| (out, f(ctx))))
    }

    pub fn grouped(self, n: usize) -> FlowWithContext<In, CtxIn, Vec<Out>, Vec<CtxOut>, Mat> {
        FlowWithContext::from_flow(self.delegate.grouped(n).map(unzip_pairs))
    }

    pub fn sliding(
        self,
        n: usize,
        step: usize,
    ) -> FlowWithContext<In, CtxIn, Vec<Out>, Vec<CtxOut>, Mat>
    where
        Out: Clone,
        CtxOut: Clone,
    {
        FlowWithContext::from_flow(self.delegate.sliding(n, step).map(unzip_pairs))
    }

    pub fn via<Out2, Ctx2, FlowMat>(
        self,
        flow: FlowWithContext<Out, CtxOut, Out2, Ctx2, FlowMat>,
    ) -> FlowWithContext<In, CtxIn, Out2, Ctx2, Mat>
    where
        Out2: Send + 'static,
        Ctx2: Send + 'static,
        FlowMat: Send + 'static,
    {
        FlowWithContext::from_flow(self.delegate.via(flow.delegate))
    }

    pub fn to<SinkMat>(self, sink: Sink<(Out, CtxOut), SinkMat>) -> Sink<(In, CtxIn), Mat>
    where
        SinkMat: Send + 'static,
    {
        self.delegate.to(sink)
    }

    pub fn to_mat<SinkMat, Combined, F>(
        self,
        sink: Sink<(Out, CtxOut), SinkMat>,
        combine: F,
    ) -> Sink<(In, CtxIn), Combined>
    where
        SinkMat: Send + 'static,
        Combined: Send + 'static,
        F: Fn(Mat, SinkMat) -> Combined + Send + Sync + 'static,
    {
        self.delegate.to_mat(sink, combine)
    }
}

fn unzip_pairs<Out, Ctx>(pairs: Vec<(Out, Ctx)>) -> (Vec<Out>, Vec<Ctx>) {
    let mut outs = Vec::with_capacity(pairs.len());
    let mut ctxs = Vec::with_capacity(pairs.len());

    for (out, ctx) in pairs {
        outs.push(out);
        ctxs.push(ctx);
    }

    (outs, ctxs)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{thread, time::Duration};

    #[test]
    fn source_with_context_preserves_context_for_map_and_filter() {
        let values = Source::from_iter(0_i32..6)
            .as_source_with_context(|item| item + 100)
            .map(|item| item + 1)
            .filter(|item| item % 2 == 0)
            .filter_not(|item| *item == 4)
            .run_collect()
            .unwrap();

        assert_eq!(values, vec![(2, 101), (6, 105)]);
    }

    #[test]
    fn source_with_context_filters_context_with_map_filter() {
        let values = Source::from_iter(1_i32..5)
            .as_source_with_context(|item| item * 10)
            .filter(|item| item % 2 == 1)
            .run_collect()
            .unwrap();

        assert_eq!(values, vec![(1, 10), (3, 30)]);
    }

    #[test]
    fn source_with_context_map_context_transform_is_supported() {
        let values = Source::from_iter(1_i32..4)
            .as_source_with_context(|item| *item)
            .map_context(|ctx| ctx + 10)
            .run_collect()
            .unwrap();

        assert_eq!(values, vec![(1, 11), (2, 12), (3, 13)]);
    }

    #[test]
    fn source_with_context_map_concat_duplicates_context() {
        let values = Source::from_iter([1_i32, 2])
            .as_source_with_context(|item| item + 10)
            .map_concat(|item| vec![item + 1, item + 2])
            .run_collect()
            .unwrap();

        assert_eq!(values, vec![(2, 11), (3, 11), (3, 12), (4, 12)]);
    }

    #[test]
    fn source_with_context_groups_context_vectors_with_grouped_and_sliding() {
        let grouped = Source::from_iter(1_i32..4)
            .as_source_with_context(|item| item + 10)
            .grouped(2)
            .run_collect()
            .unwrap();

        assert_eq!(
            grouped,
            vec![(vec![1, 2], vec![11, 12]), (vec![3], vec![13])]
        );

        let sliding = Source::from_iter([1_i32, 2, 3, 4])
            .as_source_with_context(|item| item + 10)
            .sliding(3, 2)
            .run_collect()
            .unwrap();

        assert_eq!(
            sliding,
            vec![
                (vec![1, 2, 3], vec![11, 12, 13]),
                (vec![3, 4], vec![13, 14])
            ]
        );
    }

    #[test]
    fn source_with_context_map_async_keeps_context_with_out_of_order_completions() {
        let values = Source::from_iter([3_i32, 1, 2, 0])
            .as_source_with_context(|item| item + 100)
            .map_async(2, |item| async move {
                if item % 2 == 0 {
                    thread::sleep(Duration::from_millis(20));
                } else {
                    thread::sleep(Duration::from_millis(2));
                }
                Ok(item * 2)
            })
            .run_collect()
            .unwrap();

        assert_eq!(values, vec![(6, 103), (2, 101), (4, 102), (0, 100)]);
    }

    #[test]
    fn source_with_context_filter_and_map_with_string_elements() {
        let input = ["a".to_string(), "bravo".to_string(), "charlie".to_string()];

        let source_values = Source::from_iter(input.to_vec())
            .as_source_with_context(|item| format!("ctx-{item}"))
            .filter(|item| item.len() >= 5)
            .map(|item| format!("mapped:{item}"))
            .run_collect()
            .unwrap();

        assert_eq!(
            source_values,
            vec![
                ("mapped:bravo".to_string(), "ctx-bravo".to_string()),
                ("mapped:charlie".to_string(), "ctx-charlie".to_string()),
            ]
        );

        let flow_values = Source::from_iter(input)
            .as_source_with_context(|item| format!("ctx-{item}"))
            .via(
                FlowWithContext::<String, String, String, String, NotUsed>::identity()
                    .filter(|item| item.len() >= 5)
                    .map(|item| format!("mapped:{item}")),
            )
            .run_collect()
            .unwrap();

        assert_eq!(flow_values, source_values);
    }

    #[test]
    fn source_with_context_via_context_flow_and_to_mat() {
        let flow = FlowWithContext::<i32, i32, i32, i32, NotUsed>::from_flow(
            Flow::identity().map(|(value, ctx)| (value * 2, ctx * 2)),
        );

        let sink = Sink::collect();
        let completion = Source::from_iter([1_i32, 2, 3])
            .as_source_with_context(|item| item + 10)
            .via(flow)
            .to_mat(sink, |_, mat| mat)
            .run()
            .unwrap();

        assert_eq!(completion.wait().unwrap(), vec![(2, 22), (4, 24), (6, 26)]);
    }

    #[test]
    fn source_with_context_as_source_to_runs_and_to_mat_work() {
        let source = Source::from_iter([1_i32, 2, 3]).as_source_with_context(|item| item + 10);

        assert_eq!(source.clone().to(Sink::ignore()).run(), Ok(NotUsed));

        let pair_sink = source
            .to_mat(Sink::collect(), |_, pairs| pairs)
            .run()
            .unwrap();
        assert_eq!(pair_sink.wait().unwrap(), vec![(1, 11), (2, 12), (3, 13)]);
    }
}