monoio/buf/
msg.rs

1use std::ops::{Deref, DerefMut};
2
3#[cfg(unix)]
4use libc::msghdr;
5#[cfg(windows)]
6use windows_sys::Win32::Networking::WinSock::WSAMSG;
7
8/// An `io_uring` compatible msg buffer.
9///
10/// # Safety
11/// See the safety note of the methods.
12#[allow(clippy::unnecessary_safety_doc)]
13pub unsafe trait MsgBuf: Unpin + 'static {
14    /// Returns a raw pointer to msghdr struct.
15    ///
16    /// # Safety
17    /// The implementation must ensure that, while the runtime owns the value,
18    /// the pointer returned by `stable_mut_ptr` **does not** change.
19    /// Also, the value pointed must be a valid msghdr struct.
20    #[cfg(unix)]
21    fn read_msghdr_ptr(&self) -> *const msghdr;
22
23    /// Returns a raw pointer to WSAMSG struct.
24    #[cfg(windows)]
25    fn read_wsamsg_ptr(&self) -> *const WSAMSG;
26}
27
28/// An `io_uring` compatible msg buffer.
29///
30/// # Safety
31/// See the safety note of the methods.
32#[allow(clippy::unnecessary_safety_doc)]
33pub unsafe trait MsgBufMut: Unpin + 'static {
34    /// Returns a raw pointer to msghdr struct.
35    ///
36    /// # Safety
37    /// The implementation must ensure that, while the runtime owns the value,
38    /// the pointer returned by `stable_mut_ptr` **does not** change.
39    /// Also, the value pointed must be a valid msghdr struct.
40    #[cfg(unix)]
41    fn write_msghdr_ptr(&mut self) -> *mut msghdr;
42
43    /// Returns a raw pointer to WSAMSG struct.
44    #[cfg(windows)]
45    fn write_wsamsg_ptr(&mut self) -> *mut WSAMSG;
46}
47
48#[allow(missing_docs)]
49pub struct MsgMeta {
50    #[cfg(unix)]
51    pub(crate) data: msghdr,
52    #[cfg(windows)]
53    pub(crate) data: WSAMSG,
54}
55
56unsafe impl MsgBuf for MsgMeta {
57    #[cfg(unix)]
58    fn read_msghdr_ptr(&self) -> *const msghdr {
59        &self.data
60    }
61
62    #[cfg(windows)]
63    fn read_wsamsg_ptr(&self) -> *const WSAMSG {
64        &self.data
65    }
66}
67
68unsafe impl MsgBufMut for MsgMeta {
69    #[cfg(unix)]
70    fn write_msghdr_ptr(&mut self) -> *mut msghdr {
71        &mut self.data
72    }
73
74    #[cfg(windows)]
75    fn write_wsamsg_ptr(&mut self) -> *mut WSAMSG {
76        &mut self.data
77    }
78}
79
80#[cfg(unix)]
81impl From<msghdr> for MsgMeta {
82    fn from(data: msghdr) -> Self {
83        Self { data }
84    }
85}
86
87#[cfg(unix)]
88impl Deref for MsgMeta {
89    type Target = msghdr;
90
91    fn deref(&self) -> &Self::Target {
92        &self.data
93    }
94}
95
96#[cfg(unix)]
97impl DerefMut for MsgMeta {
98    fn deref_mut(&mut self) -> &mut Self::Target {
99        &mut self.data
100    }
101}
102
103#[cfg(windows)]
104impl From<WSAMSG> for MsgMeta {
105    fn from(data: WSAMSG) -> Self {
106        Self { data }
107    }
108}
109
110#[cfg(windows)]
111impl Deref for MsgMeta {
112    type Target = WSAMSG;
113
114    fn deref(&self) -> &Self::Target {
115        &self.data
116    }
117}
118
119#[cfg(windows)]
120impl DerefMut for MsgMeta {
121    fn deref_mut(&mut self) -> &mut Self::Target {
122        &mut self.data
123    }
124}