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
include!("./handle_types.inc.rs");
use crate::{FromInner, Inner, IntoInner};
use std::alloc::Layout;
use std::ffi::CStr;
use uv::{
uv_close, uv_fileno, uv_handle_get_data, uv_handle_get_loop, uv_handle_get_type,
uv_handle_set_data, uv_handle_t, uv_handle_type_name, uv_has_ref, uv_is_active, uv_is_closing,
uv_recv_buffer_size, uv_ref, uv_send_buffer_size, uv_unref,
};
impl HandleType {
pub fn name(&self) -> String {
unsafe {
CStr::from_ptr(uv_handle_type_name(self.into_inner()))
.to_string_lossy()
.into_owned()
}
}
}
impl std::fmt::Display for HandleType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.name())
}
}
impl IntoInner<Option<Layout>> for HandleType {
fn into_inner(self) -> Option<Layout> {
match self {
HandleType::ASYNC => Some(Layout::new::<uv::uv_async_t>()),
HandleType::CHECK => Some(Layout::new::<uv::uv_check_t>()),
HandleType::FS_EVENT => Some(Layout::new::<uv::uv_fs_event_t>()),
HandleType::FS_POLL => Some(Layout::new::<uv::uv_fs_poll_t>()),
HandleType::HANDLE => Some(Layout::new::<uv::uv_handle_t>()),
HandleType::IDLE => Some(Layout::new::<uv::uv_idle_t>()),
HandleType::NAMED_PIPE => Some(Layout::new::<uv::uv_pipe_t>()),
HandleType::POLL => Some(Layout::new::<uv::uv_poll_t>()),
HandleType::PREPARE => Some(Layout::new::<uv::uv_prepare_t>()),
HandleType::PROCESS => Some(Layout::new::<uv::uv_process_t>()),
HandleType::SIGNAL => Some(Layout::new::<uv::uv_signal_t>()),
HandleType::STREAM => Some(Layout::new::<uv::uv_stream_t>()),
HandleType::TCP => Some(Layout::new::<uv::uv_tcp_t>()),
HandleType::TIMER => Some(Layout::new::<uv::uv_timer_t>()),
HandleType::TTY => Some(Layout::new::<uv::uv_tty_t>()),
HandleType::UDP => Some(Layout::new::<uv::uv_udp_t>()),
_ => None,
}
}
}
callbacks! {
pub CloseCB(handle: crate::Handle);
}
pub(crate) struct HandleData<'a> {
pub(crate) close_cb: CloseCB<'a>,
pub(crate) addl: super::AddlHandleData<'a>,
}
pub(crate) extern "C" fn uv_close_cb(handle: *mut uv_handle_t) {
let dataptr = Handle::get_data(handle);
if !dataptr.is_null() {
unsafe {
(*dataptr).close_cb.call(handle.into_inner());
}
}
Handle::free_data(handle);
let handle_obj: Handle = handle.into_inner();
let layout: Option<Layout> = handle_obj.get_type().into_inner();
if let Some(layout) = layout {
unsafe { std::alloc::dealloc(handle as _, layout) };
}
}
#[derive(Clone, Copy)]
pub struct Handle {
handle: *mut uv_handle_t,
}
impl Handle {
pub(crate) fn initialize_data(handle: *mut uv_handle_t, addl: super::AddlHandleData) {
let data: Box<HandleData> = Box::new(HandleData {
close_cb: ().into(),
addl,
});
let ptr = Box::into_raw(data);
unsafe { uv_handle_set_data(handle, ptr as _) };
}
pub(crate) fn get_data<'a>(handle: *mut uv_handle_t) -> *mut HandleData<'a> {
unsafe { uv_handle_get_data(handle) as _ }
}
pub(crate) fn free_data(handle: *mut uv_handle_t) {
let ptr = Handle::get_data(handle);
std::mem::drop(unsafe { Box::from_raw(ptr) });
unsafe { uv_handle_set_data(handle, std::ptr::null_mut()) };
}
}
pub trait ToHandle {
fn to_handle(&self) -> Handle;
}
impl ToHandle for Handle {
fn to_handle(&self) -> Handle {
Handle {
handle: self.handle,
}
}
}
impl FromInner<*mut uv_handle_t> for Handle {
fn from_inner(handle: *mut uv_handle_t) -> Handle {
Handle { handle }
}
}
impl Inner<*mut uv_handle_t> for Handle {
fn inner(&self) -> *mut uv_handle_t {
self.handle
}
}
pub trait HandleTrait: ToHandle {
fn is_active(&self) -> bool {
unsafe { uv_is_active(self.to_handle().inner()) != 0 }
}
fn is_closing(&self) -> bool {
unsafe { uv_is_closing(self.to_handle().inner()) != 0 }
}
fn close<CB: Into<CloseCB<'static>>>(&mut self, cb: CB) {
let handle = self.to_handle().inner();
let cb = cb.into();
let dataptr = Handle::get_data(handle);
if !dataptr.is_null() {
unsafe { (*dataptr).close_cb = cb };
}
unsafe { uv_close(handle, Some(uv_close_cb)) };
}
fn r#ref(&mut self) {
unsafe { uv_ref(self.to_handle().inner()) };
}
fn unref(&mut self) {
unsafe { uv_unref(self.to_handle().inner()) };
}
fn has_ref(&self) -> bool {
unsafe { uv_has_ref(self.to_handle().inner()) != 0 }
}
fn send_buffer_size(&mut self, value: i32) -> crate::Result<i32> {
let mut v = value;
crate::uvret(unsafe { uv_send_buffer_size(self.to_handle().inner(), &mut v as _) })?;
Ok(v)
}
fn recv_buffer_size(&mut self, value: i32) -> crate::Result<i32> {
let mut v = value;
crate::uvret(unsafe { uv_recv_buffer_size(self.to_handle().inner(), &mut v as _) })?;
Ok(v)
}
fn get_fileno(&self) -> crate::Result<crate::OsFile> {
let mut v: uv::uv_os_fd_t = 0 as _;
crate::uvret(unsafe { uv_fileno(self.to_handle().inner(), &mut v as _) })?;
Ok(v as _)
}
fn get_loop(&self) -> crate::Loop {
unsafe { uv_handle_get_loop(self.to_handle().inner()).into_inner() }
}
fn get_type(&self) -> HandleType {
unsafe { uv_handle_get_type(self.to_handle().inner()).into_inner() }
}
}
impl HandleTrait for Handle {}