Add Socket::{send,recv}msg and MsgHdr(Mut)#447
Add Socket::{send,recv}msg and MsgHdr(Mut)#447Thomasdezeeuw merged 10 commits intorust-lang:masterfrom
Conversation
This wraps msghdr on Unix and WSAMSG on Windows to be used in send/recvmsg calls.
Wrapper around sendmsg(2).
|
I think this is unsound when using the non-mutable version in combination with I though about solving this with a generic type in |
Maybe something like this? In this case, we would be leak some types to the users, such as Or we just copy and paste to create struct pub trait MsgHdrMutableTrait {
fn mutable(&self);
}
pub struct Mutable {}
impl MsgHdrMutableTrait for Mutable {
fn mutable(&self) {}
}
pub struct MsgHdr<'addr, 'bufs, 'control, T>
{
inner: sys::msghdr,
#[allow(clippy::type_complexity)]
_lifetimes: PhantomData<(&'addr SockAddr, &'bufs [&'bufs [u8]], &'control [u8])>,
_mutable: PhantomData<T>,
}
impl<'addr, 'bufs, 'control, T> MsgHdr<'addr, 'bufs, 'control, T>
where T: MsgHdrMutableTrait
{
pub fn with_buffers_mut(mut self, bufs: &'bufs mut [IoSlice<'bufs>]) -> Self {
let ptr = bufs.as_ptr().cast_mut().cast();
sys::set_msghdr_iov(&mut self.inner, ptr, bufs.len());
self
}
}
impl<'addr, 'bufs, 'control, T> MsgHdr<'addr, 'bufs, 'control, T>
{
pub fn new() -> MsgHdr<'addr, 'bufs, 'control, T> {
MsgHdr {
inner: unsafe { mem::zeroed() },
_lifetimes: PhantomData,
_mutable: PhantomData,
}
}
pub fn with_buffers(mut self, bufs: &'bufs [IoSlice<'bufs>]) -> Self {
let ptr = bufs.as_ptr().cast_mut().cast();
sys::set_msghdr_iov(&mut self.inner, ptr, bufs.len());
self
}
}
fn main() {
let msghdr = MsgHdr::<Mutable>::new();
// blah blah
} |
Instead we'll create a separate MsgHdrMut for recvmsg, similar to IoSlice and IoSliceMut.
To be used in recvmsg(2) calls.
|
I've just added |
|
The support for WSARecvMsg may not be a straightforward task, this API in Windows is very quirky...
By the way, thank you very much for your efforts! If there is any work that I could help, please let me know directly. |
Yeah, I don't think |
pointer::cast_mut was stablised in 1.65, so it's too new.
|
After a couple of Windows exported weighted in on For now I've decided to drop Windows support for |
Darksonn
left a comment
There was a problem hiding this comment.
Clever, but I agree that this is correct.
Unfortunate that you need these #[cfg(not(target_os = "redox"))] all over the place. Perhaps some of it could be moved to a file to avoid that? I'll let you decide what you prefer here.
Yes, I agree. I can kind want to cleanup the But I rather not delay this pr for that. |
This adds wrappers around
sendmsg(2)andrecvmsg(2).