diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index dc6365ea8c622..aea3d546d088f 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -450,6 +450,19 @@ since = "1.90.0" )] +// Macro used when rendering with `rustdoc` to replace code with `loop {}`. +#[allow(unused_macros)] +macro_rules! no_code { + ($($code:tt)*) => { + #[cfg(not(doc))] + { + $($code)* + } + #[cfg(doc)] + loop {} + } +} + // The Rust prelude // The compiler expects the prelude definition to be defined before its use statement. pub mod prelude; diff --git a/library/std/src/os/darwin/fs.rs b/library/std/src/os/darwin/fs.rs index b41391f19dccf..5349892dbd1c1 100644 --- a/library/std/src/os/darwin/fs.rs +++ b/library/std/src/os/darwin/fs.rs @@ -89,70 +89,114 @@ impl MetadataExt for Metadata { #[allow(deprecated)] #[cfg(any(doc, target_os = "macos", target_os = "ios"))] fn as_raw_stat(&self) -> &super::raw::stat { - unsafe { &*(self.as_inner().as_inner() as *const libc::stat as *const super::raw::stat) } + no_code! { + unsafe { &*(self.as_inner().as_inner() as *const libc::stat as *const super::raw::stat) } + } } fn st_dev(&self) -> u64 { - self.as_inner().as_inner().st_dev as u64 + no_code! { + self.as_inner().as_inner().st_dev as u64 + } } fn st_ino(&self) -> u64 { - self.as_inner().as_inner().st_ino as u64 + no_code! { + self.as_inner().as_inner().st_ino as u64 + } } fn st_mode(&self) -> u32 { - self.as_inner().as_inner().st_mode as u32 + no_code! { + self.as_inner().as_inner().st_mode as u32 + } } fn st_nlink(&self) -> u64 { - self.as_inner().as_inner().st_nlink as u64 + no_code! { + self.as_inner().as_inner().st_nlink as u64 + } } fn st_uid(&self) -> u32 { - self.as_inner().as_inner().st_uid as u32 + no_code! { + self.as_inner().as_inner().st_uid as u32 + } } fn st_gid(&self) -> u32 { - self.as_inner().as_inner().st_gid as u32 + no_code! { + self.as_inner().as_inner().st_gid as u32 + } } fn st_rdev(&self) -> u64 { - self.as_inner().as_inner().st_rdev as u64 + no_code! { + self.as_inner().as_inner().st_rdev as u64 + } } fn st_size(&self) -> u64 { - self.as_inner().as_inner().st_size as u64 + no_code! { + self.as_inner().as_inner().st_size as u64 + } } fn st_atime(&self) -> i64 { - self.as_inner().as_inner().st_atime as i64 + no_code! { + self.as_inner().as_inner().st_atime as i64 + } } fn st_atime_nsec(&self) -> i64 { - self.as_inner().as_inner().st_atime_nsec as i64 + no_code! { + self.as_inner().as_inner().st_atime_nsec as i64 + } } fn st_mtime(&self) -> i64 { - self.as_inner().as_inner().st_mtime as i64 + no_code! { + self.as_inner().as_inner().st_mtime as i64 + } } fn st_mtime_nsec(&self) -> i64 { - self.as_inner().as_inner().st_mtime_nsec as i64 + no_code! { + self.as_inner().as_inner().st_mtime_nsec as i64 + } } fn st_ctime(&self) -> i64 { - self.as_inner().as_inner().st_ctime as i64 + no_code! { + self.as_inner().as_inner().st_ctime as i64 + } } fn st_ctime_nsec(&self) -> i64 { - self.as_inner().as_inner().st_ctime_nsec as i64 + no_code! { + self.as_inner().as_inner().st_ctime_nsec as i64 + } } fn st_birthtime(&self) -> i64 { - self.as_inner().as_inner().st_birthtime as i64 + no_code! { + self.as_inner().as_inner().st_birthtime as i64 + } } fn st_birthtime_nsec(&self) -> i64 { - self.as_inner().as_inner().st_birthtime_nsec as i64 + no_code! { + self.as_inner().as_inner().st_birthtime_nsec as i64 + } } fn st_blksize(&self) -> u64 { - self.as_inner().as_inner().st_blksize as u64 + no_code! { + self.as_inner().as_inner().st_blksize as u64 + } } fn st_blocks(&self) -> u64 { - self.as_inner().as_inner().st_blocks as u64 + no_code! { + self.as_inner().as_inner().st_blocks as u64 + } } fn st_gen(&self) -> u32 { - self.as_inner().as_inner().st_gen as u32 + no_code! { + self.as_inner().as_inner().st_gen as u32 + } } fn st_flags(&self) -> u32 { - self.as_inner().as_inner().st_flags as u32 + no_code! { + self.as_inner().as_inner().st_flags as u32 + } } fn st_lspare(&self) -> u32 { - self.as_inner().as_inner().st_lspare as u32 + no_code! { + self.as_inner().as_inner().st_lspare as u32 + } } #[cfg(target_os = "macos")] fn st_qspare(&self) -> [u64; 2] { @@ -172,7 +216,9 @@ pub impl(self) trait FileTimesExt { #[stable(feature = "file_set_times", since = "1.75.0")] impl FileTimesExt for fs::FileTimes { fn set_created(mut self, t: SystemTime) -> Self { - self.as_inner_mut().set_created(t.into_inner()); - self + no_code! { + self.as_inner_mut().set_created(t.into_inner()); + self + } } } diff --git a/library/std/src/os/linux/process.rs b/library/std/src/os/linux/process.rs index e0ddaa85cb82e..26f2057b6d942 100644 --- a/library/std/src/os/linux/process.rs +++ b/library/std/src/os/linux/process.rs @@ -62,7 +62,9 @@ impl PidFd { /// /// [`Child::kill`]: process::Child::kill pub fn kill(&self) -> Result<()> { - self.inner.kill() + no_code! { + self.inner.kill() + } } /// Waits for the child to exit completely, returning the status that it exited with. @@ -75,7 +77,9 @@ impl PidFd { /// /// [`Child::wait`]: process::Child::wait pub fn wait(&self) -> Result { - self.inner.wait().map(FromInner::from_inner) + no_code! { + self.inner.wait().map(FromInner::from_inner) + } } /// Attempts to collect the exit status of the child if it has already exited. @@ -85,7 +89,9 @@ impl PidFd { /// /// [`Child::try_wait`]: process::Child::try_wait pub fn try_wait(&self) -> Result> { - Ok(self.inner.try_wait()?.map(FromInner::from_inner)) + no_code! { + Ok(self.inner.try_wait()?.map(FromInner::from_inner)) + } } } @@ -111,37 +117,49 @@ impl IntoInner for PidFd { impl AsRawFd for PidFd { #[inline] fn as_raw_fd(&self) -> RawFd { - self.as_inner().as_inner().as_raw_fd() + no_code! { + self.as_inner().as_inner().as_raw_fd() + } } } impl FromRawFd for PidFd { unsafe fn from_raw_fd(fd: RawFd) -> Self { - Self::from_inner(InnerPidFd::from_raw_fd(fd)) + no_code! { + Self::from_inner(InnerPidFd::from_raw_fd(fd)) + } } } impl IntoRawFd for PidFd { fn into_raw_fd(self) -> RawFd { - self.into_inner().into_inner().into_raw_fd() + no_code! { + self.into_inner().into_inner().into_raw_fd() + } } } impl AsFd for PidFd { fn as_fd(&self) -> BorrowedFd<'_> { - self.as_inner().as_inner().as_fd() + no_code! { + self.as_inner().as_inner().as_fd() + } } } impl From for PidFd { fn from(fd: OwnedFd) -> Self { - Self::from_inner(InnerPidFd::from_inner(FileDesc::from_inner(fd))) + no_code! { + Self::from_inner(InnerPidFd::from_inner(FileDesc::from_inner(fd))) + } } } impl From for OwnedFd { fn from(pid_fd: PidFd) -> Self { - pid_fd.into_inner().into_inner().into_inner() + no_code! { + pid_fd.into_inner().into_inner().into_inner() + } } } diff --git a/library/std/src/os/wasi/fs.rs b/library/std/src/os/wasi/fs.rs index 369e5d4b0fb9d..96a18ff934ff0 100644 --- a/library/std/src/os/wasi/fs.rs +++ b/library/std/src/os/wasi/fs.rs @@ -334,13 +334,19 @@ pub trait MetadataExt { impl MetadataExt for fs::Metadata { fn dev(&self) -> u64 { - self.as_inner().as_inner().st_dev + no_code! { + self.as_inner().as_inner().st_dev + } } fn ino(&self) -> u64 { - self.as_inner().as_inner().st_ino + no_code! { + self.as_inner().as_inner().st_ino + } } fn nlink(&self) -> u64 { - self.as_inner().as_inner().st_nlink + no_code! { + self.as_inner().as_inner().st_nlink + } } } @@ -359,13 +365,19 @@ pub trait FileTypeExt { impl FileTypeExt for fs::FileType { fn is_block_device(&self) -> bool { - self.as_inner().is(libc::S_IFBLK) + no_code! { + self.as_inner().is(libc::S_IFBLK) + } } fn is_char_device(&self) -> bool { - self.as_inner().is(libc::S_IFCHR) + no_code! { + self.as_inner().is(libc::S_IFCHR) + } } fn is_socket(&self) -> bool { - self.as_inner().is(libc::S_IFSOCK) + no_code! { + self.as_inner().is(libc::S_IFSOCK) + } } } diff --git a/library/std/src/os/windows/ffi.rs b/library/std/src/os/windows/ffi.rs index b27a116f60388..d21dace2346ce 100644 --- a/library/std/src/os/windows/ffi.rs +++ b/library/std/src/os/windows/ffi.rs @@ -89,7 +89,9 @@ pub impl(self) trait OsStringExt { #[stable(feature = "rust1", since = "1.0.0")] impl OsStringExt for OsString { fn from_wide(wide: &[u16]) -> OsString { - FromInner::from_inner(Buf { inner: Wtf8Buf::from_wide(wide) }) + no_code! { + FromInner::from_inner(Buf { inner: Wtf8Buf::from_wide(wide) }) + } } } @@ -126,7 +128,9 @@ pub impl(self) trait OsStrExt { impl OsStrExt for OsStr { #[inline] fn encode_wide(&self) -> EncodeWide<'_> { - EncodeWide { inner: self.as_inner().inner.encode_wide() } + no_code! { + EncodeWide { inner: self.as_inner().inner.encode_wide() } + } } } diff --git a/library/std/src/os/windows/fs.rs b/library/std/src/os/windows/fs.rs index c10766470e873..766ae0aeac3c3 100644 --- a/library/std/src/os/windows/fs.rs +++ b/library/std/src/os/windows/fs.rs @@ -501,28 +501,38 @@ pub trait OpenOptionsExt { #[stable(feature = "open_options_ext", since = "1.10.0")] impl OpenOptionsExt for OpenOptions { fn access_mode(&mut self, access: u32) -> &mut OpenOptions { - self.as_inner_mut().access_mode(access); - self + no_code! { + self.as_inner_mut().access_mode(access); + self + } } fn share_mode(&mut self, share: u32) -> &mut OpenOptions { - self.as_inner_mut().share_mode(share); - self + no_code! { + self.as_inner_mut().share_mode(share); + self + } } fn custom_flags(&mut self, flags: u32) -> &mut OpenOptions { - self.as_inner_mut().custom_flags(flags); - self + no_code! { + self.as_inner_mut().custom_flags(flags); + self + } } fn attributes(&mut self, attributes: u32) -> &mut OpenOptions { - self.as_inner_mut().attributes(attributes); - self + no_code! { + self.as_inner_mut().attributes(attributes); + self + } } fn security_qos_flags(&mut self, flags: u32) -> &mut OpenOptions { - self.as_inner_mut().security_qos_flags(flags); - self + no_code! { + self.as_inner_mut().security_qos_flags(flags); + self + } } } @@ -544,13 +554,17 @@ pub impl(self) trait OpenOptionsExt2 { #[unstable(feature = "windows_freeze_file_times", issue = "149715")] impl OpenOptionsExt2 for OpenOptions { fn freeze_last_access_time(&mut self, freeze: bool) -> &mut Self { - self.as_inner_mut().freeze_last_access_time(freeze); - self + no_code! { + self.as_inner_mut().freeze_last_access_time(freeze); + self + } } fn freeze_last_write_time(&mut self, freeze: bool) -> &mut Self { - self.as_inner_mut().freeze_last_write_time(freeze); - self + no_code! { + self.as_inner_mut().freeze_last_write_time(freeze); + self + } } } @@ -602,7 +616,9 @@ pub impl(self) trait PermissionsExt { #[unstable(feature = "windows_permissions_ext", issue = "152956")] impl PermissionsExt for fs::Permissions { fn file_attributes(&self) -> u32 { - self.as_inner().file_attributes() + no_code! { + self.as_inner().file_attributes() + } } fn set_file_attributes(&mut self, mask: u32) { @@ -813,31 +829,49 @@ pub trait MetadataExt { #[stable(feature = "metadata_ext", since = "1.1.0")] impl MetadataExt for Metadata { fn file_attributes(&self) -> u32 { - self.as_inner().attrs() + no_code! { + self.as_inner().attrs() + } } fn creation_time(&self) -> u64 { - self.as_inner().created_u64() + no_code! { + self.as_inner().created_u64() + } } fn last_access_time(&self) -> u64 { - self.as_inner().accessed_u64() + no_code! { + self.as_inner().accessed_u64() + } } fn last_write_time(&self) -> u64 { - self.as_inner().modified_u64() + no_code! { + self.as_inner().modified_u64() + } } fn file_size(&self) -> u64 { - self.as_inner().size() + no_code! { + self.as_inner().size() + } } fn volume_serial_number(&self) -> Option { - self.as_inner().volume_serial_number() + no_code! { + self.as_inner().volume_serial_number() + } } fn number_of_links(&self) -> Option { - self.as_inner().number_of_links() + no_code! { + self.as_inner().number_of_links() + } } fn file_index(&self) -> Option { - self.as_inner().file_index() + no_code! { + self.as_inner().file_index() + } } fn change_time(&self) -> Option { - self.as_inner().changed_u64() + no_code! { + self.as_inner().changed_u64() + } } } @@ -857,10 +891,14 @@ pub impl(self) trait FileTypeExt { #[stable(feature = "windows_file_type_ext", since = "1.64.0")] impl FileTypeExt for fs::FileType { fn is_symlink_dir(&self) -> bool { - self.as_inner().is_symlink_dir() + no_code! { + self.as_inner().is_symlink_dir() + } } fn is_symlink_file(&self) -> bool { - self.as_inner().is_symlink_file() + no_code! { + self.as_inner().is_symlink_file() + } } } @@ -875,8 +913,10 @@ pub impl(self) trait FileTimesExt { #[stable(feature = "file_set_times", since = "1.75.0")] impl FileTimesExt for fs::FileTimes { fn set_created(mut self, t: SystemTime) -> Self { - self.as_inner_mut().set_created(t.into_inner()); - self + no_code! { + self.as_inner_mut().set_created(t.into_inner()); + self + } } } @@ -918,7 +958,9 @@ impl FileTimesExt for fs::FileTimes { #[stable(feature = "symlink", since = "1.1.0")] #[cfg_attr(not(test), rustc_diagnostic_item = "fs_symlink_file")] pub fn symlink_file, Q: AsRef>(original: P, link: Q) -> io::Result<()> { - sys::fs::symlink_inner(original.as_ref(), link.as_ref(), false) + no_code! { + sys::fs::symlink_inner(original.as_ref(), link.as_ref(), false) + } } /// Creates a new symlink to a directory on the filesystem. @@ -959,7 +1001,9 @@ pub fn symlink_file, Q: AsRef>(original: P, link: Q) -> io: #[stable(feature = "symlink", since = "1.1.0")] #[cfg_attr(not(test), rustc_diagnostic_item = "fs_symlink_dir")] pub fn symlink_dir, Q: AsRef>(original: P, link: Q) -> io::Result<()> { - sys::fs::symlink_inner(original.as_ref(), link.as_ref(), true) + no_code! { + sys::fs::symlink_inner(original.as_ref(), link.as_ref(), true) + } } /// Creates a junction point. @@ -971,5 +1015,7 @@ pub fn symlink_dir, Q: AsRef>(original: P, link: Q) -> io:: /// If either path is not a local file path then this will fail. #[unstable(feature = "junction_point", issue = "121709")] pub fn junction_point, Q: AsRef>(original: P, link: Q) -> io::Result<()> { - sys::fs::junction_point(original.as_ref(), link.as_ref()) + no_code! { + sys::fs::junction_point(original.as_ref(), link.as_ref()) + } } diff --git a/library/std/src/os/windows/io/handle.rs b/library/std/src/os/windows/io/handle.rs index 01bd36155b110..b901c5e8bec19 100644 --- a/library/std/src/os/windows/io/handle.rs +++ b/library/std/src/os/windows/io/handle.rs @@ -173,9 +173,11 @@ impl TryFrom for OwnedHandle { impl Drop for HandleOrNull { #[inline] fn drop(&mut self) { - if self.is_valid() { - unsafe { - let _ = sys::c::CloseHandle(self.0); + no_code! { + if self.is_valid() { + unsafe { + let _ = sys::c::CloseHandle(self.0); + } } } } @@ -195,7 +197,9 @@ impl BorrowedHandle<'_> { /// object as the existing `BorrowedHandle` instance. #[stable(feature = "io_safety", since = "1.63.0")] pub fn try_clone_to_owned(&self) -> io::Result { - self.duplicate(0, false, sys::c::DUPLICATE_SAME_ACCESS) + no_code! { + self.duplicate(0, false, sys::c::DUPLICATE_SAME_ACCESS) + } } pub(crate) fn duplicate( @@ -204,30 +208,32 @@ impl BorrowedHandle<'_> { inherit: bool, options: u32, ) -> io::Result { - let handle = self.as_raw_handle(); - - // `Stdin`, `Stdout`, and `Stderr` can all hold null handles, such as - // in a process with a detached console. `DuplicateHandle` would fail - // if we passed it a null handle, but we can treat null as a valid - // handle which doesn't do any I/O, and allow it to be duplicated. - if handle.is_null() { - return unsafe { Ok(OwnedHandle::from_raw_handle(handle)) }; - } + no_code! { + let handle = self.as_raw_handle(); + + // `Stdin`, `Stdout`, and `Stderr` can all hold null handles, such as + // in a process with a detached console. `DuplicateHandle` would fail + // if we passed it a null handle, but we can treat null as a valid + // handle which doesn't do any I/O, and allow it to be duplicated. + if handle.is_null() { + return unsafe { Ok(OwnedHandle::from_raw_handle(handle)) }; + } - let mut ret = ptr::null_mut(); - cvt(unsafe { - let cur_proc = sys::c::GetCurrentProcess(); - sys::c::DuplicateHandle( - cur_proc, - handle, - cur_proc, - &mut ret, - access, - inherit as sys::c::BOOL, - options, - ) - })?; - unsafe { Ok(OwnedHandle::from_raw_handle(ret)) } + let mut ret = ptr::null_mut(); + cvt(unsafe { + let cur_proc = sys::c::GetCurrentProcess(); + sys::c::DuplicateHandle( + cur_proc, + handle, + cur_proc, + &mut ret, + access, + inherit as sys::c::BOOL, + options, + ) + })?; + unsafe { Ok(OwnedHandle::from_raw_handle(ret)) } + } } } @@ -251,9 +257,11 @@ impl TryFrom for OwnedHandle { impl Drop for HandleOrInvalid { #[inline] fn drop(&mut self) { - if self.is_valid() { - unsafe { - let _ = sys::c::CloseHandle(self.0); + no_code! { + if self.is_valid() { + unsafe { + let _ = sys::c::CloseHandle(self.0); + } } } } @@ -471,7 +479,9 @@ impl AsHandle for &mut T { impl AsHandle for crate::sync::Arc { #[inline] fn as_handle(&self) -> BorrowedHandle<'_> { - (**self).as_handle() + no_code! { + (**self).as_handle() + } } } @@ -479,7 +489,9 @@ impl AsHandle for crate::sync::Arc { impl AsHandle for crate::rc::Rc { #[inline] fn as_handle(&self) -> BorrowedHandle<'_> { - (**self).as_handle() + no_code! { + (**self).as_handle() + } } } @@ -487,7 +499,9 @@ impl AsHandle for crate::rc::Rc { impl AsHandle for crate::rc::UniqueRc { #[inline] fn as_handle(&self) -> BorrowedHandle<'_> { - (**self).as_handle() + no_code! { + (**self).as_handle() + } } } @@ -495,7 +509,9 @@ impl AsHandle for crate::rc::UniqueRc { impl AsHandle for Box { #[inline] fn as_handle(&self) -> BorrowedHandle<'_> { - (**self).as_handle() + no_code! { + (**self).as_handle() + } } } @@ -522,7 +538,9 @@ impl AsHandle for OwnedHandle { impl AsHandle for fs::File { #[inline] fn as_handle(&self) -> BorrowedHandle<'_> { - self.as_inner().as_handle() + no_code! { + self.as_inner().as_handle() + } } } @@ -531,7 +549,9 @@ impl From for OwnedHandle { /// Takes ownership of a [`File`](fs::File)'s underlying file handle. #[inline] fn from(file: fs::File) -> OwnedHandle { - file.into_inner().into_inner().into_inner() + no_code! { + file.into_inner().into_inner().into_inner() + } } } @@ -540,7 +560,9 @@ impl From for fs::File { /// Returns a [`File`](fs::File) that takes ownership of the given handle. #[inline] fn from(owned: OwnedHandle) -> Self { - Self::from_inner(FromInner::from_inner(FromInner::from_inner(owned))) + no_code! { + Self::from_inner(FromInner::from_inner(FromInner::from_inner(owned))) + } } } @@ -655,48 +677,62 @@ impl AsHandle for crate::thread::JoinHandle { impl From> for OwnedHandle { #[inline] fn from(join_handle: crate::thread::JoinHandle) -> OwnedHandle { - join_handle.into_inner().into_handle().into_inner() + no_code! { + join_handle.into_inner().into_handle().into_inner() + } } } #[stable(feature = "anonymous_pipe", since = "1.87.0")] impl AsHandle for io::PipeReader { fn as_handle(&self) -> BorrowedHandle<'_> { - self.0.as_handle() + no_code! { + self.0.as_handle() + } } } #[stable(feature = "anonymous_pipe", since = "1.87.0")] impl From for OwnedHandle { fn from(pipe: io::PipeReader) -> Self { - pipe.into_inner().into_inner() + no_code! { + pipe.into_inner().into_inner() + } } } #[stable(feature = "anonymous_pipe", since = "1.87.0")] impl AsHandle for io::PipeWriter { fn as_handle(&self) -> BorrowedHandle<'_> { - self.0.as_handle() + no_code! { + self.0.as_handle() + } } } #[stable(feature = "anonymous_pipe", since = "1.87.0")] impl From for OwnedHandle { fn from(pipe: io::PipeWriter) -> Self { - pipe.into_inner().into_inner() + no_code! { + pipe.into_inner().into_inner() + } } } #[stable(feature = "anonymous_pipe", since = "1.87.0")] impl From for io::PipeReader { fn from(owned_handle: OwnedHandle) -> Self { - Self::from_inner(FromInner::from_inner(owned_handle)) + no_code! { + Self::from_inner(FromInner::from_inner(owned_handle)) + } } } #[stable(feature = "anonymous_pipe", since = "1.87.0")] impl From for io::PipeWriter { fn from(owned_handle: OwnedHandle) -> Self { - Self::from_inner(FromInner::from_inner(owned_handle)) + no_code! { + Self::from_inner(FromInner::from_inner(owned_handle)) + } } } diff --git a/library/std/src/os/windows/io/raw.rs b/library/std/src/os/windows/io/raw.rs index d050c28832517..bd1601fc5c5fd 100644 --- a/library/std/src/os/windows/io/raw.rs +++ b/library/std/src/os/windows/io/raw.rs @@ -94,7 +94,9 @@ pub trait IntoRawHandle { impl AsRawHandle for fs::File { #[inline] fn as_raw_handle(&self) -> RawHandle { - self.as_inner().as_raw_handle() as RawHandle + no_code! { + self.as_inner().as_raw_handle() as RawHandle + } } } @@ -156,11 +158,13 @@ pub(super) fn stdio_handle(raw: RawHandle) -> RawHandle { impl FromRawHandle for fs::File { #[inline] unsafe fn from_raw_handle(handle: RawHandle) -> fs::File { - unsafe { - let handle = handle as sys::c::HANDLE; - fs::File::from_inner(sys::fs::File::from_inner(FromInner::from_inner( - OwnedHandle::from_raw_handle(handle), - ))) + no_code! { + unsafe { + let handle = handle as sys::c::HANDLE; + fs::File::from_inner(sys::fs::File::from_inner(FromInner::from_inner( + OwnedHandle::from_raw_handle(handle), + ))) + } } } } @@ -169,7 +173,9 @@ impl FromRawHandle for fs::File { impl IntoRawHandle for fs::File { #[inline] fn into_raw_handle(self) -> RawHandle { - self.into_inner().into_raw_handle() as *mut _ + no_code! { + self.into_inner().into_raw_handle() as *mut _ + } } } @@ -238,21 +244,27 @@ pub trait IntoRawSocket { impl AsRawSocket for net::TcpStream { #[inline] fn as_raw_socket(&self) -> RawSocket { - self.as_inner().socket().as_raw_socket() + no_code! { + self.as_inner().socket().as_raw_socket() + } } } #[stable(feature = "rust1", since = "1.0.0")] impl AsRawSocket for net::TcpListener { #[inline] fn as_raw_socket(&self) -> RawSocket { - self.as_inner().socket().as_raw_socket() + no_code! { + self.as_inner().socket().as_raw_socket() + } } } #[stable(feature = "rust1", since = "1.0.0")] impl AsRawSocket for net::UdpSocket { #[inline] fn as_raw_socket(&self) -> RawSocket { - self.as_inner().socket().as_raw_socket() + no_code! { + self.as_inner().socket().as_raw_socket() + } } } @@ -260,9 +272,11 @@ impl AsRawSocket for net::UdpSocket { impl FromRawSocket for net::TcpStream { #[inline] unsafe fn from_raw_socket(sock: RawSocket) -> net::TcpStream { - unsafe { - let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock)); - net::TcpStream::from_inner(sys::net::TcpStream::from_inner(sock)) + no_code! { + unsafe { + let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock)); + net::TcpStream::from_inner(sys::net::TcpStream::from_inner(sock)) + } } } } @@ -270,9 +284,11 @@ impl FromRawSocket for net::TcpStream { impl FromRawSocket for net::TcpListener { #[inline] unsafe fn from_raw_socket(sock: RawSocket) -> net::TcpListener { - unsafe { - let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock)); - net::TcpListener::from_inner(sys::net::TcpListener::from_inner(sock)) + no_code! { + unsafe { + let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock)); + net::TcpListener::from_inner(sys::net::TcpListener::from_inner(sock)) + } } } } @@ -280,9 +296,11 @@ impl FromRawSocket for net::TcpListener { impl FromRawSocket for net::UdpSocket { #[inline] unsafe fn from_raw_socket(sock: RawSocket) -> net::UdpSocket { - unsafe { - let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock)); - net::UdpSocket::from_inner(sys::net::UdpSocket::from_inner(sock)) + no_code! { + unsafe { + let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock)); + net::UdpSocket::from_inner(sys::net::UdpSocket::from_inner(sock)) + } } } } @@ -291,7 +309,9 @@ impl FromRawSocket for net::UdpSocket { impl IntoRawSocket for net::TcpStream { #[inline] fn into_raw_socket(self) -> RawSocket { - self.into_inner().into_socket().into_inner().into_raw_socket() + no_code! { + self.into_inner().into_socket().into_inner().into_raw_socket() + } } } @@ -299,7 +319,9 @@ impl IntoRawSocket for net::TcpStream { impl IntoRawSocket for net::TcpListener { #[inline] fn into_raw_socket(self) -> RawSocket { - self.into_inner().into_socket().into_inner().into_raw_socket() + no_code! { + self.into_inner().into_socket().into_inner().into_raw_socket() + } } } @@ -307,48 +329,62 @@ impl IntoRawSocket for net::TcpListener { impl IntoRawSocket for net::UdpSocket { #[inline] fn into_raw_socket(self) -> RawSocket { - self.into_inner().into_socket().into_inner().into_raw_socket() + no_code! { + self.into_inner().into_socket().into_inner().into_raw_socket() + } } } #[stable(feature = "anonymous_pipe", since = "1.87.0")] impl AsRawHandle for io::PipeReader { fn as_raw_handle(&self) -> RawHandle { - self.0.as_raw_handle() + no_code! { + self.0.as_raw_handle() + } } } #[stable(feature = "anonymous_pipe", since = "1.87.0")] impl FromRawHandle for io::PipeReader { unsafe fn from_raw_handle(raw_handle: RawHandle) -> Self { - unsafe { Self::from_inner(FromRawHandle::from_raw_handle(raw_handle)) } + no_code! { + unsafe { Self::from_inner(FromRawHandle::from_raw_handle(raw_handle)) } + } } } #[stable(feature = "anonymous_pipe", since = "1.87.0")] impl IntoRawHandle for io::PipeReader { fn into_raw_handle(self) -> RawHandle { - self.0.into_raw_handle() + no_code! { + self.0.into_raw_handle() + } } } #[stable(feature = "anonymous_pipe", since = "1.87.0")] impl AsRawHandle for io::PipeWriter { fn as_raw_handle(&self) -> RawHandle { - self.0.as_raw_handle() + no_code! { + self.0.as_raw_handle() + } } } #[stable(feature = "anonymous_pipe", since = "1.87.0")] impl FromRawHandle for io::PipeWriter { unsafe fn from_raw_handle(raw_handle: RawHandle) -> Self { - unsafe { Self::from_inner(FromRawHandle::from_raw_handle(raw_handle)) } + no_code! { + unsafe { Self::from_inner(FromRawHandle::from_raw_handle(raw_handle)) } + } } } #[stable(feature = "anonymous_pipe", since = "1.87.0")] impl IntoRawHandle for io::PipeWriter { fn into_raw_handle(self) -> RawHandle { - self.0.into_raw_handle() + no_code! { + self.0.into_raw_handle() + } } } diff --git a/library/std/src/os/windows/io/socket.rs b/library/std/src/os/windows/io/socket.rs index d625a9ac52730..dd4476bac4bd0 100644 --- a/library/std/src/os/windows/io/socket.rs +++ b/library/std/src/os/windows/io/socket.rs @@ -79,14 +79,16 @@ impl OwnedSocket { #[allow(implicit_provenance_casts)] #[cfg(not(target_vendor = "uwp"))] pub(crate) fn set_no_inherit(&self) -> io::Result<()> { - cvt(unsafe { - sys::c::SetHandleInformation( - self.as_raw_socket() as sys::c::HANDLE, - sys::c::HANDLE_FLAG_INHERIT, - 0, - ) - }) - .map(drop) + no_code! { + cvt(unsafe { + sys::c::SetHandleInformation( + self.as_raw_socket() as sys::c::HANDLE, + sys::c::HANDLE_FLAG_INHERIT, + 0, + ) + }) + .map(drop) + } } #[cfg(target_vendor = "uwp")] @@ -100,35 +102,16 @@ 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 { - let mut info = unsafe { mem::zeroed::() }; - let result = unsafe { - sys::c::WSADuplicateSocketW( - self.as_raw_socket() as sys::c::SOCKET, - sys::c::GetCurrentProcessId(), - &mut info, - ) - }; - sys::net::cvt(result)?; - let socket = unsafe { - sys::c::WSASocketW( - info.iAddressFamily, - info.iSocketType, - info.iProtocol, - &info, - 0, - sys::c::WSA_FLAG_OVERLAPPED | sys::c::WSA_FLAG_NO_HANDLE_INHERIT, - ) - }; - - if socket != sys::c::INVALID_SOCKET { - unsafe { Ok(OwnedSocket::from_raw_socket(socket as RawSocket)) } - } else { - let error = unsafe { sys::c::WSAGetLastError() }; - - if error != sys::c::WSAEPROTOTYPE && error != sys::c::WSAEINVAL { - return Err(io::Error::from_raw_os_error(error)); - } - + no_code! { + let mut info = unsafe { mem::zeroed::() }; + let result = unsafe { + sys::c::WSADuplicateSocketW( + self.as_raw_socket() as sys::c::SOCKET, + sys::c::GetCurrentProcessId(), + &mut info, + ) + }; + sys::net::cvt(result)?; let socket = unsafe { sys::c::WSASocketW( info.iAddressFamily, @@ -136,18 +119,39 @@ impl BorrowedSocket<'_> { info.iProtocol, &info, 0, - sys::c::WSA_FLAG_OVERLAPPED, + sys::c::WSA_FLAG_OVERLAPPED | sys::c::WSA_FLAG_NO_HANDLE_INHERIT, ) }; - if socket == sys::c::INVALID_SOCKET { - return Err(last_error()); - } - - unsafe { - let socket = OwnedSocket::from_raw_socket(socket as RawSocket); - socket.set_no_inherit()?; - Ok(socket) + if socket != sys::c::INVALID_SOCKET { + unsafe { Ok(OwnedSocket::from_raw_socket(socket as RawSocket)) } + } else { + let error = unsafe { sys::c::WSAGetLastError() }; + + if error != sys::c::WSAEPROTOTYPE && error != sys::c::WSAEINVAL { + return Err(io::Error::from_raw_os_error(error)); + } + + let socket = unsafe { + sys::c::WSASocketW( + info.iAddressFamily, + info.iSocketType, + info.iProtocol, + &info, + 0, + sys::c::WSA_FLAG_OVERLAPPED, + ) + }; + + if socket == sys::c::INVALID_SOCKET { + return Err(last_error()); + } + + unsafe { + let socket = OwnedSocket::from_raw_socket(socket as RawSocket); + socket.set_no_inherit()?; + Ok(socket) + } } } } @@ -155,7 +159,9 @@ impl BorrowedSocket<'_> { /// Returns the last error from the Windows socket interface. fn last_error() -> io::Error { - io::Error::from_raw_os_error(unsafe { sys::c::WSAGetLastError() }) + no_code! { + io::Error::from_raw_os_error(unsafe { sys::c::WSAGetLastError() }) + } } #[stable(feature = "io_safety", since = "1.63.0")] @@ -195,8 +201,10 @@ impl FromRawSocket for OwnedSocket { impl Drop for OwnedSocket { #[inline] fn drop(&mut self) { - unsafe { - let _ = sys::c::closesocket(self.socket.as_inner() as sys::c::SOCKET); + no_code! { + unsafe { + let _ = sys::c::closesocket(self.socket.as_inner() as sys::c::SOCKET); + } } } } diff --git a/library/std/src/os/windows/net/addr.rs b/library/std/src/os/windows/net/addr.rs index c330432039a8f..36db1ee79045a 100644 --- a/library/std/src/os/windows/net/addr.rs +++ b/library/std/src/os/windows/net/addr.rs @@ -131,20 +131,24 @@ impl SocketAddr { where P: AsRef, { - sockaddr_un(path.as_ref()).map(|(addr, len)| SocketAddr { addr, len: len as _ }) + no_code! { + sockaddr_un(path.as_ref()).map(|(addr, len)| SocketAddr { addr, len: len as _ }) + } } fn address(&self) -> AddressKind<'_> { - let len = self.len as usize - SUN_PATH_OFFSET; - let path = unsafe { mem::transmute::<&[i8], &[u8]>(&self.addr.sun_path) }; + no_code! { + let len = self.len as usize - SUN_PATH_OFFSET; + let path = unsafe { mem::transmute::<&[i8], &[u8]>(&self.addr.sun_path) }; - if len == 0 { - AddressKind::Unnamed - } else if self.addr.sun_path[0] == 0 { - AddressKind::Abstract(ByteStr::from_bytes(&path[1..len])) - } else { - AddressKind::Pathname(unsafe { - OsStr::from_encoded_bytes_unchecked(&path[..len - 1]).as_ref() - }) + if len == 0 { + AddressKind::Unnamed + } else if self.addr.sun_path[0] == 0 { + AddressKind::Abstract(ByteStr::from_bytes(&path[1..len])) + } else { + AddressKind::Pathname(unsafe { + OsStr::from_encoded_bytes_unchecked(&path[..len - 1]).as_ref() + }) + } } } diff --git a/library/std/src/os/windows/net/listener.rs b/library/std/src/os/windows/net/listener.rs index 19f5254e08bf9..5749747b3adee 100644 --- a/library/std/src/os/windows/net/listener.rs +++ b/library/std/src/os/windows/net/listener.rs @@ -164,7 +164,9 @@ impl UnixListener { /// } /// ``` pub fn local_addr(&self) -> io::Result { - SocketAddr::new(|addr, len| unsafe { getsockname(self.0.as_raw(), addr, len) }) + no_code! { + SocketAddr::new(|addr, len| unsafe { getsockname(self.0.as_raw(), addr, len) }) + } } /// Creates a new independently owned handle to the underlying socket. @@ -326,21 +328,27 @@ impl<'a> Iterator for Incoming<'a> { impl AsRawSocket for UnixListener { #[inline] fn as_raw_socket(&self) -> RawSocket { - self.0.as_raw_socket() + no_code! { + self.0.as_raw_socket() + } } } impl FromRawSocket for UnixListener { #[inline] unsafe fn from_raw_socket(sock: RawSocket) -> Self { - UnixListener(unsafe { Socket::from_raw_socket(sock) }) + no_code! { + UnixListener(unsafe { Socket::from_raw_socket(sock) }) + } } } impl IntoRawSocket for UnixListener { #[inline] fn into_raw_socket(self) -> RawSocket { - self.0.into_raw_socket() + no_code! { + self.0.into_raw_socket() + } } } diff --git a/library/std/src/os/windows/net/stream.rs b/library/std/src/os/windows/net/stream.rs index c0f32e75411e9..1df01b8b527d5 100644 --- a/library/std/src/os/windows/net/stream.rs +++ b/library/std/src/os/windows/net/stream.rs @@ -127,7 +127,9 @@ impl UnixStream { /// } /// ``` pub fn local_addr(&self) -> io::Result { - SocketAddr::new(|addr, len| unsafe { getsockname(self.0.as_raw(), addr, len) }) + no_code! { + SocketAddr::new(|addr, len| unsafe { getsockname(self.0.as_raw(), addr, len) }) + } } /// Returns the socket address of the remote half of this connection. @@ -146,7 +148,9 @@ impl UnixStream { /// } /// ``` pub fn peer_addr(&self) -> io::Result { - SocketAddr::new(|addr, len| unsafe { getpeername(self.0.as_raw(), addr, len) }) + no_code! { + SocketAddr::new(|addr, len| unsafe { getpeername(self.0.as_raw(), addr, len) }) + } } /// Returns the read timeout of this socket. @@ -167,7 +171,9 @@ impl UnixStream { /// } /// ``` pub fn read_timeout(&self) -> io::Result> { - self.0.timeout(SO_RCVTIMEO) + no_code! { + self.0.timeout(SO_RCVTIMEO) + } } /// Moves the socket into or out of nonblocking mode. @@ -231,7 +237,9 @@ impl UnixStream { /// } /// ``` pub fn set_read_timeout(&self, dur: Option) -> io::Result<()> { - self.0.set_timeout(dur, SO_RCVTIMEO) + no_code! { + self.0.set_timeout(dur, SO_RCVTIMEO) + } } /// Sets the write timeout for the socket. @@ -277,7 +285,9 @@ impl UnixStream { /// } /// ``` pub fn set_write_timeout(&self, dur: Option) -> io::Result<()> { - self.0.set_timeout(dur, SO_SNDTIMEO) + no_code! { + self.0.set_timeout(dur, SO_SNDTIMEO) + } } /// Shuts down the read, write, or both halves of this connection. @@ -368,7 +378,9 @@ impl UnixStream { /// } /// ``` pub fn write_timeout(&self) -> io::Result> { - self.0.timeout(SO_SNDTIMEO) + no_code! { + self.0.timeout(SO_SNDTIMEO) + } } } @@ -413,26 +425,34 @@ impl<'a> io::Write for &'a UnixStream { impl AsSocket for UnixStream { #[inline] fn as_socket(&self) -> BorrowedSocket<'_> { - self.0.as_socket() + no_code! { + self.0.as_socket() + } } } impl AsRawSocket for UnixStream { #[inline] fn as_raw_socket(&self) -> RawSocket { - self.0.as_raw_socket() + no_code! { + self.0.as_raw_socket() + } } } impl FromRawSocket for UnixStream { #[inline] unsafe fn from_raw_socket(sock: RawSocket) -> Self { - unsafe { UnixStream(Socket::from_raw_socket(sock)) } + no_code! { + unsafe { UnixStream(Socket::from_raw_socket(sock)) } + } } } impl IntoRawSocket for UnixStream { fn into_raw_socket(self) -> RawSocket { - self.0.into_raw_socket() + no_code! { + self.0.into_raw_socket() + } } } diff --git a/library/std/src/os/windows/process.rs b/library/std/src/os/windows/process.rs index 971d88c65ea75..6d3d7143ad635 100644 --- a/library/std/src/os/windows/process.rs +++ b/library/std/src/os/windows/process.rs @@ -15,9 +15,11 @@ use crate::{io, marker, process, ptr, sys}; #[stable(feature = "process_extensions", since = "1.2.0")] impl FromRawHandle for process::Stdio { unsafe fn from_raw_handle(handle: RawHandle) -> process::Stdio { - let handle = unsafe { sys::handle::Handle::from_raw_handle(handle as *mut _) }; - let io = sys::process::Stdio::Handle(handle); - process::Stdio::from_inner(io) + no_code! { + let handle = unsafe { sys::handle::Handle::from_raw_handle(handle as *mut _) }; + let io = sys::process::Stdio::Handle(handle); + process::Stdio::from_inner(io) + } } } @@ -26,9 +28,11 @@ impl From for process::Stdio { /// Takes ownership of a handle and returns a [`Stdio`](process::Stdio) /// that can attach a stream to it. fn from(handle: OwnedHandle) -> process::Stdio { - let handle = sys::handle::Handle::from_inner(handle); - let io = sys::process::Stdio::Handle(handle); - process::Stdio::from_inner(io) + no_code! { + let handle = sys::handle::Handle::from_inner(handle); + let io = sys::process::Stdio::Handle(handle); + process::Stdio::from_inner(io) + } } } @@ -36,7 +40,9 @@ impl From for process::Stdio { impl AsRawHandle for process::Child { #[inline] fn as_raw_handle(&self) -> RawHandle { - self.as_inner().handle().as_raw_handle() as *mut _ + no_code! { + self.as_inner().handle().as_raw_handle() as *mut _ + } } } @@ -44,14 +50,18 @@ impl AsRawHandle for process::Child { impl AsHandle for process::Child { #[inline] fn as_handle(&self) -> BorrowedHandle<'_> { - self.as_inner().handle().as_handle() + no_code! { + self.as_inner().handle().as_handle() + } } } #[stable(feature = "into_raw_os", since = "1.4.0")] impl IntoRawHandle for process::Child { fn into_raw_handle(self) -> RawHandle { - self.into_inner().into_handle().into_raw_handle() as *mut _ + no_code! { + self.into_inner().into_handle().into_raw_handle() as *mut _ + } } } @@ -59,7 +69,9 @@ impl IntoRawHandle for process::Child { impl From for OwnedHandle { /// Takes ownership of a [`Child`](process::Child)'s process handle. fn from(child: process::Child) -> OwnedHandle { - child.into_inner().into_handle().into_inner() + no_code! { + child.into_inner().into_handle().into_inner() + } } } @@ -67,7 +79,9 @@ impl From for OwnedHandle { impl AsRawHandle for process::ChildStdin { #[inline] fn as_raw_handle(&self) -> RawHandle { - self.as_inner().handle().as_raw_handle() as *mut _ + no_code! { + self.as_inner().handle().as_raw_handle() as *mut _ + } } } @@ -75,7 +89,9 @@ impl AsRawHandle for process::ChildStdin { impl AsRawHandle for process::ChildStdout { #[inline] fn as_raw_handle(&self) -> RawHandle { - self.as_inner().handle().as_raw_handle() as *mut _ + no_code! { + self.as_inner().handle().as_raw_handle() as *mut _ + } } } @@ -83,28 +99,36 @@ impl AsRawHandle for process::ChildStdout { impl AsRawHandle for process::ChildStderr { #[inline] fn as_raw_handle(&self) -> RawHandle { - self.as_inner().handle().as_raw_handle() as *mut _ + no_code! { + self.as_inner().handle().as_raw_handle() as *mut _ + } } } #[stable(feature = "into_raw_os", since = "1.4.0")] impl IntoRawHandle for process::ChildStdin { fn into_raw_handle(self) -> RawHandle { - self.into_inner().into_handle().into_raw_handle() as *mut _ + no_code! { + self.into_inner().into_handle().into_raw_handle() as *mut _ + } } } #[stable(feature = "into_raw_os", since = "1.4.0")] impl IntoRawHandle for process::ChildStdout { fn into_raw_handle(self) -> RawHandle { - self.into_inner().into_handle().into_raw_handle() as *mut _ + no_code! { + self.into_inner().into_handle().into_raw_handle() as *mut _ + } } } #[stable(feature = "into_raw_os", since = "1.4.0")] impl IntoRawHandle for process::ChildStderr { fn into_raw_handle(self) -> RawHandle { - self.into_inner().into_handle().into_raw_handle() as *mut _ + no_code! { + self.into_inner().into_handle().into_raw_handle() as *mut _ + } } } @@ -115,9 +139,11 @@ impl IntoRawHandle for process::ChildStderr { #[stable(feature = "child_stream_from_fd", since = "1.74.0")] impl From for process::ChildStdin { fn from(handle: OwnedHandle) -> process::ChildStdin { - let handle = sys::handle::Handle::from_inner(handle); - let pipe = sys::process::ChildPipe::from_inner(handle); - process::ChildStdin::from_inner(pipe) + no_code! { + let handle = sys::handle::Handle::from_inner(handle); + let pipe = sys::process::ChildPipe::from_inner(handle); + process::ChildStdin::from_inner(pipe) + } } } @@ -128,9 +154,11 @@ impl From for process::ChildStdin { #[stable(feature = "child_stream_from_fd", since = "1.74.0")] impl From for process::ChildStdout { fn from(handle: OwnedHandle) -> process::ChildStdout { - let handle = sys::handle::Handle::from_inner(handle); - let pipe = sys::process::ChildPipe::from_inner(handle); - process::ChildStdout::from_inner(pipe) + no_code! { + let handle = sys::handle::Handle::from_inner(handle); + let pipe = sys::process::ChildPipe::from_inner(handle); + process::ChildStdout::from_inner(pipe) + } } } @@ -141,9 +169,11 @@ impl From for process::ChildStdout { #[stable(feature = "child_stream_from_fd", since = "1.74.0")] impl From for process::ChildStderr { fn from(handle: OwnedHandle) -> process::ChildStderr { - let handle = sys::handle::Handle::from_inner(handle); - let pipe = sys::process::ChildPipe::from_inner(handle); - process::ChildStderr::from_inner(pipe) + no_code! { + let handle = sys::handle::Handle::from_inner(handle); + let pipe = sys::process::ChildPipe::from_inner(handle); + process::ChildStderr::from_inner(pipe) + } } } @@ -159,7 +189,9 @@ pub impl(self) trait ExitStatusExt { #[stable(feature = "exit_status_from", since = "1.12.0")] impl ExitStatusExt for process::ExitStatus { fn from_raw(raw: u32) -> Self { - process::ExitStatus::from_inner(From::from(raw)) + no_code! { + process::ExitStatus::from_inner(From::from(raw)) + } } } @@ -388,28 +420,38 @@ pub impl(self) trait CommandExt { #[stable(feature = "windows_process_extensions", since = "1.16.0")] impl CommandExt for process::Command { fn creation_flags(&mut self, flags: u32) -> &mut process::Command { - self.as_inner_mut().creation_flags(flags); - self + no_code! { + self.as_inner_mut().creation_flags(flags); + self + } } fn desktop>(&mut self, desktop: S) -> &mut process::Command { - self.as_inner_mut().desktop(desktop.as_ref()); - self + no_code! { + self.as_inner_mut().desktop(desktop.as_ref()); + self + } } fn show_window(&mut self, cmd_show: u16) -> &mut process::Command { - self.as_inner_mut().show_window(Some(cmd_show)); - self + no_code! { + self.as_inner_mut().show_window(Some(cmd_show)); + self + } } fn force_quotes(&mut self, enabled: bool) -> &mut process::Command { - self.as_inner_mut().force_quotes(enabled); - self + no_code! { + self.as_inner_mut().force_quotes(enabled); + self + } } fn raw_arg>(&mut self, raw_text: S) -> &mut process::Command { - self.as_inner_mut().raw_arg(raw_text.as_ref()); - self + no_code! { + self.as_inner_mut().raw_arg(raw_text.as_ref()); + self + } } fn async_pipes(&mut self, always_async: bool) -> &mut process::Command { @@ -425,29 +467,39 @@ impl CommandExt for process::Command { &mut self, attribute_list: &ProcThreadAttributeList<'_>, ) -> io::Result { - self.as_inner_mut() - .spawn_with_attributes(sys::process::Stdio::Inherit, true, Some(attribute_list)) - .map(process::Child::from_inner) + no_code! { + self.as_inner_mut() + .spawn_with_attributes(sys::process::Stdio::Inherit, true, Some(attribute_list)) + .map(process::Child::from_inner) + } } fn startupinfo_fullscreen(&mut self, enabled: bool) -> &mut process::Command { - self.as_inner_mut().startupinfo_fullscreen(enabled); - self + no_code! { + self.as_inner_mut().startupinfo_fullscreen(enabled); + self + } } fn startupinfo_untrusted_source(&mut self, enabled: bool) -> &mut process::Command { - self.as_inner_mut().startupinfo_untrusted_source(enabled); - self + no_code! { + self.as_inner_mut().startupinfo_untrusted_source(enabled); + self + } } fn startupinfo_force_feedback(&mut self, enabled: Option) -> &mut process::Command { - self.as_inner_mut().startupinfo_force_feedback(enabled); - self + no_code! { + self.as_inner_mut().startupinfo_force_feedback(enabled); + self + } } fn inherit_handles(&mut self, inherit_handles: bool) -> &mut process::Command { - self.as_inner_mut().inherit_handles(inherit_handles); - self + no_code! { + self.as_inner_mut().inherit_handles(inherit_handles); + self + } } } @@ -461,7 +513,9 @@ pub impl(self) trait ChildExt { #[stable(feature = "windows_process_extensions_main_thread_handle", since = "1.100.0")] impl ChildExt for process::Child { fn main_thread_handle(&self) -> BorrowedHandle<'_> { - self.handle.main_thread_handle() + no_code! { + self.handle.main_thread_handle() + } } } @@ -481,7 +535,9 @@ pub impl(self) trait ExitCodeExt { #[unstable(feature = "windows_process_exit_code_from", issue = "111688")] impl ExitCodeExt for process::ExitCode { fn from_raw(raw: u32) -> Self { - process::ExitCode::from_inner(From::from(raw)) + no_code! { + process::ExitCode::from_inner(From::from(raw)) + } } } @@ -518,9 +574,11 @@ impl<'a> Drop for ProcThreadAttributeList<'a> { /// /// [1]: fn drop(&mut self) { - let lp_attribute_list = - self.attribute_list.as_mut_ptr().cast::(); - unsafe { sys::c::DeleteProcThreadAttributeList(lp_attribute_list) } + no_code! { + let lp_attribute_list = + self.attribute_list.as_mut_ptr().cast::(); + unsafe { sys::c::DeleteProcThreadAttributeList(lp_attribute_list) } + } } } @@ -664,58 +722,60 @@ impl<'a> ProcThreadAttributeListBuilder<'a> { /// Returns an error if the maximum number of attributes is exceeded /// or if there is an I/O error during initialization. pub fn finish(&self) -> io::Result> { - // To initialize our ProcThreadAttributeList, we need to determine - // how many bytes to allocate for it. The Windows API simplifies this - // process by allowing us to call `InitializeProcThreadAttributeList` - // with a null pointer to retrieve the required size. - let mut required_size = 0; - let Ok(attribute_count) = self.attributes.len().try_into() else { - return Err(io::const_error!( - io::ErrorKind::InvalidInput, - "maximum number of ProcThreadAttributes exceeded", - )); - }; - unsafe { - sys::c::InitializeProcThreadAttributeList( - ptr::null_mut(), - attribute_count, - 0, - &mut required_size, - ) - }; - - let mut attribute_list = vec![MaybeUninit::uninit(); required_size].into_boxed_slice(); - - // Once we've allocated the necessary memory, it's safe to invoke - // `InitializeProcThreadAttributeList` to properly initialize the list. - sys::cvt(unsafe { - sys::c::InitializeProcThreadAttributeList( - attribute_list.as_mut_ptr().cast::(), - attribute_count, - 0, - &mut required_size, - ) - })?; - - // # Add our attributes to the buffer. - // It's theoretically possible for the attribute count to exceed a u32 - // value. Therefore, we ensure that we don't add more attributes than - // the buffer was initialized for. - for (&attribute, value) in self.attributes.iter().take(attribute_count as usize) { + no_code! { + // To initialize our ProcThreadAttributeList, we need to determine + // how many bytes to allocate for it. The Windows API simplifies this + // process by allowing us to call `InitializeProcThreadAttributeList` + // with a null pointer to retrieve the required size. + let mut required_size = 0; + let Ok(attribute_count) = self.attributes.len().try_into() else { + return Err(io::const_error!( + io::ErrorKind::InvalidInput, + "maximum number of ProcThreadAttributes exceeded", + )); + }; + unsafe { + sys::c::InitializeProcThreadAttributeList( + ptr::null_mut(), + attribute_count, + 0, + &mut required_size, + ) + }; + + let mut attribute_list = vec![MaybeUninit::uninit(); required_size].into_boxed_slice(); + + // Once we've allocated the necessary memory, it's safe to invoke + // `InitializeProcThreadAttributeList` to properly initialize the list. sys::cvt(unsafe { - sys::c::UpdateProcThreadAttribute( + sys::c::InitializeProcThreadAttributeList( attribute_list.as_mut_ptr().cast::(), + attribute_count, 0, - attribute, - value.ptr, - value.size, - ptr::null_mut(), - ptr::null_mut(), + &mut required_size, ) })?; - } - Ok(ProcThreadAttributeList { attribute_list, _lifetime_marker: marker::PhantomData }) + // # Add our attributes to the buffer. + // It's theoretically possible for the attribute count to exceed a u32 + // value. Therefore, we ensure that we don't add more attributes than + // the buffer was initialized for. + for (&attribute, value) in self.attributes.iter().take(attribute_count as usize) { + sys::cvt(unsafe { + sys::c::UpdateProcThreadAttribute( + attribute_list.as_mut_ptr().cast::(), + 0, + attribute, + value.ptr, + value.size, + ptr::null_mut(), + ptr::null_mut(), + ) + })?; + } + + Ok(ProcThreadAttributeList { attribute_list, _lifetime_marker: marker::PhantomData }) + } } } diff --git a/library/std/src/os/windows/thread.rs b/library/std/src/os/windows/thread.rs index 1cdee44de4b72..137afc6af1a4c 100644 --- a/library/std/src/os/windows/thread.rs +++ b/library/std/src/os/windows/thread.rs @@ -12,7 +12,9 @@ use crate::thread; impl AsRawHandle for thread::JoinHandle { #[inline] fn as_raw_handle(&self) -> RawHandle { - self.as_inner().handle().as_raw_handle() as *mut _ + no_code! { + self.as_inner().handle().as_raw_handle() as *mut _ + } } } @@ -20,6 +22,8 @@ impl AsRawHandle for thread::JoinHandle { impl IntoRawHandle for thread::JoinHandle { #[inline] fn into_raw_handle(self) -> RawHandle { - self.into_inner().into_handle().into_raw_handle() as *mut _ + no_code! { + self.into_inner().into_handle().into_raw_handle() as *mut _ + } } } diff --git a/library/std/src/sys/process/unix/unix.rs b/library/std/src/sys/process/unix/unix.rs index aa47fcb3360c1..5c709b47e626d 100644 --- a/library/std/src/sys/process/unix/unix.rs +++ b/library/std/src/sys/process/unix/unix.rs @@ -1315,11 +1315,13 @@ mod linux_child_ext { } fn into_pidfd(mut self) -> Result { - self.handle - .pidfd - .take() - .map(>::from_inner) - .ok_or_else(|| self) + no_code! { + self.handle + .pidfd + .take() + .map(>::from_inner) + .ok_or_else(|| self) + } } } } diff --git a/src/bootstrap/src/core/build_steps/doc.rs b/src/bootstrap/src/core/build_steps/doc.rs index 34e9b354bddb2..ebc1a0fd3da89 100644 --- a/src/bootstrap/src/core/build_steps/doc.rs +++ b/src/bootstrap/src/core/build_steps/doc.rs @@ -710,7 +710,14 @@ impl CommandLineStep for Std { .expect("non-utf8 paths are unsupported"); let mut extra_args = match self.format { DocumentationFormat::Html => { - vec!["--markdown-css", "rust.css", "--markdown-no-toc", "--index-page", &index_page] + vec![ + "--markdown-css", + "rust.css", + "--markdown-no-toc", + "--index-page", + &index_page, + "--generate-link-to-definition", + ] } DocumentationFormat::Json => vec![], };