Skip to content

Commit

Permalink
Auto merge of rust-lang#111401 - ChrisDenton:no-windows-allowed, r=wo…
Browse files Browse the repository at this point in the history
…rkingjubilee

Don't force include Windows goop when documenting

Why do we need to include all the windows bits on non-windows platforms? Let's try not doing that.

Possible alternative to rust-lang#111394, if it works.
  • Loading branch information
bors committed May 10, 2023
2 parents 25444e5 + d076607 commit cba1407
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 59 deletions.
18 changes: 9 additions & 9 deletions library/std/src/os/windows/io/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::io;
use crate::marker::PhantomData;
use crate::mem::forget;
use crate::ptr;
use crate::sys::c;
use crate::sys;
use crate::sys::cvt;
use crate::sys_common::{AsInner, FromInner, IntoInner};

Expand Down Expand Up @@ -190,14 +190,14 @@ impl BorrowedHandle<'_> {
/// object as the existing `BorrowedHandle` instance.
#[stable(feature = "io_safety", since = "1.63.0")]
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedHandle> {
self.duplicate(0, false, c::DUPLICATE_SAME_ACCESS)
self.duplicate(0, false, sys::c::DUPLICATE_SAME_ACCESS)
}

pub(crate) fn duplicate(
&self,
access: c::DWORD,
access: u32,
inherit: bool,
options: c::DWORD,
options: u32,
) -> io::Result<OwnedHandle> {
let handle = self.as_raw_handle();

Expand All @@ -211,14 +211,14 @@ impl BorrowedHandle<'_> {

let mut ret = ptr::null_mut();
cvt(unsafe {
let cur_proc = c::GetCurrentProcess();
c::DuplicateHandle(
let cur_proc = sys::c::GetCurrentProcess();
sys::c::DuplicateHandle(
cur_proc,
handle,
cur_proc,
&mut ret,
access,
inherit as c::BOOL,
inherit as sys::c::BOOL,
options,
)
})?;
Expand All @@ -233,7 +233,7 @@ impl TryFrom<HandleOrInvalid> for OwnedHandle {
#[inline]
fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, InvalidHandleError> {
let owned_handle = handle_or_invalid.0;
if owned_handle.handle == c::INVALID_HANDLE_VALUE {
if owned_handle.handle == sys::c::INVALID_HANDLE_VALUE {
// Don't call `CloseHandle`; it'd be harmless, except that it could
// overwrite the `GetLastError` error.
forget(owned_handle);
Expand Down Expand Up @@ -365,7 +365,7 @@ impl Drop for OwnedHandle {
#[inline]
fn drop(&mut self) {
unsafe {
let _ = c::CloseHandle(self.handle);
let _ = sys::c::CloseHandle(self.handle);
}
}
}
Expand Down
17 changes: 8 additions & 9 deletions library/std/src/os/windows/io/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::os::windows::io::{OwnedHandle, OwnedSocket};
use crate::os::windows::raw;
use crate::ptr;
use crate::sys;
use crate::sys::c;
use crate::sys_common::{self, AsInner, FromInner, IntoInner};

/// Raw HANDLEs.
Expand Down Expand Up @@ -104,42 +103,42 @@ impl AsRawHandle for fs::File {
#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawHandle for io::Stdin {
fn as_raw_handle(&self) -> RawHandle {
stdio_handle(unsafe { c::GetStdHandle(c::STD_INPUT_HANDLE) as RawHandle })
stdio_handle(unsafe { sys::c::GetStdHandle(sys::c::STD_INPUT_HANDLE) as RawHandle })
}
}

#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawHandle for io::Stdout {
fn as_raw_handle(&self) -> RawHandle {
stdio_handle(unsafe { c::GetStdHandle(c::STD_OUTPUT_HANDLE) as RawHandle })
stdio_handle(unsafe { sys::c::GetStdHandle(sys::c::STD_OUTPUT_HANDLE) as RawHandle })
}
}

#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawHandle for io::Stderr {
fn as_raw_handle(&self) -> RawHandle {
stdio_handle(unsafe { c::GetStdHandle(c::STD_ERROR_HANDLE) as RawHandle })
stdio_handle(unsafe { sys::c::GetStdHandle(sys::c::STD_ERROR_HANDLE) as RawHandle })
}
}

#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
impl<'a> AsRawHandle for io::StdinLock<'a> {
fn as_raw_handle(&self) -> RawHandle {
stdio_handle(unsafe { c::GetStdHandle(c::STD_INPUT_HANDLE) as RawHandle })
stdio_handle(unsafe { sys::c::GetStdHandle(sys::c::STD_INPUT_HANDLE) as RawHandle })
}
}

#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
impl<'a> AsRawHandle for io::StdoutLock<'a> {
fn as_raw_handle(&self) -> RawHandle {
stdio_handle(unsafe { c::GetStdHandle(c::STD_OUTPUT_HANDLE) as RawHandle })
stdio_handle(unsafe { sys::c::GetStdHandle(sys::c::STD_OUTPUT_HANDLE) as RawHandle })
}
}

#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
impl<'a> AsRawHandle for io::StderrLock<'a> {
fn as_raw_handle(&self) -> RawHandle {
stdio_handle(unsafe { c::GetStdHandle(c::STD_ERROR_HANDLE) as RawHandle })
stdio_handle(unsafe { sys::c::GetStdHandle(sys::c::STD_ERROR_HANDLE) as RawHandle })
}
}

Expand All @@ -152,14 +151,14 @@ fn stdio_handle(raw: RawHandle) -> RawHandle {
// console. In that case, return null to the user, which is consistent
// with what they'd get in the parent, and which avoids the problem that
// `INVALID_HANDLE_VALUE` aliases the current process handle.
if raw == c::INVALID_HANDLE_VALUE { ptr::null_mut() } else { raw }
if raw == sys::c::INVALID_HANDLE_VALUE { ptr::null_mut() } else { raw }
}

#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawHandle for fs::File {
#[inline]
unsafe fn from_raw_handle(handle: RawHandle) -> fs::File {
let handle = handle as c::HANDLE;
let handle = handle as sys::c::HANDLE;
fs::File::from_inner(sys::fs::File::from_inner(FromInner::from_inner(
OwnedHandle::from_raw_handle(handle),
)))
Expand Down
39 changes: 23 additions & 16 deletions library/std/src/os/windows/io/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::marker::PhantomData;
use crate::mem;
use crate::mem::forget;
use crate::sys;
use crate::sys::c;
#[cfg(not(target_vendor = "uwp"))]
use crate::sys::cvt;

Expand Down Expand Up @@ -76,7 +75,7 @@ impl BorrowedSocket<'_> {
#[rustc_const_stable(feature = "io_safety", since = "1.63.0")]
#[stable(feature = "io_safety", since = "1.63.0")]
pub const unsafe fn borrow_raw(socket: RawSocket) -> Self {
assert!(socket != c::INVALID_SOCKET as RawSocket);
assert!(socket != sys::c::INVALID_SOCKET as RawSocket);
Self { socket, _phantom: PhantomData }
}
}
Expand All @@ -94,7 +93,11 @@ impl OwnedSocket {
#[cfg(not(target_vendor = "uwp"))]
pub(crate) fn set_no_inherit(&self) -> io::Result<()> {
cvt(unsafe {
c::SetHandleInformation(self.as_raw_socket() as c::HANDLE, c::HANDLE_FLAG_INHERIT, 0)
sys::c::SetHandleInformation(
self.as_raw_socket() as sys::c::HANDLE,
sys::c::HANDLE_FLAG_INHERIT,
0,
)
})
.map(drop)
}
Expand All @@ -110,43 +113,47 @@ 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_INFOW>() };
let mut info = unsafe { mem::zeroed::<sys::c::WSAPROTOCOL_INFOW>() };
let result = unsafe {
c::WSADuplicateSocketW(self.as_raw_socket(), c::GetCurrentProcessId(), &mut info)
sys::c::WSADuplicateSocketW(
self.as_raw_socket(),
sys::c::GetCurrentProcessId(),
&mut info,
)
};
sys::net::cvt(result)?;
let socket = unsafe {
c::WSASocketW(
sys::c::WSASocketW(
info.iAddressFamily,
info.iSocketType,
info.iProtocol,
&mut info,
0,
c::WSA_FLAG_OVERLAPPED | c::WSA_FLAG_NO_HANDLE_INHERIT,
sys::c::WSA_FLAG_OVERLAPPED | sys::c::WSA_FLAG_NO_HANDLE_INHERIT,
)
};

if socket != c::INVALID_SOCKET {
if socket != sys::c::INVALID_SOCKET {
unsafe { Ok(OwnedSocket::from_raw_socket(socket)) }
} else {
let error = unsafe { c::WSAGetLastError() };
let error = unsafe { sys::c::WSAGetLastError() };

if error != c::WSAEPROTOTYPE && error != c::WSAEINVAL {
if error != sys::c::WSAEPROTOTYPE && error != sys::c::WSAEINVAL {
return Err(io::Error::from_raw_os_error(error));
}

let socket = unsafe {
c::WSASocketW(
sys::c::WSASocketW(
info.iAddressFamily,
info.iSocketType,
info.iProtocol,
&mut info,
0,
c::WSA_FLAG_OVERLAPPED,
sys::c::WSA_FLAG_OVERLAPPED,
)
};

if socket == c::INVALID_SOCKET {
if socket == sys::c::INVALID_SOCKET {
return Err(last_error());
}

Expand All @@ -161,7 +168,7 @@ impl BorrowedSocket<'_> {

/// Returns the last error from the Windows socket interface.
fn last_error() -> io::Error {
io::Error::from_raw_os_error(unsafe { c::WSAGetLastError() })
io::Error::from_raw_os_error(unsafe { sys::c::WSAGetLastError() })
}

#[stable(feature = "io_safety", since = "1.63.0")]
Expand Down Expand Up @@ -194,7 +201,7 @@ impl IntoRawSocket for OwnedSocket {
impl FromRawSocket for OwnedSocket {
#[inline]
unsafe fn from_raw_socket(socket: RawSocket) -> Self {
debug_assert_ne!(socket, c::INVALID_SOCKET as RawSocket);
debug_assert_ne!(socket, sys::c::INVALID_SOCKET as RawSocket);
Self { socket }
}
}
Expand All @@ -204,7 +211,7 @@ impl Drop for OwnedSocket {
#[inline]
fn drop(&mut self) {
unsafe {
let _ = c::closesocket(self.socket);
let _ = sys::c::closesocket(self.socket);
}
}
}
Expand Down
25 changes: 0 additions & 25 deletions library/std/src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,6 @@ cfg_if::cfg_if! {
}
}

// Import essential modules from platforms used in `std::os` when documenting.
//
// Note that on some platforms those modules don't compile
// (missing things in `libc` which is empty), so they are not included in `std::os` and can be
// omitted here as well.

#[cfg(doc)]
#[cfg(not(any(
all(target_arch = "wasm32", not(target_os = "wasi")),
all(target_vendor = "fortanix", target_env = "sgx")
)))]
cfg_if::cfg_if! {
if #[cfg(not(windows))] {
// On non-Windows platforms (aka linux/osx/etc) pull in a "minimal"
// amount of windows goop which ends up compiling

#[macro_use]
#[path = "windows/compat.rs"]
pub mod compat;

#[path = "windows/c.rs"]
pub mod c;
}
}

cfg_if::cfg_if! {
// Fuchsia components default to full backtrace.
if #[cfg(target_os = "fuchsia")] {
Expand Down

0 comments on commit cba1407

Please sign in to comment.