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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use std::{ops, ptr, time};
use std::default::Default;
use std::os::raw;
use std::sync::Arc;
use super::{sys, Logger};
#[derive(Debug, Hash)]
pub struct Context<'lg> {
inner: *mut sys::xmpp_ctx_t,
owned: bool,
_logger: Option<Logger<'lg>>,
}
impl<'lg> Context<'lg> {
pub fn new(logger: Logger<'lg>) -> Context<'lg> {
super::init();
unsafe {
Context::with_inner(
sys::xmpp_ctx_new(ptr::null(), logger.as_inner()),
true,
Some(logger)
)
}
}
pub fn new_with_default_logger() -> Arc<Context<'static>> {
Arc::new(Context::new(Logger::default()))
}
#[inline]
unsafe fn with_inner(inner: *mut sys::xmpp_ctx_t, owned: bool, logger: Option<Logger<'lg>>) -> Context<'lg> {
if inner.is_null() {
panic!("Cannot allocate memory for Context")
}
Context { inner, owned, _logger: logger }
}
pub unsafe fn from_inner_ref(inner: *const sys::xmpp_ctx_t) -> Context<'lg> {
Context::from_inner_ref_mut(inner as *mut _)
}
pub unsafe fn from_inner_ref_mut(inner: *mut sys::xmpp_ctx_t) -> Context<'lg> {
Context::with_inner(inner, false, None)
}
pub fn as_inner(&self) -> *const sys::xmpp_ctx_t { self.inner }
pub fn run_once(&self, timeout: time::Duration) {
unsafe {
sys::xmpp_run_once(self.inner, super::duration_as_ms(timeout))
}
}
pub fn run(&self) {
unsafe {
sys::xmpp_run(self.inner)
}
}
pub fn stop(&self) {
unsafe {
sys::xmpp_stop(self.inner)
}
}
pub unsafe fn free<T>(&self, p: *mut T) {
sys::xmpp_free(self.inner, p as *mut raw::c_void)
}
}
impl<'lg> PartialEq for Context<'lg> {
fn eq(&self, other: &Context) -> bool {
self.inner == other.inner
}
}
impl<'lg> Eq for Context<'lg> {}
impl<'lg> Drop for Context<'lg> {
fn drop(&mut self) {
unsafe {
if self.owned {
sys::xmpp_ctx_free(self.inner);
}
}
}
}
unsafe impl<'lg> Send for Context<'lg> {}
#[derive(Debug, Hash, PartialEq)]
pub struct ContextRef<'lg>(Arc<Context<'lg>>);
impl<'lg> ops::Deref for ContextRef<'lg> {
type Target = Context<'lg>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'lg> Into<ContextRef<'lg>> for Arc<Context<'lg>> {
fn into(self) -> ContextRef<'lg> {
ContextRef(self)
}
}