Skip to content

Implement stabilization of #[feature(io_safety)]. #95118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/std/src/os/fd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Owned and borrowed Unix-like file descriptors.

#![unstable(feature = "io_safety", issue = "87074")]
#![stable(feature = "io_safety", since = "1.63.0")]
#![deny(unsafe_op_in_unsafe_fn)]

// `RawFd`, `AsRawFd`, etc.
Expand Down
62 changes: 32 additions & 30 deletions library/std/src/os/fd/owned.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Owned and borrowed Unix-like file descriptors.

#![unstable(feature = "io_safety", issue = "87074")]
#![stable(feature = "io_safety", since = "1.63.0")]
#![deny(unsafe_op_in_unsafe_fn)]

use super::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
Expand Down Expand Up @@ -33,7 +33,7 @@ use crate::sys_common::{AsInner, FromInner, IntoInner};
// because c_int is 32 bits.
#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
#[rustc_nonnull_optimization_guaranteed]
#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
pub struct BorrowedFd<'fd> {
fd: RawFd,
_phantom: PhantomData<&'fd OwnedFd>,
Expand All @@ -54,7 +54,7 @@ pub struct BorrowedFd<'fd> {
// because c_int is 32 bits.
#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
#[rustc_nonnull_optimization_guaranteed]
#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
pub struct OwnedFd {
fd: RawFd,
}
Expand All @@ -67,7 +67,8 @@ impl BorrowedFd<'_> {
/// The resource pointed to by `fd` must remain open for the duration of
/// the returned `BorrowedFd`, and it must not have the value `-1`.
#[inline]
#[unstable(feature = "io_safety", issue = "87074")]
#[rustc_const_stable(feature = "io_safety", since = "1.63.0")]
#[stable(feature = "io_safety", since = "1.63.0")]
pub const unsafe fn borrow_raw(fd: RawFd) -> Self {
assert!(fd != u32::MAX as RawFd);
// SAFETY: we just asserted that the value is in the valid range and isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
Expand All @@ -79,6 +80,7 @@ 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> {
// We want to atomically duplicate this file descriptor and set the
// CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This
Expand All @@ -98,6 +100,7 @@ impl OwnedFd {
}

#[cfg(target_arch = "wasm32")]
#[stable(feature = "io_safety", since = "1.63.0")]
pub fn try_clone(&self) -> crate::io::Result<Self> {
Err(crate::io::const_io_error!(
crate::io::ErrorKind::Unsupported,
Expand All @@ -106,23 +109,23 @@ impl OwnedFd {
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl AsRawFd for BorrowedFd<'_> {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.fd
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl AsRawFd for OwnedFd {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.fd
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl IntoRawFd for OwnedFd {
#[inline]
fn into_raw_fd(self) -> RawFd {
Expand All @@ -132,7 +135,7 @@ impl IntoRawFd for OwnedFd {
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl FromRawFd for OwnedFd {
/// Constructs a new instance of `Self` from the given raw file descriptor.
///
Expand All @@ -148,7 +151,7 @@ impl FromRawFd for OwnedFd {
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl Drop for OwnedFd {
#[inline]
fn drop(&mut self) {
Expand All @@ -163,14 +166,14 @@ impl Drop for OwnedFd {
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl fmt::Debug for BorrowedFd<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BorrowedFd").field("fd", &self.fd).finish()
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl fmt::Debug for OwnedFd {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OwnedFd").field("fd", &self.fd).finish()
Expand All @@ -182,14 +185,13 @@ impl fmt::Debug for OwnedFd {
/// This is only available on unix platforms and must be imported in order to
/// call the method. Windows platforms have a corresponding `AsHandle` and
/// `AsSocket` set of traits.
#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
pub trait AsFd {
/// Borrows the file descriptor.
///
/// # Example
///
/// ```rust,no_run
/// # #![feature(io_safety)]
/// use std::fs::File;
/// # use std::io;
/// # #[cfg(target_os = "wasi")]
Expand All @@ -202,35 +204,35 @@ pub trait AsFd {
/// let borrowed_fd: BorrowedFd<'_> = f.as_fd();
/// # Ok::<(), io::Error>(())
/// ```
#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
fn as_fd(&self) -> BorrowedFd<'_>;
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl<T: AsFd> AsFd for &T {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
T::as_fd(self)
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl<T: AsFd> AsFd for &mut T {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
T::as_fd(self)
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl AsFd for BorrowedFd<'_> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
*self
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl AsFd for OwnedFd {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
Expand All @@ -241,47 +243,47 @@ impl AsFd for OwnedFd {
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl AsFd for fs::File {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
self.as_inner().as_fd()
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl From<fs::File> for OwnedFd {
#[inline]
fn from(file: fs::File) -> OwnedFd {
file.into_inner().into_inner().into_inner()
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl From<OwnedFd> for fs::File {
#[inline]
fn from(owned_fd: OwnedFd) -> Self {
Self::from_inner(FromInner::from_inner(FromInner::from_inner(owned_fd)))
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl AsFd for crate::net::TcpStream {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
self.as_inner().socket().as_fd()
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl From<crate::net::TcpStream> for OwnedFd {
#[inline]
fn from(tcp_stream: crate::net::TcpStream) -> OwnedFd {
tcp_stream.into_inner().into_socket().into_inner().into_inner().into()
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl From<OwnedFd> for crate::net::TcpStream {
#[inline]
fn from(owned_fd: OwnedFd) -> Self {
Expand All @@ -291,23 +293,23 @@ impl From<OwnedFd> for crate::net::TcpStream {
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl AsFd for crate::net::TcpListener {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
self.as_inner().socket().as_fd()
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl From<crate::net::TcpListener> for OwnedFd {
#[inline]
fn from(tcp_listener: crate::net::TcpListener) -> OwnedFd {
tcp_listener.into_inner().into_socket().into_inner().into_inner().into()
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl From<OwnedFd> for crate::net::TcpListener {
#[inline]
fn from(owned_fd: OwnedFd) -> Self {
Expand All @@ -317,23 +319,23 @@ impl From<OwnedFd> for crate::net::TcpListener {
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl AsFd for crate::net::UdpSocket {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
self.as_inner().socket().as_fd()
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl From<crate::net::UdpSocket> for OwnedFd {
#[inline]
fn from(udp_socket: crate::net::UdpSocket) -> OwnedFd {
udp_socket.into_inner().into_socket().into_inner().into_inner().into()
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl From<OwnedFd> for crate::net::UdpSocket {
#[inline]
fn from(owned_fd: OwnedFd) -> Self {
Expand Down
3 changes: 1 addition & 2 deletions library/std/src/os/unix/io/fd.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! Owned and borrowed file descriptors.

#![unstable(feature = "io_safety", issue = "87074")]

// Tests for this module
#[cfg(test)]
mod tests;

#[stable(feature = "io_safety", since = "1.63.0")]
pub use crate::os::fd::owned::*;
2 changes: 1 addition & 1 deletion library/std/src/os/unix/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
mod fd;
mod raw;

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
pub use fd::*;
#[stable(feature = "rust1", since = "1.0.0")]
pub use raw::*;
6 changes: 3 additions & 3 deletions library/std/src/os/unix/net/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -962,23 +962,23 @@ impl IntoRawFd for UnixDatagram {
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl AsFd for UnixDatagram {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_inner().as_fd()
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl From<UnixDatagram> for OwnedFd {
#[inline]
fn from(unix_datagram: UnixDatagram) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(unix_datagram.into_raw_fd()) }
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl From<OwnedFd> for UnixDatagram {
#[inline]
fn from(owned: OwnedFd) -> Self {
Expand Down
6 changes: 3 additions & 3 deletions library/std/src/os/unix/net/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,23 +300,23 @@ impl IntoRawFd for UnixListener {
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl AsFd for UnixListener {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_inner().as_fd()
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl From<OwnedFd> for UnixListener {
#[inline]
fn from(fd: OwnedFd) -> UnixListener {
UnixListener(Socket::from_inner(FromInner::from_inner(fd)))
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl From<UnixListener> for OwnedFd {
#[inline]
fn from(listener: UnixListener) -> OwnedFd {
Expand Down
6 changes: 3 additions & 3 deletions library/std/src/os/unix/net/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,23 +683,23 @@ impl IntoRawFd for UnixStream {
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl AsFd for UnixStream {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl From<UnixStream> for OwnedFd {
#[inline]
fn from(unix_stream: UnixStream) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(unix_stream.into_raw_fd()) }
}
}

#[unstable(feature = "io_safety", issue = "87074")]
#[stable(feature = "io_safety", since = "1.63.0")]
impl From<OwnedFd> for UnixStream {
#[inline]
fn from(owned: OwnedFd) -> Self {
Expand Down
Loading