Skip to content

Commit aeb6c2d

Browse files
committed
Replace winapi with windows-sys
windows-sys is officially supported by Microsoft.
1 parent 040769b commit aeb6c2d

File tree

7 files changed

+171
-169
lines changed

7 files changed

+171
-169
lines changed

Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ features = ["all"]
3535
[target."cfg(unix)".dependencies]
3636
libc = "0.2.124"
3737

38-
[target."cfg(windows)".dependencies]
39-
winapi = { version = "0.3.9", features = ["handleapi", "ws2ipdef", "ws2tcpip"] }
38+
[target.'cfg(windows)'.dependencies.windows-sys]
39+
version = "=0.36"
40+
features = [
41+
"Win32_Foundation",
42+
"Win32_Networking_WinSock",
43+
"Win32_System_IO",
44+
"Win32_System_WindowsProgramming",
45+
]
4046

4147
[features]
4248
# Enable all API, even ones not available on all OSs.

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ macro_rules! impl_debug {
7878
$(#[$target: meta])*
7979
// The flag(s) to check.
8080
// Need to specific the libc crate because Windows doesn't use
81-
// `libc` but `winapi`.
81+
// `libc` but `windows_sys`.
8282
$libc: ident :: $flag: ident
8383
),+ $(,)*
8484
) => {

src/sockaddr.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use std::mem::{self, size_of, MaybeUninit};
22
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
33
use std::{fmt, io};
44

5+
#[cfg(windows)]
6+
use windows_sys::Win32::Networking::WinSock::SOCKADDR_IN6_0;
7+
58
use crate::sys::{
69
sa_family_t, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t, AF_INET,
710
AF_INET6,
811
};
9-
#[cfg(windows)]
10-
use winapi::shared::ws2ipdef::SOCKADDR_IN6_LH_u;
1112

1213
/// The address of a socket.
1314
///
@@ -183,7 +184,7 @@ impl SockAddr {
183184
addr.sin6_scope_id,
184185
#[cfg(windows)]
185186
unsafe {
186-
*addr.u.sin6_scope_id()
187+
addr.Anonymous.sin6_scope_id
187188
},
188189
)))
189190
} else {
@@ -249,13 +250,6 @@ impl From<SocketAddrV4> for SockAddr {
249250

250251
impl From<SocketAddrV6> for SockAddr {
251252
fn from(addr: SocketAddrV6) -> SockAddr {
252-
#[cfg(windows)]
253-
let u = unsafe {
254-
let mut u = mem::zeroed::<SOCKADDR_IN6_LH_u>();
255-
*u.sin6_scope_id_mut() = addr.scope_id();
256-
u
257-
};
258-
259253
let sockaddr_in6 = sockaddr_in6 {
260254
sin6_family: AF_INET6 as sa_family_t,
261255
sin6_port: addr.port().to_be(),
@@ -264,7 +258,9 @@ impl From<SocketAddrV6> for SockAddr {
264258
#[cfg(unix)]
265259
sin6_scope_id: addr.scope_id(),
266260
#[cfg(windows)]
267-
u,
261+
Anonymous: SOCKADDR_IN6_0 {
262+
sin6_scope_id: addr.scope_id(),
263+
},
268264
#[cfg(any(
269265
target_os = "dragonfly",
270266
target_os = "freebsd",

src/sockref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ where
128128
/// See the `From<&impl AsRawFd>` implementation.
129129
fn from(socket: &'s S) -> Self {
130130
let socket = socket.as_raw_socket();
131-
assert!(socket != winapi::um::winsock2::INVALID_SOCKET as _);
131+
assert!(socket != windows_sys::Win32::Networking::WinSock::INVALID_SOCKET as _);
132132
SockRef {
133133
socket: ManuallyDrop::new(unsafe { Socket::from_raw_socket(socket) }),
134134
_lifetime: PhantomData,

src/sys/unix.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ macro_rules! syscall {
174174

175175
/// Maximum size of a buffer passed to system call like `recv` and `send`.
176176
#[cfg(not(target_vendor = "apple"))]
177-
const MAX_BUF_LEN: usize = <ssize_t>::max_value() as usize;
177+
const MAX_BUF_LEN: usize = ssize_t::MAX as usize;
178178

179179
// The maximum read limit on most posix-like systems is `SSIZE_MAX`, with the
180180
// man page quoting that if the count of bytes to read is greater than
@@ -185,7 +185,7 @@ const MAX_BUF_LEN: usize = <ssize_t>::max_value() as usize;
185185
// than or equal to INT_MAX. To handle both of these the read size is capped on
186186
// both platforms.
187187
#[cfg(target_vendor = "apple")]
188-
const MAX_BUF_LEN: usize = <c_int>::max_value() as usize - 1;
188+
const MAX_BUF_LEN: usize = c_int::MAX as usize - 1;
189189

190190
#[cfg(any(
191191
all(
@@ -622,7 +622,7 @@ pub(crate) fn poll_connect(socket: &crate::Socket, timeout: Duration) -> io::Res
622622
}
623623

624624
let timeout = (timeout - elapsed).as_millis();
625-
let timeout = clamp(timeout, 1, c_int::max_value() as u128) as c_int;
625+
let timeout = clamp(timeout, 1, c_int::MAX as u128) as c_int;
626626

627627
match syscall!(poll(&mut pollfd, 1, timeout)) {
628628
Ok(0) => return Err(io::ErrorKind::TimedOut.into()),
@@ -878,7 +878,7 @@ fn into_timeval(duration: Option<Duration>) -> libc::timeval {
878878
// https://github.com/rust-lang/libc/issues/1848
879879
#[cfg_attr(target_env = "musl", allow(deprecated))]
880880
Some(duration) => libc::timeval {
881-
tv_sec: min(duration.as_secs(), libc::time_t::max_value() as u64) as libc::time_t,
881+
tv_sec: min(duration.as_secs(), libc::time_t::MAX as u64) as libc::time_t,
882882
tv_usec: duration.subsec_micros() as libc::suseconds_t,
883883
},
884884
None => libc::timeval {
@@ -931,7 +931,7 @@ pub(crate) fn set_tcp_keepalive(fd: Socket, keepalive: &TcpKeepalive) -> io::Res
931931

932932
#[cfg(not(any(target_os = "haiku", target_os = "openbsd")))]
933933
fn into_secs(duration: Duration) -> c_int {
934-
min(duration.as_secs(), c_int::max_value() as u64) as c_int
934+
min(duration.as_secs(), c_int::MAX as u64) as c_int
935935
}
936936

937937
/// Add `flag` to the current set flags of `F_GETFD`.

0 commit comments

Comments
 (0)