Skip to content

Commit

Permalink
Auto merge of #110152 - ChrisDenton:windows-sys, r=thomcc
Browse files Browse the repository at this point in the history
Start using `windows sys` for Windows FFI bindings in std

Switch to using windows-sys for FFI. In order to avoid some currently contentious issues, this uses windows-bindgen to generate a smaller set of bindings instead of using the full crate.

Unlike the windows-sys crate, the generated bindings uses `*mut c_void` for handle types instead of `isize`. This to sidestep opsem concerns about mixing pointer types and integers between languages. Note that `SOCKET` remains defined as an integer but instead of being a usize, it's changed to fit the [standard library definition](https://github.com/rust-lang/rust/blob/a41fc00eaf352541008965fec0dee811e44373b3/library/std/src/os/windows/raw.rs#L12-L16):

```rust
#[cfg(target_pointer_width = "32")]
pub type SOCKET = u32;
#[cfg(target_pointer_width = "64")]
pub type SOCKET = u64;
```

The generated bindings also customizes the `#[link]` imports. I hope to switch to using raw-dylib but I don't want to tie that too closely with the switch to windows-sys.

---

Changes outside of the bindings are, for the most part, fairly minimal (e.g. some differences in `*mut` vs. `*const` or a few types differ). One issue is that our own bindings sometimes mix in higher level types, like `BorrowedHandle`. This is pretty adhoc though.
  • Loading branch information
bors committed May 9, 2023
2 parents 33a01e2 + e314a3b commit 7e7483d
Show file tree
Hide file tree
Showing 23 changed files with 7,187 additions and 3,098 deletions.
29 changes: 29 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,13 @@ dependencies = [
"serde_json",
]

[[package]]
name = "generate-windows-sys"
version = "0.1.0"
dependencies = [
"windows-bindgen",
]

[[package]]
name = "generic-array"
version = "0.14.4"
Expand Down Expand Up @@ -5509,6 +5516,22 @@ dependencies = [
"windows-targets 0.48.0",
]

[[package]]
name = "windows-bindgen"
version = "0.49.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6935fb09b84ee57929ae92518b475f5dfdfbeb87c5334756acc28ee8e202b60"
dependencies = [
"windows-metadata",
"windows-tokens",
]

[[package]]
name = "windows-metadata"
version = "0.49.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f5bca94a32bf1e6a376522b6601275a3b611ee885ec0f1b6a05f17e8cfd3385"

[[package]]
name = "windows-sys"
version = "0.42.0"
Expand Down Expand Up @@ -5572,6 +5595,12 @@ dependencies = [
"windows_x86_64_msvc 0.48.0",
]

[[package]]
name = "windows-tokens"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b34c9a3b28cb41db7385546f7f9a8179348dffc89923dde66857b1ba5312f6b4"

[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.2"
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ members = [
"src/tools/collect-license-metadata",
"src/tools/generate-copyright",
"src/tools/suggest-tests",
"src/tools/generate-windows-sys",
]

exclude = [
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/os/windows/io/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl BorrowedSocket<'_> {
/// object as the existing `BorrowedSocket` instance.
#[stable(feature = "io_safety", since = "1.63.0")]
pub fn try_clone_to_owned(&self) -> io::Result<OwnedSocket> {
let mut info = unsafe { mem::zeroed::<c::WSAPROTOCOL_INFO>() };
let mut info = unsafe { mem::zeroed::<c::WSAPROTOCOL_INFOW>() };
let result = unsafe {
c::WSADuplicateSocketW(self.as_raw_socket(), c::GetCurrentProcessId(), &mut info)
};
Expand Down
Loading

0 comments on commit 7e7483d

Please sign in to comment.