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
use crate::{FromInner, HandleTrait, Inner, IntoInner};
use std::convert::TryFrom;
use uv::{uv_async_init, uv_async_send, uv_async_t};
callbacks! {
pub AsyncCB(handle: AsyncHandle);
}
#[derive(Default)]
pub(crate) struct AsyncDataFields<'a> {
async_cb: AsyncCB<'a>,
}
extern "C" fn uv_async_cb(handle: *mut uv_async_t) {
let dataptr = crate::Handle::get_data(uv_handle!(handle));
if !dataptr.is_null() {
unsafe {
if let super::AsyncData(d) = &mut (*dataptr).addl {
d.async_cb.call(handle.into_inner());
}
}
}
}
#[derive(Clone, Copy)]
pub struct AsyncHandle {
handle: *mut uv_async_t,
}
unsafe impl Send for AsyncHandle {}
unsafe impl Sync for AsyncHandle {}
impl AsyncHandle {
pub fn new<CB: Into<AsyncCB<'static>>>(
r#loop: &crate::Loop,
cb: CB,
) -> crate::Result<AsyncHandle> {
let layout = std::alloc::Layout::new::<uv_async_t>();
let handle = unsafe { std::alloc::alloc(layout) as *mut uv_async_t };
if handle.is_null() {
return Err(crate::Error::ENOMEM);
}
let async_cb = cb.into();
let uv_cb = use_c_callback!(uv_async_cb, async_cb);
let data = AsyncDataFields { async_cb };
crate::Handle::initialize_data(uv_handle!(handle), super::AsyncData(data));
let ret = unsafe { uv_async_init(r#loop.into_inner(), handle, uv_cb) };
if ret < 0 {
crate::Handle::free_data(uv_handle!(handle));
unsafe { std::alloc::dealloc(handle as _, layout) };
return Err(crate::Error::from_inner(ret as uv::uv_errno_t));
}
Ok(AsyncHandle { handle })
}
pub fn send(&mut self) -> crate::Result<()> {
crate::uvret(unsafe { uv_async_send(self.handle) })
}
}
impl FromInner<*mut uv_async_t> for AsyncHandle {
fn from_inner(handle: *mut uv_async_t) -> AsyncHandle {
AsyncHandle { handle }
}
}
impl Inner<*mut uv::uv_handle_t> for AsyncHandle {
fn inner(&self) -> *mut uv::uv_handle_t {
uv_handle!(self.handle)
}
}
impl From<AsyncHandle> for crate::Handle {
fn from(r#async: AsyncHandle) -> crate::Handle {
crate::Handle::from_inner(Inner::<*mut uv::uv_handle_t>::inner(&r#async))
}
}
impl crate::ToHandle for AsyncHandle {
fn to_handle(&self) -> crate::Handle {
crate::Handle::from_inner(Inner::<*mut uv::uv_handle_t>::inner(self))
}
}
impl TryFrom<crate::Handle> for AsyncHandle {
type Error = crate::ConversionError;
fn try_from(handle: crate::Handle) -> Result<Self, Self::Error> {
let t = handle.get_type();
if t != crate::HandleType::ASYNC {
Err(crate::ConversionError::new(t, crate::HandleType::ASYNC))
} else {
Ok((handle.inner() as *mut uv_async_t).into_inner())
}
}
}
impl HandleTrait for AsyncHandle {}
impl crate::Loop {
pub fn r#async<CB: Into<AsyncCB<'static>>>(&self, cb: CB) -> crate::Result<AsyncHandle> {
AsyncHandle::new(self, cb)
}
}