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
include!("./req_types.inc.rs");
use crate::{FromInner, Inner, IntoInner};
use std::ffi::CStr;
use uv::{
uv_cancel, uv_req_get_data, uv_req_get_type, uv_req_set_data, uv_req_t, uv_req_type_name,
};
impl ReqType {
pub fn name(&self) -> String {
unsafe {
CStr::from_ptr(uv_req_type_name(self.into_inner()))
.to_string_lossy()
.into_owned()
}
}
}
impl std::fmt::Display for ReqType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.name())
}
}
#[derive(Clone, Copy)]
pub struct Req {
req: *mut uv_req_t,
}
impl Req {
pub(crate) fn initialize_data(req: *mut uv_req_t, data: super::ReqData) {
let ptr = Box::into_raw(Box::new(data));
unsafe { uv_req_set_data(req, ptr as _) }
}
pub(crate) fn get_data<'a>(req: *mut uv_req_t) -> *mut super::ReqData<'a> {
unsafe { uv_req_get_data(req) as _ }
}
pub(crate) fn free_data(req: *mut uv_req_t) {
let ptr = Req::get_data(req);
std::mem::drop(unsafe { Box::from_raw(ptr) });
unsafe { uv_req_set_data(req, std::ptr::null_mut()) };
}
}
pub trait ToReq {
fn to_req(&self) -> Req;
}
impl FromInner<*mut uv_req_t> for Req {
fn from_inner(req: *mut uv_req_t) -> Req {
Req { req }
}
}
impl Inner<*mut uv_req_t> for Req {
fn inner(&self) -> *mut uv_req_t {
self.req
}
}
impl ToReq for Req {
fn to_req(&self) -> Req {
Req { req: self.req }
}
}
pub trait ReqTrait: ToReq {
fn cancel(&mut self) -> crate::Result<()> {
crate::uvret(unsafe { uv_cancel(self.to_req().inner()) })
}
fn get_type(&self) -> ReqType {
unsafe { uv_req_get_type(self.to_req().inner()).into_inner() }
}
}
impl ReqTrait for Req {}