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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use crate::*;
use log::trace;
use runng_sys::*;
use std::{convert::TryFrom, convert::TryInto, ffi::CStr, marker, result};
pub trait NngStat {
unsafe fn nng_stat(&self) -> *mut nng_stat;
fn child(&self) -> Option<NngStatChild> {
unsafe {
let node = nng_stat_child(self.nng_stat());
NngStatChild::new(node)
}
}
}
#[derive(Debug)]
pub struct NngStatRoot {
node: *mut nng_stat,
}
impl NngStatRoot {
pub fn new() -> Result<NngStatRoot> {
unsafe {
let mut node: *mut nng_stat = std::ptr::null_mut();
let res = nng_stats_get(&mut node);
Error::zero_map(res, || NngStatRoot { node })
}
}
}
impl NngStat for NngStatRoot {
unsafe fn nng_stat(&self) -> *mut nng_stat {
self.node
}
}
impl Drop for NngStatRoot {
fn drop(&mut self) {
unsafe {
trace!("Drop NngStatRoot");
nng_stats_free(self.node)
}
}
}
#[derive(Debug)]
pub struct NngStatChild<'root> {
node: *mut nng_stat,
_phantom: marker::PhantomData<&'root nng_stat>,
}
impl<'root> NngStatChild<'root> {
pub fn new(node: *mut nng_stat) -> Option<NngStatChild<'root>> {
if node.is_null() {
None
} else {
Some(NngStatChild {
node,
_phantom: marker::PhantomData,
})
}
}
pub fn name(&self) -> result::Result<&str, std::str::Utf8Error> {
unsafe {
let ptr = nng_stat_name(self.nng_stat());
CStr::from_ptr(ptr).to_str()
}
}
pub fn desc(&self) -> result::Result<&str, std::str::Utf8Error> {
unsafe {
let ptr = nng_stat_desc(self.nng_stat());
CStr::from_ptr(ptr).to_str()
}
}
pub fn stat_type(&self) -> result::Result<nng_stat_type_enum, EnumFromIntError> {
unsafe {
let val = nng_stat_type(self.nng_stat());
val.try_into()
}
}
pub fn value(&self) -> u64 {
unsafe { nng_stat_value(self.nng_stat()) }
}
pub fn string(&self) -> Option<&str> {
unsafe {
let ptr = nng_stat_string(self.nng_stat());
if ptr.is_null() {
return None;
}
CStr::from_ptr(ptr).to_str().ok()
}
}
pub fn unit(&self) -> result::Result<nng_unit_enum, EnumFromIntError> {
unsafe {
let val = nng_stat_unit(self.nng_stat());
nng_unit_enum::try_from(val)
}
}
pub fn timestamp(&self) -> u64 {
unsafe { nng_stat_timestamp(self.nng_stat()) }
}
pub fn iter(&self) -> Iter {
unsafe {
let node = self.nng_stat();
Iter {
node: NngStatChild::new(node),
}
}
}
pub fn next(&self) -> Option<NngStatChild<'root>> {
unsafe {
let node = self.nng_stat();
let node = nng_stat_next(node);
NngStatChild::new(node)
}
}
}
impl<'root> NngStat for NngStatChild<'_> {
unsafe fn nng_stat(&self) -> *mut nng_stat {
self.node
}
}
#[derive(Debug)]
pub struct Iter<'root> {
node: Option<NngStatChild<'root>>,
}
impl<'root> Iterator for Iter<'root> {
type Item = NngStatChild<'root>;
fn next(&mut self) -> Option<Self::Item> {
let next = self.node.take();
if let Some(ref node) = next {
self.node = node.next();
}
next
}
}