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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
use crate::{error::msg_from_errno, GroupSlice};
use libzmq_sys as sys;
use sys::errno;
use libc::size_t;
use log::error;
use serde::{Deserialize, Serialize};
use std::{
ffi::CStr,
fmt,
os::raw::c_void,
ptr, slice,
str::{self, Utf8Error},
};
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct RoutingId(u32);
pub struct Msg {
msg: sys::zmq_msg_t,
}
impl Msg {
pub fn new() -> Self {
Self::default()
}
pub fn with_size(size: usize) -> Self {
unsafe {
Self::deferred_alloc(|msg| {
sys::zmq_msg_init_size(msg, size as size_t)
})
}
}
pub fn len(&self) -> usize {
unsafe { sys::zmq_msg_size(self.as_ptr()) }
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn to_str(&self) -> Result<&str, Utf8Error> {
str::from_utf8(self.as_bytes())
}
pub fn as_bytes(&self) -> &[u8] {
unsafe {
let ptr = &self.msg as *const _ as *mut _;
let data = sys::zmq_msg_data(ptr);
slice::from_raw_parts(data as *mut u8, self.len())
}
}
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
unsafe {
let data = sys::zmq_msg_data(self.as_mut_ptr());
slice::from_raw_parts_mut(data as *mut u8, self.len())
}
}
pub fn routing_id(&self) -> Option<RoutingId> {
let rc = unsafe {
let ptr = self.as_ptr() as *mut _;
sys::zmq_msg_routing_id(ptr)
};
if rc == 0 {
None
} else {
Some(RoutingId(rc))
}
}
pub fn set_routing_id(&mut self, routing_id: RoutingId) {
let rc = unsafe {
sys::zmq_msg_set_routing_id(self.as_mut_ptr(), routing_id.0)
};
if rc != 0 {
let errno = unsafe { sys::zmq_errno() };
panic!(msg_from_errno(errno));
}
}
pub fn group(&self) -> Option<&GroupSlice> {
let mut_msg_ptr = self.as_ptr() as *mut _;
let char_ptr = unsafe { sys::zmq_msg_group(mut_msg_ptr) };
if char_ptr.is_null() {
None
} else {
let c_str = unsafe { CStr::from_ptr(char_ptr) };
Some(GroupSlice::from_c_str_unchecked(c_str))
}
}
pub fn set_group<G>(&mut self, group: G)
where
G: AsRef<GroupSlice>,
{
let group = group.as_ref();
let rc = unsafe {
sys::zmq_msg_set_group(self.as_mut_ptr(), group.as_c_str().as_ptr())
};
if rc == -1 {
let errno = unsafe { sys::zmq_errno() };
panic!(msg_from_errno(errno));
}
}
unsafe fn deferred_alloc<F>(f: F) -> Msg
where
F: FnOnce(&mut sys::zmq_msg_t) -> i32,
{
let mut msg = sys::zmq_msg_t::default();
let rc = f(&mut msg);
if rc == -1 {
panic!(msg_from_errno(sys::zmq_errno()));
}
Msg { msg }
}
pub(crate) fn as_mut_ptr(&mut self) -> *mut sys::zmq_msg_t {
&mut self.msg
}
pub(crate) fn as_ptr(&self) -> *const sys::zmq_msg_t {
&self.msg
}
pub(crate) fn has_more(&self) -> bool {
let rc = unsafe { sys::zmq_msg_more(self.as_ptr()) };
rc != 0
}
}
impl PartialEq for Msg {
fn eq(&self, other: &Self) -> bool {
ptr::eq(self.as_ptr(), other.as_ptr())
}
}
impl Eq for Msg {}
impl fmt::Debug for Msg {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.as_bytes())
}
}
impl Default for Msg {
fn default() -> Self {
unsafe { Self::deferred_alloc(|msg| sys::zmq_msg_init(msg)) }
}
}
impl Clone for Msg {
fn clone(&self) -> Self {
let mut msg = Msg::new();
let rc = unsafe {
let ptr = self.as_ptr() as *mut _;
sys::zmq_msg_copy(msg.as_mut_ptr(), ptr)
};
if rc != 0 {
let errno = unsafe { sys::zmq_errno() };
match errno {
errno::EFAULT => panic!("invalid message"),
_ => panic!(msg_from_errno(errno)),
}
}
msg
}
}
impl Drop for Msg {
fn drop(&mut self) {
let rc = unsafe { sys::zmq_msg_close(self.as_mut_ptr()) };
if rc != 0 {
let errno = unsafe { sys::zmq_errno() };
error!("error while dropping message: {}", msg_from_errno(errno));
}
}
}
impl From<Box<[u8]>> for Msg {
fn from(data: Box<[u8]>) -> Msg {
unsafe extern "C" fn drop_zmq_msg_t(
data: *mut c_void,
_hint: *mut c_void,
) {
Box::from_raw(data as *mut u8);
}
if data.is_empty() {
return Msg::new();
}
let size = data.len() as size_t;
let data = Box::into_raw(data);
unsafe {
Self::deferred_alloc(|msg| {
sys::zmq_msg_init_data(
msg,
data as *mut c_void,
size,
Some(drop_zmq_msg_t),
ptr::null_mut(),
)
})
}
}
}
impl<'a> From<&[u8]> for Msg {
fn from(slice: &[u8]) -> Self {
unsafe {
let mut msg = Msg::with_size(slice.len());
ptr::copy_nonoverlapping(
slice.as_ptr(),
msg.as_bytes_mut().as_mut_ptr(),
slice.len(),
);
msg
}
}
}
macro_rules! array_impls {
($($N:expr)+) => {
$(
impl From<[u8; $N]> for Msg {
fn from(array: [u8; $N]) -> Self {
let boxed: Box<[u8]> = Box::new(array);
Msg::from(boxed)
}
}
)+
}
}
array_impls! {
0 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
}
impl From<Vec<u8>> for Msg {
fn from(bytes: Vec<u8>) -> Self {
Msg::from(bytes.into_boxed_slice())
}
}
impl<'a> From<&'a str> for Msg {
fn from(text: &str) -> Self {
Msg::from(text.as_bytes())
}
}
impl From<String> for Msg {
fn from(text: String) -> Self {
Msg::from(text.into_bytes())
}
}
impl<'a, T> From<&'a T> for Msg
where
T: Into<Msg> + Clone,
{
fn from(v: &'a T) -> Self {
v.clone().into()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::mem;
#[test]
fn test_cast_routing_id_slice() {
assert_eq!(mem::size_of::<u32>(), mem::size_of::<RoutingId>());
let routing_stack: &[u32] = &[1, 2, 3, 4];
let cast_stack = unsafe {
slice::from_raw_parts(
routing_stack.as_ptr() as *const RoutingId,
routing_stack.len(),
)
};
for (&i, &j) in routing_stack.iter().zip(cast_stack.iter()) {
assert_eq!(i, j.0);
}
}
}