-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrap.rs
89 lines (77 loc) · 1.63 KB
/
wrap.rs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use async_macros::join;
use async_std::task;
use async_std::sync::{ channel };
use crate::base::*;
use crate::protocol::*;
pub fn wrap_session
< C, T >
( cont :
PartialSession <
C,
T :: Unwrap
>
) ->
PartialSession <
C,
Wrap < T >
>
where
C : Context,
T : Wrapper,
T : Send + 'static,
T :: Unwrap : Protocol,
{
unsafe_create_session (
async move | ctx, sender1 | {
let (sender2, receiver) = channel(1);
let child1 = task::spawn(async move {
let val = receiver.recv().await.unwrap();
sender1.send (
Wrap { unwrap : Box::new ( val ) }
).await;
});
let child2 = task::spawn(
unsafe_run_session
( cont, ctx, sender2
) );
join!(child1, child2).await;
})
}
pub fn unwrap_session
< N, C, T, A >
( _ : N,
cont :
PartialSession <
N :: Target,
A
>
) ->
PartialSession < C, A >
where
C : Context,
A : Protocol,
T : Wrapper + Send + 'static,
N :
ContextLens <
C,
Wrap < T >,
T :: Unwrap
>
{
unsafe_create_session(
async move | ctx1, sender1 | {
let (receiver1, ctx2) = N :: extract_source ( ctx1 );
let (sender2, receiver2) = channel(1);
let ctx3 =
N :: insert_target ( receiver2, ctx2 );
let child1 = task::spawn ( async move {
let wrapped = receiver1.recv().await.unwrap();
sender2.send( *wrapped.unwrap ).await;
});
let child2 = task::spawn(
unsafe_run_session
( cont, ctx3, sender1
));
join!(child1, child2).await;
})
}