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
use std::mem;
use wrapper::nif_interface::{self, ErlNifPid};
use wrapper::pid;
use {Decoder, Encoder, Env, Error, NifResult, Term};
#[derive(Clone)]
pub struct Pid {
c: ErlNifPid,
}
impl Pid {
pub fn as_c_arg(&self) -> &ErlNifPid {
&self.c
}
}
impl<'a> Decoder<'a> for Pid {
fn decode(term: Term<'a>) -> NifResult<Pid> {
unsafe { pid::get_local_pid(term.get_env().as_c_arg(), term.as_c_arg()) }
.map(|pid| Pid { c: pid })
.ok_or(Error::BadArg)
}
}
impl Encoder for Pid {
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
unsafe { Term::new(env, pid::make_pid(env.as_c_arg(), &self.c)) }
}
}
impl<'a> Env<'a> {
pub fn pid(self) -> Pid {
let mut pid: ErlNifPid = unsafe { mem::uninitialized() };
if unsafe { nif_interface::enif_self(self.as_c_arg(), &mut pid) }.is_null() {
panic!("environment is process-independent");
}
Pid { c: pid }
}
}