Skip to main content

ferrite_session/internal/session/
wrap.rs

1use crate::internal::{
2  base::*,
3  functional::App,
4  protocol::*,
5};
6
7pub fn wrap_session<C, T>(
8  cont: PartialSession<C, T::Unwrap>
9) -> PartialSession<C, Wrap<T>>
10where
11  C: Context,
12  T: Wrapper,
13  T: Send + 'static,
14  T::Unwrap: Protocol,
15{
16  unsafe_create_session::<C, Wrap<T>, _, _>(move |ctx, sender1| async move {
17    let (provider_end, client_end) = T::Unwrap::create_endpoints();
18
19    sender1
20      .send(Wrap {
21        unwrap: Box::new(client_end),
22      })
23      .unwrap();
24
25    unsafe_run_session(cont, ctx, provider_end).await;
26  })
27}
28
29pub fn unwrap_session<N, C, T, A>(
30  _: N,
31  cont: PartialSession<N::Target, A>,
32) -> PartialSession<C, A>
33where
34  C: Context,
35  A: Protocol,
36  T: Wrapper + Send + 'static,
37  N: ContextLens<C, Wrap<T>, T::Unwrap>,
38{
39  unsafe_create_session(move |ctx1, provider_end_a| async move {
40    let (endpoint, ctx2) = N::extract_source(ctx1);
41
42    let receiver = endpoint.get_applied();
43
44    let wrapped = receiver.recv().await.unwrap();
45
46    let unwrapped = wrapped.unwrap.unwrap();
47
48    let ctx3 = N::insert_target(App::new(unwrapped), ctx2);
49
50    unsafe_run_session(cont, ctx3, provider_end_a).await;
51  })
52}