Skip to main content

ferrite_session/internal/session/value/
send.rs

1use crate::internal::{
2  base::{
3    unsafe_create_session,
4    unsafe_run_session,
5    Context,
6    ContextLens,
7    PartialSession,
8    Protocol,
9    Value,
10  },
11  functional::App,
12  protocol::SendValue,
13};
14
15pub fn send_value<T, C, A>(
16  val: T,
17  cont: PartialSession<C, A>,
18) -> PartialSession<C, SendValue<T, A>>
19where
20  T: Send + 'static,
21  A: Protocol,
22  C: Context,
23{
24  unsafe_create_session::<C, SendValue<T, A>, _, _>(
25    move |ctx, sender1| async move {
26      let (provider_end, client_end) = A::create_endpoints();
27      sender1.send((Value(val), client_end)).unwrap();
28
29      unsafe_run_session(cont, ctx, provider_end).await;
30    },
31  )
32}
33
34pub fn receive_value_from<N, C1, C2, T, A, B>(
35  _n: N,
36  cont: impl FnOnce(T) -> PartialSession<C2, B> + Send + 'static,
37) -> PartialSession<C1, B>
38where
39  A: Protocol,
40  B: Protocol,
41  C1: Context,
42  C2: Context,
43  T: Send + 'static,
44  N: ContextLens<C1, SendValue<T, A>, A, Target = C2>,
45{
46  unsafe_create_session(move |ctx1, sender| async move {
47    let (endpoint, ctx2) = N::extract_source(ctx1);
48
49    let receiver = endpoint.get_applied();
50
51    let (Value(val), client_end) = receiver.recv().await.unwrap();
52
53    let ctx3 = N::insert_target(App::new(client_end), ctx2);
54
55    let cont2 = cont(val);
56
57    unsafe_run_session(cont2, ctx3, sender).await;
58  })
59}