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
use ::{NifDecoder, NifEnv, NifResult};
use ::wrapper::nif_interface::NIF_TERM;
use ::wrapper::env::term_to_binary;
use ::types::binary::NifBinary;
use std::fmt::{self, Debug};
#[derive(Clone, Copy)]
pub struct NifTerm<'a> {
term: NIF_TERM,
env: NifEnv<'a>,
}
impl<'a> Debug for NifTerm<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
::wrapper::term::fmt(self.as_c_arg(), f)
}
}
impl<'a> NifTerm<'a> {
pub unsafe fn new(env: NifEnv<'a>, inner: NIF_TERM) -> Self {
NifTerm {
term: inner,
env: env,
}
}
pub fn as_c_arg(&self) -> NIF_TERM {
self.term
}
pub fn get_env(&self) -> NifEnv<'a> {
self.env
}
pub fn in_env<'b>(&self, env: NifEnv<'b>) -> NifTerm<'b> {
if self.get_env() == env {
unsafe { NifTerm::new(env, self.as_c_arg()) }
} else {
unsafe { NifTerm::new(env, ::wrapper::copy_term(env.as_c_arg(), self.as_c_arg())) }
}
}
pub fn decode<T>(self) -> NifResult<T> where T: NifDecoder<'a> {
NifDecoder::decode(self)
}
pub fn to_binary(self) -> NifBinary<'a> {
let raw_binary = unsafe { term_to_binary(self.env.as_c_arg(), self.as_c_arg()) }.unwrap();
unsafe { NifBinary::from_raw(self.env, raw_binary) }
}
}