Skip to content

Commit 5d0eae8

Browse files
committed
Add BorrowedFd::try_clone_to_owned.
And `BorrowedHandle::try_clone_to_owned` and `BorrowedSocket::try_clone_to_owned` on Windows.
1 parent eb37bbc commit 5d0eae8

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

library/std/src/os/fd/owned.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,18 @@ impl BorrowedFd<'_> {
7979
impl OwnedFd {
8080
/// Creates a new `OwnedFd` instance that shares the same underlying file handle
8181
/// as the existing `OwnedFd` instance.
82-
#[cfg(not(target_arch = "wasm32"))]
8382
#[stable(feature = "io_safety", since = "1.63.0")]
8483
pub fn try_clone(&self) -> crate::io::Result<Self> {
84+
self.as_fd().try_clone_to_owned()
85+
}
86+
}
87+
88+
impl BorrowedFd<'_> {
89+
/// Creates a new `OwnedFd` instance that shares the same underlying file
90+
/// description as the existing `BorrowedFd` instance.
91+
#[cfg(not(target_arch = "wasm32"))]
92+
#[stable(feature = "io_safety", since = "1.63.0")]
93+
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
8594
// We want to atomically duplicate this file descriptor and set the
8695
// CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This
8796
// is a POSIX flag that was added to Linux in 2.6.24.
@@ -96,12 +105,12 @@ impl OwnedFd {
96105
let cmd = libc::F_DUPFD;
97106

98107
let fd = cvt(unsafe { libc::fcntl(self.as_raw_fd(), cmd, 0) })?;
99-
Ok(unsafe { Self::from_raw_fd(fd) })
108+
Ok(unsafe { OwnedFd::from_raw_fd(fd) })
100109
}
101110

102111
#[cfg(target_arch = "wasm32")]
103112
#[stable(feature = "io_safety", since = "1.63.0")]
104-
pub fn try_clone(&self) -> crate::io::Result<Self> {
113+
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
105114
Err(crate::io::const_io_error!(
106115
crate::io::ErrorKind::Unsupported,
107116
"operation not supported on WASI yet",

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

+8
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ impl OwnedHandle {
181181
/// as the existing `OwnedHandle` instance.
182182
#[stable(feature = "io_safety", since = "1.63.0")]
183183
pub fn try_clone(&self) -> crate::io::Result<Self> {
184+
self.as_handle().try_clone_to_owned()
185+
}
186+
}
187+
188+
impl BorrowedHandle<'_> {
189+
/// Creates a new `OwnedHandle` instance that shares the same underlying
190+
/// object as the existing `BorrowedHandle` instance.
191+
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedHandle> {
184192
self.duplicate(0, false, c::DUPLICATE_SAME_ACCESS)
185193
}
186194

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

+22-14
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,28 @@ impl OwnedSocket {
8686
/// as the existing `OwnedSocket` instance.
8787
#[stable(feature = "io_safety", since = "1.63.0")]
8888
pub fn try_clone(&self) -> io::Result<Self> {
89+
self.as_socket().try_clone_to_owned()
90+
}
91+
92+
// FIXME(strict_provenance_magic): we defined RawSocket to be a u64 ;-;
93+
#[cfg(not(target_vendor = "uwp"))]
94+
pub(crate) fn set_no_inherit(&self) -> io::Result<()> {
95+
cvt(unsafe {
96+
c::SetHandleInformation(self.as_raw_socket() as c::HANDLE, c::HANDLE_FLAG_INHERIT, 0)
97+
})
98+
.map(drop)
99+
}
100+
101+
#[cfg(target_vendor = "uwp")]
102+
pub(crate) fn set_no_inherit(&self) -> io::Result<()> {
103+
Err(io::const_io_error!(io::ErrorKind::Unsupported, "Unavailable on UWP"))
104+
}
105+
}
106+
107+
impl BorrowedSocket<'_> {
108+
/// Creates a new `OwnedSocket` instance that shares the same underlying
109+
/// object as the existing `BorrowedSocket` instance.
110+
pub fn try_clone_to_owned(&self) -> io::Result<OwnedSocket> {
89111
let mut info = unsafe { mem::zeroed::<c::WSAPROTOCOL_INFO>() };
90112
let result = unsafe {
91113
c::WSADuplicateSocketW(self.as_raw_socket(), c::GetCurrentProcessId(), &mut info)
@@ -133,20 +155,6 @@ impl OwnedSocket {
133155
}
134156
}
135157
}
136-
137-
// FIXME(strict_provenance_magic): we defined RawSocket to be a u64 ;-;
138-
#[cfg(not(target_vendor = "uwp"))]
139-
pub(crate) fn set_no_inherit(&self) -> io::Result<()> {
140-
cvt(unsafe {
141-
c::SetHandleInformation(self.as_raw_socket() as c::HANDLE, c::HANDLE_FLAG_INHERIT, 0)
142-
})
143-
.map(drop)
144-
}
145-
146-
#[cfg(target_vendor = "uwp")]
147-
pub(crate) fn set_no_inherit(&self) -> io::Result<()> {
148-
Err(io::const_io_error!(io::ErrorKind::Unsupported, "Unavailable on UWP"))
149-
}
150158
}
151159

152160
/// Returns the last error from the Windows socket interface.

0 commit comments

Comments
 (0)