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
use libc::size_t;
use std::ffi;
use std::fmt;
use std::ops::{Deref, DerefMut};
use std::os::raw::c_void;
use std::{ptr, slice, str};
use super::errno_to_error;
pub struct Message {
msg: zmq_sys::zmq_msg_t,
}
impl Drop for Message {
fn drop(&mut self) {
unsafe {
let rc = zmq_sys::zmq_msg_close(&mut self.msg);
assert_eq!(rc, 0);
}
}
}
impl fmt::Debug for Message {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.deref())
}
}
unsafe extern "C" fn drop_msg_content_box(data: *mut c_void, _hint: *mut c_void) {
let _ = Box::from_raw(data as *mut u8);
}
impl Message {
unsafe fn alloc<F>(f: F) -> Message
where
F: FnOnce(&mut zmq_sys::zmq_msg_t) -> i32,
{
let mut msg = zmq_sys::zmq_msg_t::default();
let rc = f(&mut msg);
if rc == -1 {
panic!(errno_to_error())
}
Message { msg }
}
pub fn new() -> Message {
unsafe { Self::alloc(|msg| zmq_sys::zmq_msg_init(msg)) }
}
#[deprecated(
since = "0.9.1",
note = "This method has an unintuitive name, and should not be needed."
)]
pub unsafe fn with_capacity_unallocated(len: usize) -> Message {
Self::alloc(|msg| zmq_sys::zmq_msg_init_size(msg, len as size_t))
}
unsafe fn with_size_uninit(len: usize) -> Message {
Self::alloc(|msg| zmq_sys::zmq_msg_init_size(msg, len as size_t))
}
pub fn with_size(len: usize) -> Message {
unsafe {
let mut msg = Message::with_size_uninit(len);
ptr::write_bytes(msg.as_mut_ptr(), 0, len);
msg
}
}
#[deprecated(
since = "0.9.1",
note = "This method has a name which does not match its semantics. Use `with_size` instead"
)]
pub fn with_capacity(len: usize) -> Message {
Self::with_size(len)
}
#[deprecated(since = "0.9.1", note = "Use the `From` trait instead.")]
pub fn from_slice(data: &[u8]) -> Message {
Self::from(data)
}
pub fn as_str(&self) -> Option<&str> {
str::from_utf8(self).ok()
}
pub fn get_more(&self) -> bool {
let rc = unsafe { zmq_sys::zmq_msg_more(&self.msg) };
rc != 0
}
pub fn gets<'a>(&'a mut self, property: &str) -> Option<&'a str> {
let c_str = ffi::CString::new(property.as_bytes()).unwrap();
let value = unsafe { zmq_sys::zmq_msg_gets(&self.msg, c_str.as_ptr()) };
if value.is_null() {
None
} else {
str::from_utf8(unsafe { ffi::CStr::from_ptr(value) }.to_bytes()).ok()
}
}
}
impl Deref for Message {
type Target = [u8];
fn deref(&self) -> &[u8] {
unsafe {
let ptr = &self.msg as *const _ as *mut _;
let data = zmq_sys::zmq_msg_data(ptr);
let len = zmq_sys::zmq_msg_size(ptr) as usize;
slice::from_raw_parts(data as *mut u8, len)
}
}
}
impl PartialEq for Message {
fn eq(&self, other: &Message) -> bool {
self[..] == other[..]
}
}
impl Eq for Message {}
impl DerefMut for Message {
fn deref_mut(&mut self) -> &mut [u8] {
unsafe {
let data = zmq_sys::zmq_msg_data(&mut self.msg);
let len = zmq_sys::zmq_msg_size(&self.msg) as usize;
slice::from_raw_parts_mut(data as *mut u8, len)
}
}
}
impl<'a> From<&'a [u8]> for Message {
fn from(data: &'a [u8]) -> Self {
unsafe {
let mut msg = Message::with_size_uninit(data.len());
ptr::copy_nonoverlapping(data.as_ptr(), msg.as_mut_ptr(), data.len());
msg
}
}
}
impl From<Vec<u8>> for Message {
fn from(msg: Vec<u8>) -> Self {
Message::from(msg.into_boxed_slice())
}
}
impl From<Box<[u8]>> for Message {
fn from(data: Box<[u8]>) -> Self {
let n = data.len();
if n == 0 {
return Message::new();
}
let raw = Box::into_raw(data);
unsafe {
Self::alloc(|msg| {
zmq_sys::zmq_msg_init_data(
msg,
raw as *mut c_void,
n,
Some(drop_msg_content_box),
ptr::null_mut(),
)
})
}
}
}
impl<'a> From<&'a str> for Message {
fn from(msg: &str) -> Self {
Message::from(msg.as_bytes())
}
}
impl<'a> From<&'a String> for Message {
fn from(msg: &String) -> Self {
Message::from(msg.as_bytes())
}
}
impl<'a, T> From<&'a T> for Message
where
T: Into<Message> + Clone,
{
fn from(v: &'a T) -> Self {
v.clone().into()
}
}
pub fn msg_ptr(msg: &mut Message) -> *mut zmq_sys::zmq_msg_t {
&mut msg.msg
}