Skip to content

build: allow windows-sys 0.59 #545

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ features = ["all"]
libc = "0.2.150"

[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.52"
version = ">=0.52,<0.60"
features = [
"Win32_Foundation",
"Win32_Networking_WinSock",
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ impl RecvFlags {
#[repr(transparent)]
pub struct MaybeUninitSlice<'a>(sys::MaybeUninitSlice<'a>);

impl<'a> fmt::Debug for MaybeUninitSlice<'a> {
impl fmt::Debug for MaybeUninitSlice<'_> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.0.as_slice(), fmt)
}
Expand All @@ -406,15 +406,15 @@ impl<'a> MaybeUninitSlice<'a> {
}
}

impl<'a> Deref for MaybeUninitSlice<'a> {
impl Deref for MaybeUninitSlice<'_> {
type Target = [MaybeUninit<u8>];

fn deref(&self) -> &[MaybeUninit<u8>] {
self.0.as_slice()
}
}

impl<'a> DerefMut for MaybeUninitSlice<'a> {
impl DerefMut for MaybeUninitSlice<'_> {
fn deref_mut(&mut self) -> &mut [MaybeUninit<u8>] {
self.0.as_mut_slice()
}
Expand Down Expand Up @@ -659,7 +659,7 @@ impl<'addr, 'bufs, 'control> MsgHdr<'addr, 'bufs, 'control> {
}

#[cfg(not(target_os = "redox"))]
impl<'name, 'bufs, 'control> fmt::Debug for MsgHdr<'name, 'bufs, 'control> {
impl fmt::Debug for MsgHdr<'_, '_, '_> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
"MsgHdr".fmt(fmt)
}
Expand Down Expand Up @@ -736,7 +736,7 @@ impl<'addr, 'bufs, 'control> MsgHdrMut<'addr, 'bufs, 'control> {
}

#[cfg(not(target_os = "redox"))]
impl<'name, 'bufs, 'control> fmt::Debug for MsgHdrMut<'name, 'bufs, 'control> {
impl fmt::Debug for MsgHdrMut<'_, '_, '_> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
"MsgHdrMut".fmt(fmt)
}
Expand Down
4 changes: 2 additions & 2 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2349,7 +2349,7 @@ impl Read for Socket {
}
}

impl<'a> Read for &'a Socket {
impl Read for &Socket {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
// Safety: see other `Read::read` impl.
let buf = unsafe { &mut *(buf as *mut [u8] as *mut [MaybeUninit<u8>]) };
Expand Down Expand Up @@ -2379,7 +2379,7 @@ impl Write for Socket {
}
}

impl<'a> Write for &'a Socket {
impl Write for &Socket {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.send(buf)
}
Expand Down
2 changes: 1 addition & 1 deletion src/sockref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub struct SockRef<'s> {
_lifetime: PhantomData<&'s Socket>,
}

impl<'s> Deref for SockRef<'s> {
impl Deref for SockRef<'_> {
type Target = Socket;

fn deref(&self) -> &Self::Target {
Expand Down
4 changes: 2 additions & 2 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,9 +662,9 @@ pub struct MaybeUninitSlice<'a> {
_lifetime: PhantomData<&'a mut [MaybeUninit<u8>]>,
}

unsafe impl<'a> Send for MaybeUninitSlice<'a> {}
unsafe impl Send for MaybeUninitSlice<'_> {}

unsafe impl<'a> Sync for MaybeUninitSlice<'a> {}
unsafe impl Sync for MaybeUninitSlice<'_> {}

impl<'a> MaybeUninitSlice<'a> {
pub(crate) fn new(buf: &'a mut [MaybeUninit<u8>]) -> MaybeUninitSlice<'a> {
Expand Down
53 changes: 30 additions & 23 deletions src/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl_debug!(
impl Type {
/// Our custom flag to set `WSA_FLAG_NO_HANDLE_INHERIT` on socket creation.
/// Trying to mimic `Type::cloexec` on windows.
const NO_INHERIT: c_int = 1 << ((size_of::<c_int>() * 8) - 1); // Last bit.
const NO_INHERIT: c_int = 1 << (c_int::BITS as usize - 1); // Last bit.

/// Set `WSA_FLAG_NO_HANDLE_INHERIT` on the socket.
#[cfg(feature = "all")]
Expand Down Expand Up @@ -166,9 +166,9 @@ pub struct MaybeUninitSlice<'a> {
_lifetime: PhantomData<&'a mut [MaybeUninit<u8>]>,
}

unsafe impl<'a> Send for MaybeUninitSlice<'a> {}
unsafe impl Send for MaybeUninitSlice<'_> {}

unsafe impl<'a> Sync for MaybeUninitSlice<'a> {}
unsafe impl Sync for MaybeUninitSlice<'_> {}

impl<'a> MaybeUninitSlice<'a> {
pub fn new(buf: &'a mut [MaybeUninit<u8>]) -> MaybeUninitSlice<'a> {
Expand Down Expand Up @@ -251,7 +251,7 @@ pub(crate) fn socket(family: c_int, mut ty: c_int, protocol: c_int) -> io::Resul

// Check if we set our custom flag.
let flags = if ty & Type::NO_INHERIT != 0 {
ty = ty & !Type::NO_INHERIT;
ty &= !Type::NO_INHERIT;
WSA_FLAG_NO_HANDLE_INHERIT
} else {
0
Expand Down Expand Up @@ -284,7 +284,7 @@ pub(crate) fn poll_connect(socket: &crate::Socket, timeout: Duration) -> io::Res

let mut fd_array = WSAPOLLFD {
fd: socket.as_raw(),
events: (POLLRDNORM | POLLWRNORM) as i16,
events: (POLLRDNORM | POLLWRNORM),
revents: 0,
};

Expand All @@ -305,9 +305,7 @@ pub(crate) fn poll_connect(socket: &crate::Socket, timeout: Duration) -> io::Res
Ok(0) => return Err(io::ErrorKind::TimedOut.into()),
Ok(_) => {
// Error or hang up indicates an error (or failure to connect).
if (fd_array.revents & POLLERR as i16) != 0
|| (fd_array.revents & POLLHUP as i16) != 0
{
if (fd_array.revents & POLLERR) != 0 || (fd_array.revents & POLLHUP) != 0 {
match socket.take_error() {
Ok(Some(err)) => return Err(err),
Ok(None) => {
Expand Down Expand Up @@ -422,7 +420,7 @@ pub(crate) fn shutdown(socket: Socket, how: Shutdown) -> io::Result<()> {
Shutdown::Write => SD_SEND,
Shutdown::Read => SD_RECEIVE,
Shutdown::Both => SD_BOTH,
} as i32;
};
syscall!(shutdown(socket, how), PartialEq::eq, SOCKET_ERROR).map(|_| ())
}

Expand All @@ -439,7 +437,7 @@ pub(crate) fn recv(socket: Socket, buf: &mut [MaybeUninit<u8>], flags: c_int) ->
);
match res {
Ok(n) => Ok(n as usize),
Err(ref err) if err.raw_os_error() == Some(WSAESHUTDOWN as i32) => Ok(0),
Err(ref err) if err.raw_os_error() == Some(WSAESHUTDOWN) => Ok(0),
Err(err) => Err(err),
}
}
Expand All @@ -466,8 +464,8 @@ pub(crate) fn recv_vectored(
);
match res {
Ok(_) => Ok((nread as usize, RecvFlags(0))),
Err(ref err) if err.raw_os_error() == Some(WSAESHUTDOWN as i32) => Ok((0, RecvFlags(0))),
Err(ref err) if err.raw_os_error() == Some(WSAEMSGSIZE as i32) => {
Err(ref err) if err.raw_os_error() == Some(WSAESHUTDOWN) => Ok((0, RecvFlags(0))),
Err(ref err) if err.raw_os_error() == Some(WSAEMSGSIZE) => {
Ok((nread as usize, RecvFlags(MSG_TRUNC)))
}
Err(err) => Err(err),
Expand Down Expand Up @@ -496,7 +494,7 @@ pub(crate) fn recv_from(
);
match res {
Ok(n) => Ok(n as usize),
Err(ref err) if err.raw_os_error() == Some(WSAESHUTDOWN as i32) => Ok(0),
Err(ref err) if err.raw_os_error() == Some(WSAESHUTDOWN) => Ok(0),
Err(err) => Err(err),
}
})
Expand All @@ -523,9 +521,7 @@ pub(crate) fn peek_sender(socket: Socket) -> io::Result<SockAddr> {
match res {
Ok(_n) => Ok(()),
Err(e) => match e.raw_os_error() {
Some(code) if code == (WSAESHUTDOWN as i32) || code == (WSAEMSGSIZE as i32) => {
Ok(())
}
Some(code) if code == WSAESHUTDOWN || code == WSAEMSGSIZE => Ok(()),
_ => Err(e),
},
}
Expand Down Expand Up @@ -562,10 +558,10 @@ pub(crate) fn recv_from_vectored(
);
match res {
Ok(_) => Ok((nread as usize, RecvFlags(0))),
Err(ref err) if err.raw_os_error() == Some(WSAESHUTDOWN as i32) => {
Err(ref err) if err.raw_os_error() == Some(WSAESHUTDOWN) => {
Ok((nread as usize, RecvFlags(0)))
}
Err(ref err) if err.raw_os_error() == Some(WSAEMSGSIZE as i32) => {
Err(ref err) if err.raw_os_error() == Some(WSAEMSGSIZE) => {
Ok((nread as usize, RecvFlags(MSG_TRUNC)))
}
Err(err) => Err(err),
Expand Down Expand Up @@ -699,7 +695,7 @@ fn from_ms(duration: u32) -> Option<Duration> {
} else {
let secs = duration / 1000;
let nsec = (duration % 1000) * 1000000;
Some(Duration::new(secs as u64, nsec as u32))
Some(Duration::new(secs as u64, nsec))
}
}

Expand Down Expand Up @@ -760,7 +756,7 @@ pub(crate) unsafe fn getsockopt<T>(socket: Socket, level: c_int, optname: i32) -
syscall!(
getsockopt(
socket,
level as i32,
level,
optname,
optval.as_mut_ptr().cast(),
&mut optlen,
Expand All @@ -786,7 +782,7 @@ pub(crate) unsafe fn setsockopt<T>(
syscall!(
setsockopt(
socket,
level as i32,
level,
optname,
(&optval as *const T).cast(),
mem::size_of::<T>() as c_int,
Expand Down Expand Up @@ -930,6 +926,17 @@ pub(crate) fn unix_sockaddr(path: &Path) -> io::Result<SockAddr> {
}

storage.sun_family = crate::sys::AF_UNIX as sa_family_t;

// `windows-sys` 0.52.* represents `SOCKADDR_UN::sun_path` as `&[u8]`, but 0.59.*
// represents it as `&[i8]`.
//
// TODO: Remove this once `windows-sys` 0.52.* is no longer
// permitted as a dependency.
//
// SAFETY: We are safe in doing this, because: `bytes` starts as `&[u8]`, and is converted
// to a `&[u8]` or `&[i8]`, and all of these types have the same size and alignment.
let bytes = unsafe { slice::from_raw_parts(bytes.as_ptr().cast(), bytes.len()) };

// `storage` was initialized to zero above, so the path is
// already null terminated.
storage.sun_path[..bytes.len()].copy_from_slice(bytes);
Expand Down Expand Up @@ -1034,14 +1041,14 @@ impl FromRawSocket for crate::Socket {
fn in_addr_convertion() {
let ip = Ipv4Addr::new(127, 0, 0, 1);
let raw = to_in_addr(&ip);
assert_eq!(unsafe { raw.S_un.S_addr }, 127 << 0 | 1 << 24);
assert_eq!(unsafe { raw.S_un.S_addr }, 127 | 1 << 24);
assert_eq!(from_in_addr(raw), ip);

let ip = Ipv4Addr::new(127, 34, 4, 12);
let raw = to_in_addr(&ip);
assert_eq!(
unsafe { raw.S_un.S_addr },
127 << 0 | 34 << 8 | 4 << 16 | 12 << 24
127 | 34 << 8 | 4 << 16 | 12 << 24
);
assert_eq!(from_in_addr(raw), ip);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ fn unix_sockets_supported() -> bool {
Ok(_) => {}
Err(err)
if err.raw_os_error()
== Some(windows_sys::Win32::Networking::WinSock::WSAEAFNOSUPPORT as i32) =>
== Some(windows_sys::Win32::Networking::WinSock::WSAEAFNOSUPPORT) =>
{
return false;
}
Expand Down
Loading