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 crate::internal::{
  base::{
    unsafe_create_session,
    unsafe_run_session,
    Context,
    ContextLens,
    PartialSession,
    Protocol,
    Value,
  },
  functional::App,
  protocol::SendValue,
};

pub fn send_value<T, C, A>(
  val: T,
  cont: PartialSession<C, A>,
) -> PartialSession<C, SendValue<T, A>>
where
  T: Send + 'static,
  A: Protocol,
  C: Context,
{
  unsafe_create_session::<C, SendValue<T, A>, _, _>(
    move |ctx, sender1| async move {
      let (provider_end, client_end) = A::create_endpoints();
      sender1.send((Value(val), client_end)).unwrap();

      unsafe_run_session(cont, ctx, provider_end).await;
    },
  )
}

pub fn receive_value_from<N, C1, C2, T, A, B>(
  _n: N,
  cont: impl FnOnce(T) -> PartialSession<C2, B> + Send + 'static,
) -> PartialSession<C1, B>
where
  A: Protocol,
  B: Protocol,
  C1: Context,
  C2: Context,
  T: Send + 'static,
  N: ContextLens<C1, SendValue<T, A>, A, Target = C2>,
{
  unsafe_create_session(move |ctx1, sender| async move {
    let (endpoint, ctx2) = N::extract_source(ctx1);

    let receiver = endpoint.get_applied();

    let (Value(val), client_end) = receiver.recv().await.unwrap();

    let ctx3 = N::insert_target(App::new(client_end), ctx2);

    let cont2 = cont(val);

    unsafe_run_session(cont2, ctx3, sender).await;
  })
}