Skip to content

Commit 9159f69

Browse files
committed
Port from winapi to windows-sys.
1 parent f5b52f0 commit 9159f69

File tree

6 files changed

+95
-79
lines changed

6 files changed

+95
-79
lines changed

Cargo.toml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ repository = "https://github.com/sunfishcode/io-lifetimes"
1111
exclude = ["/.github"]
1212

1313
[dependencies]
14-
# io-lifetimes only depends on libc/winapi for the ability to close
15-
# fds/handles/sockets. The following are just optional dependencies to add
16-
# foreign-type impls for the traits. In the future, we'll prefer to have
17-
# crates provide their own impls; this is just a temporary measure.
14+
# io-lifetimes only depends on libc/windows-sys for the ability to close
15+
# and duplicate fds/handles/sockets. The following are just optional
16+
# dependencies to add foreign-type impls for the traits. In the future,
17+
# we'll prefer to have crates provide their own impls; this is just a
18+
# temporary measure.
1819

1920
# Optionally depend on async-std just to provide impls for its types for now.
2021
#
@@ -37,9 +38,18 @@ os_pipe = { version = "1.0.0", optional = true }
3738
[target.'cfg(not(windows))'.dependencies]
3839
libc = { version = "0.2.96", optional = true }
3940

40-
[target.'cfg(windows)'.dependencies]
41-
winapi = { version = "0.3.9", optional = true, features = ["handleapi", "std", "winsock2"] }
41+
[target.'cfg(windows)'.dependencies.windows-sys]
42+
version = "0.35.0"
43+
optional = true
44+
features = [
45+
"Win32_Foundation",
46+
"Win32_Storage_FileSystem",
47+
"Win32_Networking_WinSock",
48+
"Win32_Security",
49+
"Win32_System_IO",
50+
"Win32_System_Threading",
51+
]
4252

4353
[features]
4454
default = ["close"]
45-
close = ["libc", "winapi"]
55+
close = ["libc", "windows-sys"]

examples/hello.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use io_lifetimes::{AsFd, FromFd, OwnedFd};
1818
#[cfg(windows)]
1919
use io_lifetimes::{AsHandle, FromHandle, OwnedHandle};
2020
#[cfg(windows)]
21-
use std::{convert::TryInto, ptr::null_mut};
21+
use std::{convert::TryInto, os::windows::io::RawHandle, ptr::null_mut};
2222

2323
#[cfg(all(rustc_attrs, unix, feature = "close"))]
2424
fn main() -> io::Result<()> {
@@ -71,7 +71,7 @@ fn main() -> io::Result<()> {
7171
null_mut(),
7272
OPEN_EXISTING,
7373
FILE_ATTRIBUTE_NORMAL,
74-
null_mut(),
74+
null_mut() as RawHandle as HANDLE,
7575
)
7676
.try_into()
7777
.map_err(|()| io::Error::last_os_error())?;
@@ -86,7 +86,7 @@ fn main() -> io::Result<()> {
8686
null_mut(),
8787
);
8888
match (result, number_of_bytes_written) {
89-
(FALSE, _) => return Err(io::Error::last_os_error()),
89+
(0, _) => return Err(io::Error::last_os_error()),
9090
(_, 13) => (),
9191
(_, _) => return Err(io::Error::new(io::ErrorKind::Other, "short write")),
9292
}
@@ -109,7 +109,7 @@ fn main() -> io::Result<()> {
109109
null_mut(),
110110
);
111111
match (result, number_of_bytes_written) {
112-
(FALSE, _) => return Err(io::Error::last_os_error()),
112+
(0, _) => return Err(io::Error::last_os_error()),
113113
(_, 5) => (),
114114
(_, _) => return Err(io::Error::new(io::ErrorKind::Other, "short write")),
115115
}

src/example_ffi.rs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ use crate::{BorrowedHandle, HandleOrInvalid, OwnedHandle};
1111
#[cfg(any(unix, target_os = "wasi"))]
1212
use libc::{c_char, c_int, c_void, size_t, ssize_t};
1313
#[cfg(windows)]
14-
use winapi::{
15-
shared::minwindef::{BOOL, DWORD, LPCVOID, LPDWORD, LPVOID},
16-
shared::ntdef::{HANDLE, LPCWSTR},
17-
um::minwinbase::{LPOVERLAPPED, LPSECURITY_ATTRIBUTES},
14+
use {
15+
core::ffi::c_void,
16+
windows_sys::core::PCWSTR,
17+
windows_sys::Win32::Foundation::BOOL,
18+
windows_sys::Win32::Security::SECURITY_ATTRIBUTES,
19+
windows_sys::Win32::Storage::FileSystem::{
20+
FILE_ACCESS_FLAGS, FILE_CREATION_DISPOSITION, FILE_FLAGS_AND_ATTRIBUTES, FILE_SHARE_MODE,
21+
},
22+
windows_sys::Win32::System::IO::OVERLAPPED,
1823
};
1924

2025
// Declare a few FFI functions ourselves, to show off the FFI ergonomics.
@@ -36,32 +41,35 @@ pub use libc::{O_CLOEXEC, O_CREAT, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY};
3641
#[cfg(windows)]
3742
extern "system" {
3843
pub fn CreateFileW(
39-
lpFileName: LPCWSTR,
40-
dwDesiredAccess: DWORD,
41-
dwShareMode: DWORD,
42-
lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
43-
dwCreationDisposition: DWORD,
44-
dwFlagsAndAttributes: DWORD,
45-
hTemplateFile: HANDLE,
44+
lpfilename: PCWSTR,
45+
dwdesiredaccess: FILE_ACCESS_FLAGS,
46+
dwsharemode: FILE_SHARE_MODE,
47+
lpsecurityattributes: *const SECURITY_ATTRIBUTES,
48+
dwcreationdisposition: FILE_CREATION_DISPOSITION,
49+
dwflagsandattributes: FILE_FLAGS_AND_ATTRIBUTES,
50+
htemplatefile: HANDLE,
4651
) -> HandleOrInvalid;
4752
pub fn ReadFile(
48-
hFile: BorrowedHandle<'_>,
49-
lpBuffer: LPVOID,
50-
nNumberOfBytesToRead: DWORD,
51-
lpNumberOfBytesRead: LPDWORD,
52-
lpOverlapped: LPOVERLAPPED,
53+
hfile: BorrowedHandle<'_>,
54+
lpbuffer: *mut c_void,
55+
nnumberofbytestoread: u32,
56+
lpnumberofbytesread: *mut u32,
57+
lpoverlapped: *mut OVERLAPPED,
5358
) -> BOOL;
5459
pub fn WriteFile(
55-
hFile: BorrowedHandle<'_>,
56-
lpBuffer: LPCVOID,
57-
nNumberOfBytesToWrite: DWORD,
58-
lpNumberOfBytesWritten: LPDWORD,
59-
lpOverlapped: LPOVERLAPPED,
60+
hfile: BorrowedHandle<'_>,
61+
lpbuffer: *const c_void,
62+
nnumberofbytestowrite: u32,
63+
lpnumberofbyteswritten: *mut u32,
64+
lpoverlapped: *mut OVERLAPPED,
6065
) -> BOOL;
6166
}
67+
6268
#[cfg(windows)]
63-
pub use winapi::{
64-
shared::minwindef::{FALSE, TRUE},
65-
um::fileapi::{CREATE_ALWAYS, CREATE_NEW, OPEN_EXISTING},
66-
um::winnt::{FILE_ATTRIBUTE_NORMAL, FILE_GENERIC_READ, FILE_GENERIC_WRITE},
69+
pub use {
70+
windows_sys::Win32::Foundation::HANDLE,
71+
windows_sys::Win32::Storage::FileSystem::{
72+
CREATE_ALWAYS, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, FILE_GENERIC_READ, FILE_GENERIC_WRITE,
73+
OPEN_EXISTING,
74+
},
6775
};

src/types.rs

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,24 @@ use std::{
1414
},
1515
};
1616
#[cfg(all(windows, feature = "close"))]
17-
use winapi::{
18-
shared::minwindef::{BOOL, DWORD},
19-
shared::ntdef::HANDLE,
20-
um::handleapi::DuplicateHandle,
21-
um::handleapi::SetHandleInformation,
22-
um::handleapi::INVALID_HANDLE_VALUE,
23-
um::processthreadsapi::GetCurrentProcess,
24-
um::processthreadsapi::GetCurrentProcessId,
25-
um::winbase::HANDLE_FLAG_INHERIT,
26-
um::winnt::DUPLICATE_SAME_ACCESS,
27-
um::winsock2::WSADuplicateSocketW,
28-
um::winsock2::WSAGetLastError,
29-
um::winsock2::WSASocketW,
30-
um::winsock2::INVALID_SOCKET,
31-
um::winsock2::SOCKET_ERROR,
32-
um::winsock2::WSAEINVAL,
33-
um::winsock2::WSAEPROTOTYPE,
34-
um::winsock2::WSAPROTOCOL_INFOW,
35-
um::winsock2::WSA_FLAG_NO_HANDLE_INHERIT,
36-
um::winsock2::WSA_FLAG_OVERLAPPED,
17+
use {
18+
windows_sys::Win32::Foundation::{
19+
CloseHandle, DuplicateHandle, SetHandleInformation, BOOL, DUPLICATE_HANDLE_OPTIONS,
20+
DUPLICATE_SAME_ACCESS, HANDLE, HANDLE_FLAG_INHERIT, INVALID_HANDLE_VALUE,
21+
},
22+
windows_sys::Win32::Networking::WinSock::{
23+
closesocket, WSADuplicateSocketW, WSAGetLastError, WSASocketW, INVALID_SOCKET, SOCKET,
24+
SOCKET_ERROR, WSAEINVAL, WSAEPROTOTYPE, WSAPROTOCOL_INFOW, WSA_FLAG_NO_HANDLE_INHERIT,
25+
WSA_FLAG_OVERLAPPED,
26+
},
27+
windows_sys::Win32::System::Threading::{GetCurrentProcess, GetCurrentProcessId},
3728
};
3829

39-
#[cfg(all(windows, not(feature = "winapi")))]
40-
const INVALID_HANDLE_VALUE: *mut core::ffi::c_void = !0 as _;
41-
#[cfg(all(windows, not(feature = "winapi")))]
30+
#[cfg(all(windows, not(feature = "close")))]
31+
type HANDLE = isize;
32+
#[cfg(all(windows, not(feature = "close")))]
33+
const INVALID_HANDLE_VALUE: HANDLE = !0 as _;
34+
#[cfg(all(windows, not(feature = "close")))]
4235
const INVALID_SOCKET: usize = !0 as _;
4336

4437
/// A borrowed file descriptor.
@@ -235,16 +228,16 @@ impl OwnedHandle {
235228
#[cfg(feature = "close")]
236229
pub(crate) fn duplicate(
237230
&self,
238-
access: DWORD,
231+
access: u32,
239232
inherit: bool,
240-
options: DWORD,
233+
options: DUPLICATE_HANDLE_OPTIONS,
241234
) -> std::io::Result<Self> {
242235
let mut ret = 0 as HANDLE;
243236
match unsafe {
244237
let cur_proc = GetCurrentProcess();
245238
DuplicateHandle(
246239
cur_proc,
247-
self.as_raw_handle(),
240+
self.as_raw_handle() as HANDLE,
248241
cur_proc,
249242
&mut ret,
250243
access,
@@ -255,7 +248,7 @@ impl OwnedHandle {
255248
0 => return Err(std::io::Error::last_os_error()),
256249
_ => (),
257250
}
258-
unsafe { Ok(Self::from_raw_handle(ret)) }
251+
unsafe { Ok(Self::from_raw_handle(ret as RawHandle)) }
259252
}
260253
}
261254

@@ -496,7 +489,7 @@ impl TryFrom<HandleOrInvalid> for OwnedHandle {
496489
#[inline]
497490
fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, ()> {
498491
let raw = handle_or_invalid.0;
499-
if raw == INVALID_HANDLE_VALUE {
492+
if raw as HANDLE == INVALID_HANDLE_VALUE {
500493
// Don't call `CloseHandle`; it'd be harmless, except that it could
501494
// overwrite the `GetLastError` error.
502495
forget(handle_or_invalid);
@@ -706,7 +699,7 @@ impl Drop for OwnedHandle {
706699
fn drop(&mut self) {
707700
#[cfg(feature = "close")]
708701
unsafe {
709-
let _ = winapi::um::handleapi::CloseHandle(self.handle);
702+
let _ = CloseHandle(self.handle as HANDLE);
710703
}
711704

712705
// If the `close` feature is disabled, we expect users to avoid letting
@@ -724,7 +717,7 @@ impl Drop for HandleOrInvalid {
724717
fn drop(&mut self) {
725718
#[cfg(feature = "close")]
726719
unsafe {
727-
let _ = winapi::um::handleapi::CloseHandle(self.0);
720+
let _ = CloseHandle(self.0 as HANDLE);
728721
}
729722

730723
// If the `close` feature is disabled, we expect users to avoid letting
@@ -742,7 +735,7 @@ impl Drop for HandleOrNull {
742735
fn drop(&mut self) {
743736
#[cfg(feature = "close")]
744737
unsafe {
745-
let _ = winapi::um::handleapi::CloseHandle(self.0);
738+
let _ = CloseHandle(self.0 as HANDLE);
746739
}
747740

748741
// If the `close` feature is disabled, we expect users to avoid letting
@@ -760,7 +753,7 @@ impl Drop for OwnedSocket {
760753
fn drop(&mut self) {
761754
#[cfg(feature = "close")]
762755
unsafe {
763-
let _ = winapi::um::winsock2::closesocket(self.socket as winapi::um::winsock2::SOCKET);
756+
let _ = closesocket(self.socket as SOCKET);
764757
}
765758

766759
// If the `close` feature is disabled, we expect users to avoid letting

tests/assumptions.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,18 @@ fn test_assumptions() {
2828
fn test_assumptions() {
2929
assert_eq!(
3030
size_of::<std::os::windows::io::RawSocket>(),
31-
size_of::<winapi::um::winsock2::SOCKET>()
31+
size_of::<windows_sys::Win32::Networking::WinSock::SOCKET>()
32+
);
33+
assert_eq!(
34+
size_of::<std::os::windows::io::RawHandle>(),
35+
size_of::<windows_sys::Win32::Foundation::HANDLE>()
36+
);
37+
assert_eq!(
38+
windows_sys::Win32::Networking::WinSock::INVALID_SOCKET,
39+
usize::MAX
3240
);
33-
assert_eq!(winapi::um::winsock2::INVALID_SOCKET, usize::MAX);
34-
3541
assert_ne!(
36-
winapi::um::handleapi::INVALID_HANDLE_VALUE,
37-
std::ptr::null_mut()
42+
windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE,
43+
std::ptr::null_mut() as std::os::windows::io::RawHandle as _
3844
);
3945
}

tests/ffi.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ use io_lifetimes::example_ffi::*;
77
#[cfg(windows)]
88
use io_lifetimes::OwnedHandle;
99
#[cfg(windows)]
10-
use std::{convert::TryInto, ptr::null_mut};
10+
use std::{convert::TryInto, os::windows::io::RawHandle, ptr::null_mut};
1111
#[cfg(windows)]
12-
use winapi::{
13-
um::fileapi::OPEN_EXISTING,
14-
um::winnt::{FILE_ATTRIBUTE_NORMAL, FILE_GENERIC_READ},
12+
use windows_sys::Win32::Storage::FileSystem::{
13+
FILE_ATTRIBUTE_NORMAL, FILE_GENERIC_READ, OPEN_EXISTING,
1514
};
1615

1716
#[cfg(all(rustc_attrs, unix))]
@@ -41,7 +40,7 @@ fn test_file_not_found() {
4140
null_mut(),
4241
OPEN_EXISTING,
4342
FILE_ATTRIBUTE_NORMAL,
44-
null_mut(),
43+
null_mut() as RawHandle as HANDLE,
4544
)
4645
}
4746
.try_into();
@@ -73,7 +72,7 @@ fn test_file_found() {
7372
null_mut(),
7473
OPEN_EXISTING,
7574
FILE_ATTRIBUTE_NORMAL,
76-
null_mut(),
75+
null_mut() as RawHandle as HANDLE,
7776
)
7877
}
7978
.try_into();

0 commit comments

Comments
 (0)