-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Socket::{send,recv}msg and MsgHdr(Mut) #447
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
.