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
use hash_map_id::HashMapId;
use lunatic::{process::{AbstractProcess, ProcessRef, RequestHandler, StartProcess, Request}};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Envelop(u64);
pub fn ensure_spawned() {
get_ref();
}
pub fn create_envelop(buf: Vec<u8>) -> Envelop {
Envelop(get_ref().request(serde_bytes::ByteBuf::from(buf)))
}
pub fn open_envelop(buf: Envelop) -> Vec<u8> {
get_ref().request(buf.0).into_vec()
}
fn get_ref() -> ProcessRef<P> {
ProcessRef::lookup("__envelop_process").or_else(|| Some(P::start((), Some("__envelop_process")))).unwrap()
}
struct P(HashMapId<serde_bytes::ByteBuf>);
impl AbstractProcess for P {
type Arg = ();
type State = P;
fn init(_: ProcessRef<Self>, _: ()) -> P {
P(HashMapId::new())
}
fn terminate(_: Self::State) {
}
}
impl RequestHandler<serde_bytes::ByteBuf> for P {
type Response = u64;
fn handle(state: &mut Self::State, request: serde_bytes::ByteBuf) -> Self::Response {
state.0.add(request)
}
}
impl RequestHandler<u64> for P {
type Response = serde_bytes::ByteBuf;
fn handle(state: &mut Self::State, request: u64) -> Self::Response {
state.0.remove(request).or(Some(serde_bytes::ByteBuf::new())).unwrap()
}
}