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
//! Create a lightweight, concurrent data processing pipeline for Rust applications.
//!
//! # Overview
//!
//! Async Pipes provides a simple way to create high-throughput data processing pipelines by
//! utilizing Rust's asynchronous runtime capabilities. This is done by managing task execution and
//! data flow so the developer only has to worry about the task-specific implementation for each
//! stage in the pipeline.
//!
//! # Terminology
//!
//! All of these are abstractions to help conceptualize how data is transferred and operated on in
//! the pipeline.
//!
//! * **Pipe** - Represents something where a type of data can flow.
//!   An example of this being a pipe that allows strings to flow through it.
//! * **Stage** - Represents the "nodes" in a pipeline where work is done.
//!   A stage typically includes the definition of the worker, an optional pipe connection
//!   for reading data from, and zero or more pipe connections for sending data to.
//! * **Worker** - A worker is internally defined by this library, and does the work of
//!   reading from the optional input pipe, performing a user-defined task on the input, and
//!   then writing the output of that task to the zero or more output pipes.
//! * **Pipeline** - Represents the overall set of stages and the pipes that connect the stages.
//!   Pipelines don't necessarily have to be linear, they can branch off of one stage into
//!   multiple stages.
//!
//! # Getting Started
//!
//! A pipeline can be built using the builder provided by [Pipeline::builder]. This allows the
//! pipeline to be configured before any work is done.
//! ```
//! use async_pipes::{Pipeline, PipelineBuilder};
//!
//! let builder: PipelineBuilder = Pipeline::builder();
//! ```
//!
//! Using the builder, stages can be defined, where a stage contains the name of a pipe to read from
//! (if applicable), the name of a pipe to write to (or more if applicable), and a user-defined
//! "task" function. Demonstrated below is a pipeline being built with a producer stage, a regular
//! stage, and a consuming stage.
//! ```
//! use async_pipes::Pipeline;
//!
//! #[tokio::main]
//! async fn main() {
//!     let pipeline: Result<Pipeline, String> = Pipeline::builder()
//!         .with_inputs("InputPipe", vec![1, 2, 3])
//!         .with_stage("InputPipe", "OutputPipe", |n: i32| async move {
//!             Some(n + 1)
//!         })
//!         .with_consumer("OutputPipe", |n: i32| async move {
//!             println!("{}", n)
//!         })
//!         .build();
//!
//!     assert!(pipeline.is_ok());
//! }
//! ```
//!
//! With the builder, any number of stages can be defined with any number of pipes, but there are a
//! few requirements:
//! 1. There must be at least one producer - how else will data get into the pipeline?
//! 2. Every pipe must have a corresponding stage that reads data from it - this is required to
//!    avoid a deadlock from pipes being filled up but not emptied.
//!
//! These requirements are enforced by [PipelineBuilder::build] returning a
//! [Result<Pipeline, String>] where an error describing the missing requirement is returned.
//!
//! For example, here is an invalid pipeline due to requirement (1) not being followed:
//! ```
//! use async_pipes::Pipeline;
//!
//! #[tokio::main]
//! async fn main() {
//!     let pipeline = Pipeline::builder()
//!         .with_consumer("MyPipe", |n: usize| async move {
//!             println!("{}", n);
//!         })
//!         .build();
//!
//!     assert_eq!(pipeline.unwrap_err(), "pipeline must have at least one producer");
//! }
//! ```
//!
//! And here is an invalid pipeline due to requirement (2) not being followed:
//! ```
//! use async_pipes::Pipeline;
//!
//! #[tokio::main]
//! async fn main() {
//!     let pipeline = Pipeline::builder()
//!         .with_inputs("MyPipe", vec![1, 2, 3])
//!         .build();
//!
//!     assert_eq!(pipeline.unwrap_err(), "pipeline has open-ended pipe: 'MyPipe'");
//! }
//! ```
//!
//! Once an `Ok(Pipeline)` is returned, it can be waited on using [Pipeline::wait], where it will
//! make progress until all workers finish or there is no more data in the pipeline.
//!
//! _Note_: When a pipeline is built, depending on the runtime it may or may not be running.
//! In single-threaded runtimes no progress will be made as the workers can't make progress on their
//! own unless the single thread yields to them. It is possible for them to make progress in multi-
//! threaded runtimes. However, the pipeline will never "finish" until [Pipeline::wait] is called.
//!
//! ```
//! use async_pipes::Pipeline;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), String> {
//!     Pipeline::builder()
//!         .with_inputs("InputPipe", vec![1, 2, 3])
//!         .with_stage("InputPipe", "OutputPipe", |n: i32| async move {
//!             Some(n + 1)
//!         })
//!         .with_consumer("OutputPipe", |n: i32| async move {
//!             println!("{}", n)
//!         })
//!         .build()?
//!         .wait()
//!         .await;
//!
//!     Ok(())
//! }
//! ```
//!
//! ### Stateful Stages
//!
//! It is possible to maintain state in a stage across tasks, however the state must be [Send].
//! Usually this is best done for non-Send objects by wrapping them in an [std::sync::Mutex]
//! (or even better, [tokio::sync::Mutex]).
//!
//! Another caveat with state in stages is that since the task function returns a future
//! (`async move { ... }`), it requires ownership of non-`'static` lifetime values in order to
//! continue working on other inputs as the future may not be able to reference borrowed state.
//! A way around this is to wrap values that may be expensive to clone in [std::sync::Arc].
//!
//! The following is an example of a mutable sum being used as a stateful item in a stage:
//! ```
//! use async_pipes::Pipeline;
//! use std::sync::Arc;
//! use tokio::sync::Mutex;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), String> {
//!     // [AtomicUsize] may be preferred here, but we use [Mutex] for the sake of this example
//!     let sum = Arc::new(Mutex::new(0));
//!     // For the assertion at the end of this example
//!     let test_sum = sum.clone();
//!
//!     Pipeline::builder()
//!         .with_inputs("InputPipe", vec![1, 2, 3])
//!         .with_stage("InputPipe", "OutputPipe", move |n: i32| {
//!             // As the sum is owned by this closure, we need to clone it to move an owned value
//!             // into the `async move` block.
//!             let sum = sum.clone();
//!             async move {
//!                 let mut sum = sum.lock().await;
//!                 *sum += n;
//!                 Some(*sum)
//!             }
//!         })
//!         .with_consumer("OutputPipe", |n: i32| async move {
//!             println!("Counter now at: {}", n)
//!         })
//!         .build()?
//!         .wait()
//!         .await;
//!
//!     assert_eq!(*test_sum.lock().await, 6);
//!     Ok(())
//! }
//! ```
//!
//! # Stage Categories <a name="stage-categories"></a>
//!
//! ### Producer ("entry stage")
//! A producer is the only place where data can be fed into the pipeline.
//!
//! **Static (definite)**
//!
//! This is where a list of concrete values can be provided to the stage and the worker will loop
//! over each value and feed it into a pipe.
//! * [PipelineBuilder::with_inputs]
//! * [PipelineBuilder::with_branching_inputs]
//!
//! **Dynamic (indefinite)**
//!
//! This is useful when there are no pre-defined input values. Instead, a function that produces a
//! single value can be provided that produces an [Option] where it's continually called until
//! [None] is returned. This can be useful when receiving data over the network, or data is read
//! from a file.
//! * [PipelineBuilder::with_producer]
//! * [PipelineBuilder::with_branching_producer]
//!
//! ### Consumer ("terminating stage")
//! A consumer is a final stage in the pipeline where data ends up. It takes in a single pipe to
//! read from and produces no output.
//! * [PipelineBuilder::with_consumer]
//!
//! ### Regular (1 input, 1 output)
//! This is an intermediate stage in the pipeline that takes in a single input, and produces one or
//! more output.
//! * [PipelineBuilder::with_stage]
//! * [PipelineBuilder::with_branching_stage]
//!
//! ### Utility
//! This is an intermediate stage in the pipeline that can be used to do common operations on data
//! between pipes.
//! * [PipelineBuilder::with_flattener]
//!
//! # Stage Variants
//!
//! ### Branching (1 input, N outputs)
//! A branching stage is a stage where multiple output pipes are connected. This means the task
//! defined by the user in this stage returns two or more output values.
//! * [PipelineBuilder::with_branching_inputs]
//! * [PipelineBuilder::with_branching_producer]
//! * [PipelineBuilder::with_branching_stage]
//!
//! # Examples
//!
//! ```
//! use std::sync::Arc;
//!
//! use async_pipes::Pipeline;
//!
//! use std::sync::atomic::{AtomicUsize, Ordering};
//! use tokio::sync::Mutex;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), String> {
//!     // Due to the task function returning a future (`async move { ... }`), data needs
//!     // to be wrapped in an [Arc] and then cloned in order to be moved into the task
//!     // while still referencing it from this scope
//!     let total_count = Arc::new(AtomicUsize::new(0));
//!     let task_total_count = total_count.clone();
//!
//!     Pipeline::builder()
//!         .with_inputs("MapPipe", vec!["a", "bb", "ccc"])
//!
//!         // Read from the 'MapPipe' and write to the 'ReducePipe'
//!         .with_stage("MapPipe", "ReducePipe", |value: &'static str| async move {
//!             // We return an option to tell the stage whether to write the new value
//!             // to the pipe or ignore it
//!             Some(format!("{}!", value))
//!         })
//!
//!         // Read from the 'ReducePipe'.
//!         .with_consumer("ReducePipe", move |value: String| {
//!             // The captured `task_total_count` can't move out of this closure, so we
//!             // have to clone it to give ownership to the async block. Remember, it's
//!             // wrapped in an [Arc] so we're still referring to the original data.
//!             let total_count = task_total_count.clone();
//!             async move {
//!                 total_count.fetch_add(value.len(), Ordering::SeqCst);
//!             }
//!         })
//!
//!         // Build the pipeline and wait for it to finish
//!         .build()?
//!         .wait()
//!         .await;
//!
//!     // We see that after the data goes through our map and reduce stages,
//!     // we effectively get this: `len("a!") + len("bb!") + len("ccc!") = 9`
//!     assert_eq!(total_count.load(Ordering::Acquire), 9);
//!     Ok(())
//! }
//! ```
//!
#![warn(missing_docs)]

pub use pipeline::*;

mod pipeline;

/// A value used in coordination with [branch] to indicate there is no value to be sent to a
/// pipe.
///
/// # Examples
///
/// ```
/// use async_pipes::{NoOutput, BoxedAnySend, branch};
///
/// let outputs: Vec<Option<BoxedAnySend>> = branch![
///     "one",
///     NoOutput,
///     3,
/// ];
///
/// assert!(outputs[0].is_some());
/// assert!(outputs[1].is_none());
/// assert!(outputs[2].is_some());
/// ```
#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub struct NoOutput;

/// Defines an idiomatic way to provide values to a static branching producer stage (i.e. concrete
/// input values).
///
/// A list of tuples of values (of possibly different types) can be provided, and those values will be boxed
/// and then put into a [Vec].
///
/// # Examples
///
/// Here's an example of what is returned by the macro call.
/// ```
/// use async_pipes::{BoxedAnySend, branch_inputs};
///
/// let inputs: Vec<Vec<BoxedAnySend>> = branch_inputs![
///     (1usize, 1i32, 1u8),
///     (2usize, 2i32, 2u8),
///     (3usize, 3i32, 3u8),
/// ];
///
/// assert_eq!(inputs.len(), 3);
/// ```
///
/// Here's an example of the macro being used in a pipeline.
/// ```
/// use async_pipes::{branch_inputs, Pipeline};
///
/// #[tokio::main]
/// async fn main() {
///     Pipeline::builder()
///         .with_branching_inputs(
///             vec!["One", "Two"],
///             branch_inputs![
///                 (1usize, "Hello"),
///                 (1usize, "World"),
///                 (1usize, "!"),
///             ],
///         )
///         .with_consumer("One", |value: usize| async move { /* ... */ })
///         .with_consumer("Two", |value: &'static str| async move { /* ... */ })
///         .build()
///         .unwrap()
///         .wait()
///         .await;
/// }
/// ```
#[macro_export]
macro_rules! branch_inputs {
    ($(( $($x:expr),+ $(,)? )),* $(,)?) => {
        vec![
            $( branch_inputs!($($x),+) ),*
        ]
    };

    ($($x:expr),+ $(,)?) => {
        vec![
            $(std::boxed::Box::new($x) as $crate::BoxedAnySend),+
        ]
    };
}

/// Defines an idiomatic way to return values in a branching stage.
///
/// A list of values (possibly of different types) can be provided. These values will be boxed and
/// then wrapped in a [Some]. In order to specify [None] (i.e. no value should be sent to the
/// respective pipe), [NoOutput] should be used in the place of a value. The macro will detect this
/// and use [None] in its place.
///
/// # Examples
///
/// Here's an example of what is returned by the macro call.
/// ```
/// use async_pipes::{BoxedAnySend, NoOutput, branch};
///
/// let inputs: Vec<Option<BoxedAnySend>> = branch![1, "hello", true, NoOutput, 12.0];
///
/// assert_eq!(inputs.len(), 5);
/// assert!(inputs[3].is_none())
/// ```
///
/// Here's an example of the macro being used in a pipeline.
/// ```
/// use async_pipes::{branch, branch_inputs, Pipeline};
/// use std::sync::atomic::{AtomicUsize, Ordering};
/// use std::sync::Arc;
///
/// #[tokio::main]
/// async fn main() {
///     Pipeline::builder()
///         .with_inputs("Count", vec![1, 2, 3])
///         .with_branching_stage("Count", vec!["Value", "Doubled"], |value: i32| async move {
///             Some(branch![value, value * 2])
///         })
///         .with_consumer("Value", |value: i32| async move { /* ... */ })
///         .with_consumer("Doubled", |value: i32| async move { /* ... */ })
///         .build()
///         .unwrap()
///         .wait()
///         .await;
/// }
/// ```
#[macro_export]
macro_rules! branch {
    ($($x:expr),+ $(,)?) => {
        std::vec![
            $({
                let x: $crate::BoxedAnySend = std::boxed::Box::new($x);
                x.downcast_ref::<$crate::NoOutput>().is_none().then_some(x)
            }),+
        ]
    };
}