Macro branch

Source
macro_rules! branch {
    ($($x:expr),+ $(,)?) => { ... };
}
Expand description

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;
use async_pipes::WorkerOptions;

#[tokio::main]
async fn main() {
    Pipeline::builder()
        .with_inputs("Count", vec![1, 2, 3])
        .with_branching_stage(
            "Count",
            vec!["Value", "Doubled"],
            WorkerOptions::default(),
            |value: i32| async move {
                Some(branch![value, value * 2])
            }
        )
        .with_consumer("Value", WorkerOptions::default(), |value: i32| async move {
            /* ... */
        })
        .with_consumer("Doubled", WorkerOptions::default(), |value: i32| async move {
            /* ... */
        })
        .build()
        .unwrap()
        .wait()
        .await;
}