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
use crate::internal::{
  base::{
    unsafe_create_session,
    unsafe_run_session,
    AppendContext,
    Context,
    ContextLens,
    Empty,
    PartialSession,
    Protocol,
  },
  functional::{
    App,
    Nat,
  },
  protocol::ReceiveChannel,
};

pub fn receive_channel<C1, C2, N, A, B>(
  cont: impl FnOnce(N) -> PartialSession<C2, B>
) -> PartialSession<C1, ReceiveChannel<A, B>>
where
  N: Nat,
  A: Protocol,
  B: Protocol,
  C1: Context<Length = N>,
  C2: Context,
  C1: AppendContext<(A, ()), Appended = C2>,
{
  let cont2 = cont(N::nat());

  unsafe_create_session::<C1, ReceiveChannel<A, B>, _, _>(
    move |ctx1, (chan_receiver, provider_end)| async move {
      let client_end = chan_receiver.recv().await.unwrap();

      let ctx2 = C1::append_context(ctx1, (App::new(client_end), ()));

      unsafe_run_session(cont2, ctx2, provider_end).await;
    },
  )
}

pub fn send_channel_to<N, M, C1, C2, C3, A1, A2, B>(
  _n: N,
  _m: M,
  cont: PartialSession<C3, B>,
) -> PartialSession<C1, B>
where
  C1: Context,
  C2: Context,
  C3: Context,
  A1: Protocol,
  A2: Protocol,
  B: Protocol,
  N: ContextLens<C2, ReceiveChannel<A1, A2>, A2, Target = C3>,
  M: ContextLens<C1, A1, Empty, Target = C2>,
{
  unsafe_create_session(move |ctx1, provider_end_b| async move {
    let (client_end_1, ctx2) = M::extract_source(ctx1);

    let ctx3 = M::insert_target((), ctx2);

    let (endpoint, ctx4) = N::extract_source(ctx3);

    let (chan_sender, client_end_2) = endpoint.get_applied();

    chan_sender.send(client_end_1.get_applied()).unwrap();

    let ctx5 = N::insert_target(App::new(client_end_2), ctx4);

    unsafe_run_session(cont, ctx5, provider_end_b).await;
  })
}