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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use core::{ops, ptr, slice, mem, fmt};
use crate::error::{ErrorCode, error};
use nng_c_sys::nng_msg;
use nng_c_sys::{nng_msg_alloc, nng_msg_free, nng_msg_capacity, nng_msg_reserve};
use nng_c_sys::{nng_msg_clear, nng_msg_dup};
use nng_c_sys::{nng_msg_body, nng_msg_len};
use nng_c_sys::{nng_msg_trim, nng_msg_chop};
use nng_c_sys::{nng_msg_chop_u16, nng_msg_chop_u32, nng_msg_chop_u64};
use nng_c_sys::{nng_msg_trim_u16, nng_msg_trim_u32, nng_msg_trim_u64};
use nng_c_sys::{nng_msg_append, nng_msg_append_u16, nng_msg_append_u32, nng_msg_append_u64};
use nng_c_sys::{nng_msg_insert, nng_msg_insert_u16, nng_msg_insert_u32, nng_msg_insert_u64};
use nng_c_sys::{nng_msg_header, nng_msg_header_len};
///Message primitive
pub struct Message(pub(crate) ptr::NonNull<nng_msg>);
impl Message {
#[inline(always)]
///Creates empty message
///
///Returns `None` if unable to allocate memory
pub fn new() -> Option<Self> {
Self::with_capaicty(0)
}
#[inline]
///Creates message with pre-allocated buffer of provided `size`
///
///Returns `None` if unable to allocate memory
pub fn with_capaicty(size: usize) -> Option<Self> {
let mut ptr = ptr::null_mut();
unsafe {
nng_msg_alloc(&mut ptr, size);
}
ptr::NonNull::new(ptr).map(Self)
}
#[inline]
///Creates new copy of the message.
///
///Returns `None` if unable to allocate memory
pub fn dup(&self) -> Option<Self> {
let mut ptr = ptr::null_mut();
unsafe {
nng_msg_dup(&mut ptr, self.0.as_ptr());
}
ptr::NonNull::new(ptr).map(Self)
}
#[inline(always)]
///Reserves additional space to accommodate specified `capacity`
///
///Does nothing, if message already has enough space.
///
///Returns error only if reallocation failed
pub fn reserve(&mut self, capacity: usize) -> Result<(), ErrorCode> {
let result = unsafe {
nng_msg_reserve(self.0.as_ptr(), capacity)
};
match result {
0 => Ok(()),
code => Err(error(code))
}
}
#[inline(always)]
///Returns length of message body
pub fn len(&self) -> usize {
unsafe {
nng_msg_len(self.0.as_ptr())
}
}
#[inline(always)]
///Returns capacity of message body
pub fn capaciy(&self) -> usize {
unsafe {
nng_msg_capacity(self.0.as_ptr())
}
}
#[inline(always)]
///Clears content of the message.
///
///Note that it is primarily sets length of body to 0.
///Allocated capacity remains the same.
pub fn clear(&mut self) {
unsafe {
nng_msg_clear(self.0.as_ptr())
}
}
#[inline(always)]
///Shortens body length, keeping `len` starting elements
///
///Has no effect if `len` is equal or greater to current body's length
pub fn truncate(&mut self, len: usize) {
let size = self.len().saturating_sub(len);
unsafe {
nng_msg_chop(self.0.as_ptr(), size);
}
}
#[inline(always)]
///Shortens body length, keeping `len` last elements inside
///
///Has no effect if `len` is equal or greater to current body's length
pub fn truncate_start(&mut self, len: usize) {
let size = self.len().saturating_sub(len);
unsafe {
nng_msg_trim(self.0.as_ptr(), size);
}
}
#[inline(always)]
///Returns reference to the body content
pub fn body(&self) -> &[u8] {
let ptr = self.0.as_ptr();
unsafe {
let body = nng_msg_body(ptr);
let len = nng_msg_len(ptr);
slice::from_raw_parts(body as *const u8, len)
}
}
#[inline(always)]
///Returns reference to the header content
pub fn header(&self) -> &[u8] {
let ptr = self.0.as_ptr();
unsafe {
let body = nng_msg_header(ptr);
let len = nng_msg_header_len(ptr);
slice::from_raw_parts(body as *const u8, len)
}
}
fn push_inner<T: Copy>(&mut self, value: T, insertor: unsafe extern "C" fn(*mut nng_msg, T) -> core::ffi::c_int) -> Result<(), ErrorCode> {
let result = unsafe {
(insertor)(self.0.as_ptr(), value)
};
match result {
0 => Ok(()),
code => Err(error(code)),
}
}
fn pop_inner<T: Copy>(&mut self, extractor: unsafe extern "C" fn(*mut nng_msg, *mut T) -> core::ffi::c_int) -> Option<T> {
let mut out = mem::MaybeUninit::uninit();
let result = unsafe {
(extractor)(self.0.as_ptr(), out.as_mut_ptr())
};
match result {
0 => Some(unsafe {
out.assume_init()
}),
_ => None
}
}
//pop
#[inline(always)]
///Extracts u16 from the end of body, encoding it into native byte order
///
///Returns `None` if there is not enough space
pub fn pop_u16(&mut self) -> Option<u16> {
self.pop_inner(nng_msg_chop_u16)
}
#[inline(always)]
///Extracts u32 from the end of body, encoding it into native byte order
///
///Returns `None` if there is not enough space
pub fn pop_u32(&mut self) -> Option<u32> {
self.pop_inner(nng_msg_chop_u32)
}
#[inline(always)]
///Extracts u64 from the end of body, encoding it into native byte order
///
///Returns `None` if there is not enough space
pub fn pop_u64(&mut self) -> Option<u64> {
self.pop_inner(nng_msg_chop_u64)
}
#[inline(always)]
///Extracts u16 from the start of body, encoding it into native byte order
///
///Returns `None` if there is not enough space
pub fn pop_front_u16(&mut self) -> Option<u16> {
self.pop_inner(nng_msg_trim_u16)
}
#[inline(always)]
///Extracts u32 from the start of body, encoding it into native byte order
///
///Returns `None` if there is not enough space
pub fn pop_front_u32(&mut self) -> Option<u32> {
self.pop_inner(nng_msg_trim_u32)
}
#[inline(always)]
///Extracts u64 from the start of body, encoding it into native byte order
///
///Returns `None` if there is not enough space
pub fn pop_front_u64(&mut self) -> Option<u64> {
self.pop_inner(nng_msg_trim_u64)
}
//push
#[inline(always)]
///Appends u16 to the end of body, encoding it into network byte order
///
///Returns `Err` if there is not enough space
pub fn append_u16(&mut self, value: u16) -> Result<(), ErrorCode> {
self.push_inner(value, nng_msg_append_u16)
}
#[inline(always)]
///Appends u32 to the end of body, encoding it into network byte order
///
///Returns `Err` if there is not enough space
pub fn append_u32(&mut self, value: u32) -> Result<(), ErrorCode> {
self.push_inner(value, nng_msg_append_u32)
}
#[inline(always)]
///Appends u64 to the end of body, encoding it into network byte order
///
///Returns `Err` if there is not enough space
pub fn append_u64(&mut self, value: u64) -> Result<(), ErrorCode> {
self.push_inner(value, nng_msg_append_u64)
}
///Appends `bytes` to the message body.
pub fn append(&mut self, bytes: &[u8]) -> Result<(), ErrorCode> {
let result = unsafe {
nng_msg_append(self.0.as_ptr(), bytes.as_ptr() as _, bytes.len())
};
match result {
0 => Ok(()),
code => Err(error(code)),
}
}
#[inline(always)]
///Inserts u16 at the start of body, encoding it into network byte order
///
///Returns `Err` if there is not enough space
pub fn insert_u16(&mut self, value: u16) -> Result<(), ErrorCode> {
self.push_inner(value, nng_msg_insert_u16)
}
#[inline(always)]
///Inserts u32 at the start of body, encoding it into network byte order
///
///Returns `Err` if there is not enough space
pub fn insert_u32(&mut self, value: u32) -> Result<(), ErrorCode> {
self.push_inner(value, nng_msg_insert_u32)
}
#[inline(always)]
///Inserts u64 at the start of body, encoding it into network byte order
///
///Returns `Err` if there is not enough space
pub fn insert_u64(&mut self, value: u64) -> Result<(), ErrorCode> {
self.push_inner(value, nng_msg_insert_u64)
}
///Inserts `bytes` at the start of the body.
pub fn insert(&mut self, bytes: &[u8]) -> Result<(), ErrorCode> {
let result = unsafe {
nng_msg_insert(self.0.as_ptr(), bytes.as_ptr() as _, bytes.len())
};
match result {
0 => Ok(()),
code => Err(error(code)),
}
}
}
impl Clone for Message {
#[inline]
fn clone(&self) -> Self {
self.dup().unwrap()
}
}
impl ops::Deref for Message {
type Target = ptr::NonNull<nng_msg>;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl ops::DerefMut for Message {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl fmt::Debug for Message {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("Message").field("len", &self.len()).finish()
}
}
impl Drop for Message {
#[inline(always)]
fn drop(&mut self) {
unsafe {
nng_msg_free(self.0.as_ptr())
}
}
}