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
use std::time::Duration;

pub use ferrite_session::prelude::*;
use tokio::time::sleep;

type Producer = SendValue<String, End>;

fn cut_session() -> Session<End>
{
  let client: Session<
    ReceiveChannel<
      Producer,
      ReceiveChannel<Producer, ReceiveChannel<Producer, End>>,
    >,
  > = receive_channels! ( (c1, c2, c3) => {
    cut! {
      [ R, L, R ] ;
      receive_value_from ( c2, move |x2| {
        step(async move {
          println! ("[right] got x2: {}", x2);
          sleep(Duration::from_secs(1)).await;

          wait( c2,
            terminate_async(|| async {
              println! ("[right] terminating");
            }) )
        })
      });
      c4 => {
        receive_value_from( c1, move |x1| {
          println! ("[left] got x1: {}", x1);

          receive_value_from ( c3, move |x3| {
            println! ("[left] got x3: {}", x3);

            wait_all! ( [ c1, c3, c4 ],
              terminate_async (|| async {
                println! ("[left] terminating");
              }) )
          })
        })
      }
    }
  });

  let p1: Session<Producer> = send_value("foo".to_string(), terminate());

  let p2: Session<Producer> = send_value("bar".to_string(), terminate());

  let p3: Session<Producer> = send_value("baz".to_string(), terminate());

  apply_channel(apply_channel(apply_channel(client, p1), p2), p3)
}

#[tokio::main]
pub async fn main()
{
  run_session(cut_session()).await;
}