diff options
| author | Dan Gohman <dev@sunfishcode.online> | 2021-12-01 14:47:02 -0800 |
|---|---|---|
| committer | Dan Gohman <dev@sunfishcode.online> | 2021-12-02 13:03:33 -0800 |
| commit | fa3a41f8c6463614cd1bc7f9776d9f0cfdad614c (patch) | |
| tree | 8edb61dc8d47a356c16fcbea324248bfaf57c685 | |
| parent | 5edd8df720183850794259d09a51d04b06ad6ea3 (diff) | |
| download | rustix.tar.gz | |
Use rustix types for OwnedFd, BorrowedFd, and AsFd.rustix
| -rw-r--r-- | library/std/src/os/fd/owned.rs | 155 | ||||
| -rw-r--r-- | library/std/src/os/fd/raw.rs | 115 |
2 files changed, 7 insertions, 263 deletions
diff --git a/library/std/src/os/fd/owned.rs b/library/std/src/os/fd/owned.rs index 52d7d4690d3..7a45bd24395 100644 --- a/library/std/src/os/fd/owned.rs +++ b/library/std/src/os/fd/owned.rs @@ -3,11 +3,7 @@ #![unstable(feature = "io_safety", issue = "87074")] #![deny(unsafe_op_in_unsafe_fn)] -use super::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; -use crate::fmt; use crate::fs; -use crate::marker::PhantomData; -use crate::mem::forget; use crate::sys_common::{AsInner, FromInner, IntoInner}; /// A borrowed file descriptor. @@ -19,18 +15,8 @@ use crate::sys_common::{AsInner, FromInner, IntoInner}; /// descriptor, so it can be used in FFI in places where a file descriptor is /// passed as an argument, it is not captured or consumed, and it never has the /// value `-1`. -#[derive(Copy, Clone)] -#[repr(transparent)] -#[rustc_layout_scalar_valid_range_start(0)] -// libstd/os/raw/mod.rs assures me that every libstd-supported platform has a -// 32-bit c_int. Below is -2, in two's complement, but that only works out -// because c_int is 32 bits. -#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)] #[unstable(feature = "io_safety", issue = "87074")] -pub struct BorrowedFd<'fd> { - fd: RawFd, - _phantom: PhantomData<&'fd OwnedFd>, -} +pub use rustix::fd::BorrowedFd; /// An owned file descriptor. /// @@ -40,103 +26,8 @@ pub struct BorrowedFd<'fd> { /// descriptor, so it can be used in FFI in places where a file descriptor is /// passed as a consumed argument or returned as an owned value, and it never /// has the value `-1`. -#[repr(transparent)] -#[rustc_layout_scalar_valid_range_start(0)] -// libstd/os/raw/mod.rs assures me that every libstd-supported platform has a -// 32-bit c_int. Below is -2, in two's complement, but that only works out -// because c_int is 32 bits. -#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)] -#[unstable(feature = "io_safety", issue = "87074")] -pub struct OwnedFd { - fd: RawFd, -} - -impl BorrowedFd<'_> { - /// Return a `BorrowedFd` holding the given raw file descriptor. - /// - /// # Safety - /// - /// 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")] - pub unsafe fn borrow_raw_fd(fd: RawFd) -> Self { - assert_ne!(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) - unsafe { Self { fd, _phantom: PhantomData } } - } -} - -#[unstable(feature = "io_safety", issue = "87074")] -impl AsRawFd for BorrowedFd<'_> { - #[inline] - fn as_raw_fd(&self) -> RawFd { - self.fd - } -} - -#[unstable(feature = "io_safety", issue = "87074")] -impl AsRawFd for OwnedFd { - #[inline] - fn as_raw_fd(&self) -> RawFd { - self.fd - } -} - -#[unstable(feature = "io_safety", issue = "87074")] -impl IntoRawFd for OwnedFd { - #[inline] - fn into_raw_fd(self) -> RawFd { - let fd = self.fd; - forget(self); - fd - } -} - -#[unstable(feature = "io_safety", issue = "87074")] -impl FromRawFd for OwnedFd { - /// Constructs a new instance of `Self` from the given raw file descriptor. - /// - /// # Safety - /// - /// The resource pointed to by `fd` must be open and suitable for assuming - /// ownership. The resource must not require any cleanup other than `close`. - #[inline] - unsafe fn from_raw_fd(fd: RawFd) -> Self { - assert_ne!(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) - unsafe { Self { fd } } - } -} - -#[unstable(feature = "io_safety", issue = "87074")] -impl Drop for OwnedFd { - #[inline] - fn drop(&mut self) { - unsafe { - // Note that errors are ignored when closing a file descriptor. The - // reason for this is that if an error occurs we don't actually know if - // the file descriptor was closed or not, and if we retried (for - // something like EINTR), we might close another valid file descriptor - // opened after we closed ours. - let _ = libc::close(self.fd); - } - } -} - #[unstable(feature = "io_safety", issue = "87074")] -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")] -impl fmt::Debug for OwnedFd { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("OwnedFd").field("fd", &self.fd).finish() - } -} +pub use rustix::io::OwnedFd; /// A trait to borrow the file descriptor from an underlying object. /// @@ -144,47 +35,7 @@ impl fmt::Debug for OwnedFd { /// call the method. Windows platforms have a corresponding `AsHandle` and /// `AsSocket` set of traits. #[unstable(feature = "io_safety", issue = "87074")] -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")] - /// # use std::os::wasi::io::{AsFd, BorrowedFd}; - /// # #[cfg(unix)] - /// # use std::os::unix::io::{AsFd, BorrowedFd}; - /// - /// let mut f = File::open("foo.txt")?; - /// # #[cfg(any(unix, target_os = "wasi"))] - /// let borrowed_fd: BorrowedFd<'_> = f.as_fd(); - /// # Ok::<(), io::Error>(()) - /// ``` - #[unstable(feature = "io_safety", issue = "87074")] - fn as_fd(&self) -> BorrowedFd<'_>; -} - -#[unstable(feature = "io_safety", issue = "87074")] -impl AsFd for BorrowedFd<'_> { - #[inline] - fn as_fd(&self) -> BorrowedFd<'_> { - *self - } -} - -#[unstable(feature = "io_safety", issue = "87074")] -impl AsFd for OwnedFd { - #[inline] - fn as_fd(&self) -> BorrowedFd<'_> { - // Safety: `OwnedFd` and `BorrowedFd` have the same validity - // invariants, and the `BorrowdFd` is bounded by the lifetime - // of `&self`. - unsafe { BorrowedFd::borrow_raw_fd(self.as_raw_fd()) } - } -} +pub use rustix::fd::AsFd; #[unstable(feature = "io_safety", issue = "87074")] impl AsFd for fs::File { diff --git a/library/std/src/os/fd/raw.rs b/library/std/src/os/fd/raw.rs index f874cf0b42d..66715f4ef1b 100644 --- a/library/std/src/os/fd/raw.rs +++ b/library/std/src/os/fd/raw.rs @@ -4,7 +4,6 @@ use crate::fs; use crate::io; -use crate::os::raw; #[cfg(unix)] use crate::os::unix::io::OwnedFd; #[cfg(target_os = "wasi")] @@ -13,7 +12,7 @@ use crate::sys_common::{AsInner, IntoInner}; /// Raw file descriptors. #[stable(feature = "rust1", since = "1.0.0")] -pub type RawFd = raw::c_int; +pub use rustix::fd::RawFd; /// A trait to extract the raw file descriptor from an underlying object. /// @@ -21,123 +20,17 @@ pub type RawFd = raw::c_int; /// order to call the method. Windows platforms have a corresponding /// `AsRawHandle` and `AsRawSocket` set of traits. #[stable(feature = "rust1", since = "1.0.0")] -pub trait AsRawFd { - /// Extracts the raw file descriptor. - /// - /// This method does **not** pass ownership of the raw file descriptor - /// to the caller. The descriptor is only guaranteed to be valid while - /// the original object has not yet been destroyed. - /// - /// # Example - /// - /// ```no_run - /// use std::fs::File; - /// # use std::io; - /// #[cfg(unix)] - /// use std::os::unix::io::{AsRawFd, RawFd}; - /// #[cfg(target_os = "wasi")] - /// use std::os::wasi::io::{AsRawFd, RawFd}; - /// - /// let mut f = File::open("foo.txt")?; - /// // Note that `raw_fd` is only valid as long as `f` exists. - /// #[cfg(any(unix, target_os = "wasi"))] - /// let raw_fd: RawFd = f.as_raw_fd(); - /// # Ok::<(), io::Error>(()) - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn as_raw_fd(&self) -> RawFd; -} +pub use rustix::fd::AsRawFd; /// A trait to express the ability to construct an object from a raw file /// descriptor. #[stable(feature = "from_raw_os", since = "1.1.0")] -pub trait FromRawFd { - /// Constructs a new instance of `Self` from the given raw file - /// descriptor. - /// - /// This function **consumes ownership** of the specified file - /// descriptor. The returned object will take responsibility for closing - /// it when the object goes out of scope. - /// - /// This function is also unsafe as the primitives currently returned - /// have the contract that they are the sole owner of the file - /// descriptor they are wrapping. Usage of this function could - /// accidentally allow violating this contract which can cause memory - /// unsafety in code that relies on it being true. - /// - /// # Example - /// - /// ```no_run - /// use std::fs::File; - /// # use std::io; - /// #[cfg(unix)] - /// use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd}; - /// #[cfg(target_os = "wasi")] - /// use std::os::wasi::io::{FromRawFd, IntoRawFd, RawFd}; - /// - /// let f = File::open("foo.txt")?; - /// # #[cfg(any(unix, target_os = "wasi"))] - /// let raw_fd: RawFd = f.into_raw_fd(); - /// // SAFETY: no other functions should call `from_raw_fd`, so there - /// // is only one owner for the file descriptor. - /// # #[cfg(any(unix, target_os = "wasi"))] - /// let f = unsafe { File::from_raw_fd(raw_fd) }; - /// # Ok::<(), io::Error>(()) - /// ``` - #[stable(feature = "from_raw_os", since = "1.1.0")] - unsafe fn from_raw_fd(fd: RawFd) -> Self; -} +pub use rustix::fd::FromRawFd; /// A trait to express the ability to consume an object and acquire ownership of /// its raw file descriptor. #[stable(feature = "into_raw_os", since = "1.4.0")] -pub trait IntoRawFd { - /// Consumes this object, returning the raw underlying file descriptor. - /// - /// This function **transfers ownership** of the underlying file descriptor - /// to the caller. Callers are then the unique owners of the file descriptor - /// and must close the descriptor once it's no longer needed. - /// - /// # Example - /// - /// ```no_run - /// use std::fs::File; - /// # use std::io; - /// #[cfg(unix)] - /// use std::os::unix::io::{IntoRawFd, RawFd}; - /// #[cfg(target_os = "wasi")] - /// use std::os::wasi::io::{IntoRawFd, RawFd}; - /// - /// let f = File::open("foo.txt")?; - /// #[cfg(any(unix, target_os = "wasi"))] - /// let raw_fd: RawFd = f.into_raw_fd(); - /// # Ok::<(), io::Error>(()) - /// ``` - #[stable(feature = "into_raw_os", since = "1.4.0")] - fn into_raw_fd(self) -> RawFd; -} - -#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")] -impl AsRawFd for RawFd { - #[inline] - fn as_raw_fd(&self) -> RawFd { - *self - } -} -#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")] -impl IntoRawFd for RawFd { - #[inline] - fn into_raw_fd(self) -> RawFd { - self - } -} -#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")] -impl FromRawFd for RawFd { - #[inline] - unsafe fn from_raw_fd(fd: RawFd) -> RawFd { - fd - } -} +pub use rustix::fd::IntoRawFd; #[stable(feature = "rust1", since = "1.0.0")] impl AsRawFd for fs::File { |
