1use std::ops::{Deref, DerefMut};
2
3#[cfg(unix)]
4use libc::msghdr;
5#[cfg(windows)]
6use windows_sys::Win32::Networking::WinSock::WSAMSG;
7
8#[allow(clippy::unnecessary_safety_doc)]
13pub unsafe trait MsgBuf: Unpin + 'static {
14 #[cfg(unix)]
21 fn read_msghdr_ptr(&self) -> *const msghdr;
22
23 #[cfg(windows)]
25 fn read_wsamsg_ptr(&self) -> *const WSAMSG;
26}
27
28#[allow(clippy::unnecessary_safety_doc)]
33pub unsafe trait MsgBufMut: Unpin + 'static {
34 #[cfg(unix)]
41 fn write_msghdr_ptr(&mut self) -> *mut msghdr;
42
43 #[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}