Skip to content

Commit 3ffb27f

Browse files
committed
Use new bindings
1 parent e92ee03 commit 3ffb27f

File tree

12 files changed

+70
-53
lines changed

12 files changed

+70
-53
lines changed

library/std/src/os/windows/io/socket.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl BorrowedSocket<'_> {
110110
/// object as the existing `BorrowedSocket` instance.
111111
#[stable(feature = "io_safety", since = "1.63.0")]
112112
pub fn try_clone_to_owned(&self) -> io::Result<OwnedSocket> {
113-
let mut info = unsafe { mem::zeroed::<c::WSAPROTOCOL_INFO>() };
113+
let mut info = unsafe { mem::zeroed::<c::WSAPROTOCOL_INFOW>() };
114114
let result = unsafe {
115115
c::WSADuplicateSocketW(self.as_raw_socket(), c::GetCurrentProcessId(), &mut info)
116116
};

library/std/src/sys/windows/compat.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,20 @@ impl Module {
114114
/// (e.g. kernel32 and ntdll).
115115
pub unsafe fn new(name: &CStr) -> Option<Self> {
116116
// SAFETY: A CStr is always null terminated.
117-
let module = c::GetModuleHandleA(name.as_ptr());
117+
let module = c::GetModuleHandleA(name.as_ptr().cast::<u8>());
118118
NonNull::new(module).map(Self)
119119
}
120120

121121
// Try to get the address of a function.
122122
pub fn proc_address(self, name: &CStr) -> Option<NonNull<c_void>> {
123-
// SAFETY:
124-
// `self.0` will always be a valid module.
125-
// A CStr is always null terminated.
126-
let proc = unsafe { c::GetProcAddress(self.0.as_ptr(), name.as_ptr()) };
127-
NonNull::new(proc)
123+
unsafe {
124+
// SAFETY:
125+
// `self.0` will always be a valid module.
126+
// A CStr is always null terminated.
127+
let proc = c::GetProcAddress(self.0.as_ptr(), name.as_ptr().cast::<u8>());
128+
// SAFETY: `GetProcAddress` returns None on null.
129+
proc.map(|p| NonNull::new_unchecked(p as *mut c_void))
130+
}
128131
}
129132
}
130133

@@ -199,6 +202,7 @@ macro_rules! compat_fn_optional {
199202
)+) => (
200203
$(
201204
pub mod $symbol {
205+
#[allow(unused_imports)]
202206
use super::*;
203207
use crate::ffi::c_void;
204208
use crate::mem;

library/std/src/sys/windows/fs.rs

+26-13
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ pub struct FileTimes {
8989
accessed: Option<c::FILETIME>,
9090
modified: Option<c::FILETIME>,
9191
}
92+
impl core::fmt::Debug for c::FILETIME {
93+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94+
let time = ((self.dwHighDateTime as u64) << 32) | self.dwLowDateTime as u64;
95+
f.debug_tuple("FILETIME").field(&time).finish()
96+
}
97+
}
9298

9399
#[derive(Debug)]
94100
pub struct DirBuilder;
@@ -290,6 +296,7 @@ impl File {
290296
ptr::null_mut(),
291297
)
292298
};
299+
let handle = unsafe { HandleOrInvalid::from_raw_handle(handle) };
293300
if let Ok(handle) = handle.try_into() {
294301
Ok(File { handle: Handle::from_inner(handle) })
295302
} else {
@@ -501,7 +508,8 @@ impl File {
501508
}
502509

503510
fn readlink(&self) -> io::Result<PathBuf> {
504-
let mut space = Align8([MaybeUninit::<u8>::uninit(); c::MAXIMUM_REPARSE_DATA_BUFFER_SIZE]);
511+
let mut space =
512+
Align8([MaybeUninit::<u8>::uninit(); c::MAXIMUM_REPARSE_DATA_BUFFER_SIZE as usize]);
505513
let (_bytes, buf) = self.reparse_point(&mut space)?;
506514
unsafe {
507515
let (path_buffer, subst_off, subst_len, relative) = match (*buf).ReparseTag {
@@ -589,7 +597,11 @@ impl File {
589597
));
590598
}
591599
cvt(unsafe {
592-
c::SetFileTime(self.as_handle(), None, times.accessed.as_ref(), times.modified.as_ref())
600+
let accessed =
601+
times.accessed.as_ref().map(|a| a as *const c::FILETIME).unwrap_or(ptr::null());
602+
let modified =
603+
times.modified.as_ref().map(|a| a as *const c::FILETIME).unwrap_or(ptr::null());
604+
c::SetFileTime(self.as_raw_handle(), ptr::null_mut(), accessed, modified)
593605
})?;
594606
Ok(())
595607
}
@@ -618,9 +630,9 @@ impl File {
618630
/// then errors will be `ERROR_NOT_SUPPORTED` or `ERROR_INVALID_PARAMETER`.
619631
fn posix_delete(&self) -> io::Result<()> {
620632
let mut info = c::FILE_DISPOSITION_INFO_EX {
621-
Flags: c::FILE_DISPOSITION_DELETE
622-
| c::FILE_DISPOSITION_POSIX_SEMANTICS
623-
| c::FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE,
633+
Flags: c::FILE_DISPOSITION_FLAG_DELETE
634+
| c::FILE_DISPOSITION_FLAG_POSIX_SEMANTICS
635+
| c::FILE_DISPOSITION_FLAG_IGNORE_READONLY_ATTRIBUTE,
624636
};
625637
let size = mem::size_of_val(&info);
626638
cvt(unsafe {
@@ -791,23 +803,23 @@ fn open_link_no_reparse(parent: &File, name: &[u16], access: u32) -> io::Result<
791803
// See https://docs.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntcreatefile
792804
unsafe {
793805
let mut handle = ptr::null_mut();
794-
let mut io_status = c::IO_STATUS_BLOCK::default();
795-
let name_str = c::UNICODE_STRING::from_ref(name);
806+
let mut io_status = c::IO_STATUS_BLOCK::PENDING;
807+
let mut name_str = c::UNICODE_STRING::from_ref(name);
796808
use crate::sync::atomic::{AtomicU32, Ordering};
797809
// The `OBJ_DONT_REPARSE` attribute ensures that we haven't been
798810
// tricked into following a symlink. However, it may not be available in
799811
// earlier versions of Windows.
800812
static ATTRIBUTES: AtomicU32 = AtomicU32::new(c::OBJ_DONT_REPARSE);
801-
let object = c::OBJECT_ATTRIBUTES {
802-
ObjectName: &name_str,
813+
let mut object = c::OBJECT_ATTRIBUTES {
814+
ObjectName: &mut name_str,
803815
RootDirectory: parent.as_raw_handle(),
804816
Attributes: ATTRIBUTES.load(Ordering::Relaxed),
805817
..c::OBJECT_ATTRIBUTES::default()
806818
};
807819
let status = c::NtCreateFile(
808820
&mut handle,
809821
access,
810-
&object,
822+
&mut object,
811823
&mut io_status,
812824
crate::ptr::null_mut(),
813825
0,
@@ -1368,7 +1380,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
13681380
_dwCallbackReason: c::DWORD,
13691381
_hSourceFile: c::HANDLE,
13701382
_hDestinationFile: c::HANDLE,
1371-
lpData: c::LPVOID,
1383+
lpData: c::LPCVOID,
13721384
) -> c::DWORD {
13731385
if dwStreamNumber == 1 {
13741386
*(lpData as *mut i64) = StreamBytesTransferred;
@@ -1415,9 +1427,10 @@ fn symlink_junction_inner(original: &Path, junction: &Path) -> io::Result<()> {
14151427
let f = File::open(junction, &opts)?;
14161428
let h = f.as_inner().as_raw_handle();
14171429
unsafe {
1418-
let mut data = Align8([MaybeUninit::<u8>::uninit(); c::MAXIMUM_REPARSE_DATA_BUFFER_SIZE]);
1430+
let mut data =
1431+
Align8([MaybeUninit::<u8>::uninit(); c::MAXIMUM_REPARSE_DATA_BUFFER_SIZE as usize]);
14191432
let data_ptr = data.0.as_mut_ptr();
1420-
let data_end = data_ptr.add(c::MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
1433+
let data_end = data_ptr.add(c::MAXIMUM_REPARSE_DATA_BUFFER_SIZE as usize);
14211434
let db = data_ptr.cast::<c::REPARSE_MOUNTPOINT_DATA_BUFFER>();
14221435
// Zero the header to ensure it's fully initialized, including reserved parameters.
14231436
*db = mem::zeroed();

library/std/src/sys/windows/handle.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl Handle {
144144
let len = cmp::min(buf.len(), <c::DWORD>::MAX as usize) as c::DWORD;
145145
let mut amt = 0;
146146
let res = cvt(c::ReadFile(
147-
self.as_handle(),
147+
self.as_raw_handle(),
148148
buf.as_ptr() as c::LPVOID,
149149
len,
150150
&mut amt,
@@ -235,7 +235,7 @@ impl Handle {
235235
len: usize,
236236
offset: Option<u64>,
237237
) -> io::Result<usize> {
238-
let mut io_status = c::IO_STATUS_BLOCK::default();
238+
let mut io_status = c::IO_STATUS_BLOCK::PENDING;
239239

240240
// The length is clamped at u32::MAX.
241241
let len = cmp::min(len, c::DWORD::MAX as usize) as c::DWORD;
@@ -283,7 +283,7 @@ impl Handle {
283283
///
284284
/// If `offset` is `None` then the current file position is used.
285285
fn synchronous_write(&self, buf: &[u8], offset: Option<u64>) -> io::Result<usize> {
286-
let mut io_status = c::IO_STATUS_BLOCK::default();
286+
let mut io_status = c::IO_STATUS_BLOCK::PENDING;
287287

288288
// The length is clamped at u32::MAX.
289289
let len = cmp::min(buf.len(), c::DWORD::MAX as usize) as c::DWORD;

library/std/src/sys/windows/io.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ impl<'a> IoSlice<'a> {
1717
pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
1818
assert!(buf.len() <= c::ULONG::MAX as usize);
1919
IoSlice {
20-
vec: c::WSABUF {
21-
len: buf.len() as c::ULONG,
22-
buf: buf.as_ptr() as *mut u8 as *mut c::CHAR,
23-
},
20+
vec: c::WSABUF { len: buf.len() as c::ULONG, buf: buf.as_ptr() as *mut u8 },
2421
_p: PhantomData,
2522
}
2623
}
@@ -54,7 +51,7 @@ impl<'a> IoSliceMut<'a> {
5451
pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
5552
assert!(buf.len() <= c::ULONG::MAX as usize);
5653
IoSliceMut {
57-
vec: c::WSABUF { len: buf.len() as c::ULONG, buf: buf.as_mut_ptr() as *mut c::CHAR },
54+
vec: c::WSABUF { len: buf.len() as c::ULONG, buf: buf.as_mut_ptr() },
5855
_p: PhantomData,
5956
}
6057
}

library/std/src/sys/windows/net.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl Socket {
263263
&mut nread,
264264
&mut flags,
265265
ptr::null_mut(),
266-
ptr::null_mut(),
266+
None,
267267
)
268268
};
269269

@@ -347,7 +347,7 @@ impl Socket {
347347
&mut nwritten,
348348
0,
349349
ptr::null_mut(),
350-
ptr::null_mut(),
350+
None,
351351
)
352352
};
353353
cvt(result).map(|_| nwritten as usize)

library/std/src/sys/windows/pipe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ impl AnonPipe {
373373

374374
// Asynchronous read of the pipe.
375375
// If successful, `callback` will be called once it completes.
376-
let result = io(self.inner.as_handle(), buf, len, &mut overlapped, callback);
376+
let result = io(self.inner.as_handle(), buf, len, &mut overlapped, Some(callback));
377377
if result == c::FALSE {
378378
// We can return here because the call failed.
379379
// After this we must not return until the I/O completes.

library/std/src/sys/windows/process.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ impl Command {
308308
let stderr = stderr.to_handle(c::STD_ERROR_HANDLE, &mut pipes.stderr)?;
309309

310310
let mut si = zeroed_startupinfo();
311-
si.cb = mem::size_of::<c::STARTUPINFO>() as c::DWORD;
311+
si.cb = mem::size_of::<c::STARTUPINFOW>() as c::DWORD;
312312

313313
// If at least one of stdin, stdout or stderr are set (i.e. are non null)
314314
// then set the `hStd` fields in `STARTUPINFO`.
@@ -332,7 +332,7 @@ impl Command {
332332
flags,
333333
envp,
334334
dirp,
335-
&mut si,
335+
&si,
336336
&mut pi,
337337
))
338338
}?;
@@ -720,8 +720,8 @@ impl From<u32> for ExitCode {
720720
}
721721
}
722722

723-
fn zeroed_startupinfo() -> c::STARTUPINFO {
724-
c::STARTUPINFO {
723+
fn zeroed_startupinfo() -> c::STARTUPINFOW {
724+
c::STARTUPINFOW {
725725
cb: 0,
726726
lpReserved: ptr::null_mut(),
727727
lpDesktop: ptr::null_mut(),
@@ -731,7 +731,7 @@ fn zeroed_startupinfo() -> c::STARTUPINFO {
731731
dwXSize: 0,
732732
dwYSize: 0,
733733
dwXCountChars: 0,
734-
dwYCountCharts: 0,
734+
dwYCountChars: 0,
735735
dwFillAttribute: 0,
736736
dwFlags: 0,
737737
wShowWindow: 0,

library/std/src/sys/windows/rand.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::ffi::c_void;
12
use crate::io;
23
use crate::mem;
34
use crate::ptr;
@@ -25,8 +26,9 @@ pub fn hashmap_random_keys() -> (u64, u64) {
2526
#[inline(never)]
2627
fn fallback_rng() -> (u64, u64) {
2728
let mut v = (0, 0);
28-
let ret =
29-
unsafe { c::RtlGenRandom(&mut v as *mut _ as *mut u8, mem::size_of_val(&v) as c::ULONG) };
29+
let ret = unsafe {
30+
c::RtlGenRandom(&mut v as *mut _ as *mut c_void, mem::size_of_val(&v) as c::ULONG)
31+
};
3032

3133
if ret != 0 { v } else { panic!("fallback RNG broken: {}", io::Error::last_os_error()) }
3234
}

library/std/src/sys/windows/stack_overflow.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl Handler {
1818
}
1919
}
2020

21-
extern "system" fn vectored_handler(ExceptionInfo: *mut c::EXCEPTION_POINTERS) -> c::LONG {
21+
unsafe extern "system" fn vectored_handler(ExceptionInfo: *mut c::EXCEPTION_POINTERS) -> c::LONG {
2222
unsafe {
2323
let rec = &(*(*ExceptionInfo).ExceptionRecord);
2424
let code = rec.ExceptionCode;
@@ -34,7 +34,7 @@ extern "system" fn vectored_handler(ExceptionInfo: *mut c::EXCEPTION_POINTERS) -
3434
}
3535

3636
pub unsafe fn init() {
37-
if c::AddVectoredExceptionHandler(0, vectored_handler).is_null() {
37+
if c::AddVectoredExceptionHandler(0, Some(vectored_handler)).is_null() {
3838
panic!("failed to install exception handler");
3939
}
4040
// Set the thread stack guarantee for the main thread.

library/std/src/sys/windows/stdio.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ fn write_valid_utf8_to_console(handle: c::HANDLE, utf8: &str) -> io::Result<usiz
180180
let result = c::MultiByteToWideChar(
181181
c::CP_UTF8, // CodePage
182182
c::MB_ERR_INVALID_CHARS, // dwFlags
183-
utf8.as_ptr() as c::LPCCH, // lpMultiByteStr
183+
utf8.as_ptr(), // lpMultiByteStr
184184
utf8.len() as c::c_int, // cbMultiByte
185185
utf16.as_mut_ptr() as c::LPWSTR, // lpWideCharStr
186186
utf16.len() as c::c_int, // cchWideChar
@@ -344,7 +344,7 @@ fn read_u16s(handle: c::HANDLE, buf: &mut [MaybeUninit<u16>]) -> io::Result<usiz
344344
// See #38274 and https://stackoverflow.com/questions/43836040/win-api-readconsole.
345345
const CTRL_Z: u16 = 0x1A;
346346
const CTRL_Z_MASK: c::ULONG = 1 << CTRL_Z;
347-
let mut input_control = c::CONSOLE_READCONSOLE_CONTROL {
347+
let input_control = c::CONSOLE_READCONSOLE_CONTROL {
348348
nLength: crate::mem::size_of::<c::CONSOLE_READCONSOLE_CONTROL>() as c::ULONG,
349349
nInitialChars: 0,
350350
dwCtrlWakeupMask: CTRL_Z_MASK,
@@ -360,7 +360,7 @@ fn read_u16s(handle: c::HANDLE, buf: &mut [MaybeUninit<u16>]) -> io::Result<usiz
360360
buf.as_mut_ptr() as c::LPVOID,
361361
buf.len() as u32,
362362
&mut amount,
363-
&mut input_control as c::PCONSOLE_READCONSOLE_CONTROL,
363+
&input_control,
364364
)
365365
})?;
366366

@@ -385,14 +385,14 @@ fn utf16_to_utf8(utf16: &[u16], utf8: &mut [u8]) -> io::Result<usize> {
385385

386386
let result = unsafe {
387387
c::WideCharToMultiByte(
388-
c::CP_UTF8, // CodePage
389-
c::WC_ERR_INVALID_CHARS, // dwFlags
390-
utf16.as_ptr(), // lpWideCharStr
391-
utf16.len() as c::c_int, // cchWideChar
392-
utf8.as_mut_ptr() as c::LPSTR, // lpMultiByteStr
393-
utf8.len() as c::c_int, // cbMultiByte
394-
ptr::null(), // lpDefaultChar
395-
ptr::null_mut(), // lpUsedDefaultChar
388+
c::CP_UTF8, // CodePage
389+
c::WC_ERR_INVALID_CHARS, // dwFlags
390+
utf16.as_ptr(), // lpWideCharStr
391+
utf16.len() as c::c_int, // cchWideChar
392+
utf8.as_mut_ptr(), // lpMultiByteStr
393+
utf8.len() as c::c_int, // cbMultiByte
394+
ptr::null(), // lpDefaultChar
395+
ptr::null_mut(), // lpUsedDefaultChar
396396
)
397397
};
398398
if result == 0 {

library/std/src/sys/windows/thread.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::ffi::CStr;
22
use crate::io;
33
use crate::num::NonZeroUsize;
44
use crate::os::windows::io::AsRawHandle;
5+
use crate::os::windows::io::HandleOrNull;
56
use crate::ptr;
67
use crate::sys::c;
78
use crate::sys::handle::Handle;
@@ -32,12 +33,12 @@ impl Thread {
3233
let ret = c::CreateThread(
3334
ptr::null_mut(),
3435
stack,
35-
thread_start,
36+
Some(thread_start),
3637
p as *mut _,
3738
c::STACK_SIZE_PARAM_IS_A_RESERVATION,
3839
ptr::null_mut(),
3940
);
40-
41+
let ret = HandleOrNull::from_raw_handle(ret);
4142
return if let Ok(handle) = ret.try_into() {
4243
Ok(Thread { handle: Handle::from_inner(handle) })
4344
} else {

0 commit comments

Comments
 (0)