diff options
| author | Dan Gohman <dev@sunfishcode.online> | 2022-05-19 06:43:37 -0700 |
|---|---|---|
| committer | Dan Gohman <dev@sunfishcode.online> | 2022-06-15 08:54:06 -0700 |
| commit | 5d0eae81aecb71d70d620525459972bf36771921 (patch) | |
| tree | 94710759ef5ef64d32b4392c81a2a1c0503ba36a | |
| parent | eb37bbcebc3f6d0981eef892817f3a4570e35907 (diff) | |
| download | rust-5d0eae81aecb71d70d620525459972bf36771921.tar.gz rust-5d0eae81aecb71d70d620525459972bf36771921.zip | |
Add `BorrowedFd::try_clone_to_owned`.
And `BorrowedHandle::try_clone_to_owned` and `BorrowedSocket::try_clone_to_owned` on Windows.
| -rw-r--r-- | library/std/src/os/fd/owned.rs | 15 | ||||
| -rw-r--r-- | library/std/src/os/windows/io/handle.rs | 8 | ||||
| -rw-r--r-- | library/std/src/os/windows/io/socket.rs | 36 |
3 files changed, 42 insertions, 17 deletions
diff --git a/library/std/src/os/fd/owned.rs b/library/std/src/os/fd/owned.rs index 45b792039ef..7d688940d45 100644 --- a/library/std/src/os/fd/owned.rs +++ b/library/std/src/os/fd/owned.rs @@ -79,9 +79,18 @@ impl BorrowedFd<'_> { impl OwnedFd { /// Creates a new `OwnedFd` instance that shares the same underlying file handle /// as the existing `OwnedFd` instance. - #[cfg(not(target_arch = "wasm32"))] #[stable(feature = "io_safety", since = "1.63.0")] pub fn try_clone(&self) -> crate::io::Result<Self> { + self.as_fd().try_clone_to_owned() + } +} + +impl BorrowedFd<'_> { + /// Creates a new `OwnedFd` instance that shares the same underlying file + /// description as the existing `BorrowedFd` instance. + #[cfg(not(target_arch = "wasm32"))] + #[stable(feature = "io_safety", since = "1.63.0")] + pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> { // We want to atomically duplicate this file descriptor and set the // CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This // is a POSIX flag that was added to Linux in 2.6.24. @@ -96,12 +105,12 @@ impl OwnedFd { let cmd = libc::F_DUPFD; let fd = cvt(unsafe { libc::fcntl(self.as_raw_fd(), cmd, 0) })?; - Ok(unsafe { Self::from_raw_fd(fd) }) + Ok(unsafe { OwnedFd::from_raw_fd(fd) }) } #[cfg(target_arch = "wasm32")] #[stable(feature = "io_safety", since = "1.63.0")] - pub fn try_clone(&self) -> crate::io::Result<Self> { + pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> { Err(crate::io::const_io_error!( crate::io::ErrorKind::Unsupported, "operation not supported on WASI yet", diff --git a/library/std/src/os/windows/io/handle.rs b/library/std/src/os/windows/io/handle.rs index 91b886c0888..c9d8624e838 100644 --- a/library/std/src/os/windows/io/handle.rs +++ b/library/std/src/os/windows/io/handle.rs @@ -181,6 +181,14 @@ impl OwnedHandle { /// as the existing `OwnedHandle` instance. #[stable(feature = "io_safety", since = "1.63.0")] pub fn try_clone(&self) -> crate::io::Result<Self> { + self.as_handle().try_clone_to_owned() + } +} + +impl BorrowedHandle<'_> { + /// Creates a new `OwnedHandle` instance that shares the same underlying + /// object as the existing `BorrowedHandle` instance. + pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedHandle> { self.duplicate(0, false, c::DUPLICATE_SAME_ACCESS) } diff --git a/library/std/src/os/windows/io/socket.rs b/library/std/src/os/windows/io/socket.rs index c5a381c40d2..b8cacf0b6c5 100644 --- a/library/std/src/os/windows/io/socket.rs +++ b/library/std/src/os/windows/io/socket.rs @@ -86,6 +86,28 @@ impl OwnedSocket { /// as the existing `OwnedSocket` instance. #[stable(feature = "io_safety", since = "1.63.0")] pub fn try_clone(&self) -> io::Result<Self> { + self.as_socket().try_clone_to_owned() + } + + // FIXME(strict_provenance_magic): we defined RawSocket to be a u64 ;-; + #[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) + }) + .map(drop) + } + + #[cfg(target_vendor = "uwp")] + pub(crate) fn set_no_inherit(&self) -> io::Result<()> { + Err(io::const_io_error!(io::ErrorKind::Unsupported, "Unavailable on UWP")) + } +} + +impl BorrowedSocket<'_> { + /// Creates a new `OwnedSocket` instance that shares the same underlying + /// object as the existing `BorrowedSocket` instance. + pub fn try_clone_to_owned(&self) -> io::Result<OwnedSocket> { let mut info = unsafe { mem::zeroed::<c::WSAPROTOCOL_INFO>() }; let result = unsafe { c::WSADuplicateSocketW(self.as_raw_socket(), c::GetCurrentProcessId(), &mut info) @@ -133,20 +155,6 @@ impl OwnedSocket { } } } - - // FIXME(strict_provenance_magic): we defined RawSocket to be a u64 ;-; - #[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) - }) - .map(drop) - } - - #[cfg(target_vendor = "uwp")] - pub(crate) fn set_no_inherit(&self) -> io::Result<()> { - Err(io::const_io_error!(io::ErrorKind::Unsupported, "Unavailable on UWP")) - } } /// Returns the last error from the Windows socket interface. |
