diff options
| author | Stefan Lankes <slankes@eonerc.rwth-aachen.de> | 2023-01-28 23:42:08 +0100 |
|---|---|---|
| committer | Stefan Lankes <slankes@eonerc.rwth-aachen.de> | 2023-02-24 15:30:14 +0100 |
| commit | b5fb4f3d9b1b308d59cab24ef2f9bf23dad948aa (patch) | |
| tree | f682c0e00500a79dee0a236211318f14507f92d0 /library/std/src/sys | |
| parent | 7143379a528d7a26afbe835dd7e4bdca4bdb1412 (diff) | |
| download | rust-b5fb4f3d9b1b308d59cab24ef2f9bf23dad948aa.tar.gz rust-b5fb4f3d9b1b308d59cab24ef2f9bf23dad948aa.zip | |
move IO traits to std/src/os/hermit
By moving the IO traits, the RustyHermit support is harmonized to of other operating systems.
Diffstat (limited to 'library/std/src/sys')
| -rw-r--r-- | library/std/src/sys/hermit/args.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/hermit/fd.rs (renamed from library/std/src/sys/hermit/fd/mod.rs) | 7 | ||||
| -rw-r--r-- | library/std/src/sys/hermit/fd/owned.rs | 238 | ||||
| -rw-r--r-- | library/std/src/sys/hermit/fd/raw.rs | 115 | ||||
| -rw-r--r-- | library/std/src/sys/hermit/fs.rs | 8 | ||||
| -rw-r--r-- | library/std/src/sys/hermit/mod.rs | 4 | ||||
| -rw-r--r-- | library/std/src/sys/hermit/net.rs | 3 | ||||
| -rw-r--r-- | library/std/src/sys/hermit/os.rs | 2 |
8 files changed, 12 insertions, 367 deletions
diff --git a/library/std/src/sys/hermit/args.rs b/library/std/src/sys/hermit/args.rs index afcae6c90ee..220a76e4b12 100644 --- a/library/std/src/sys/hermit/args.rs +++ b/library/std/src/sys/hermit/args.rs @@ -1,6 +1,6 @@ use crate::ffi::{c_char, CStr, OsString}; use crate::fmt; -use crate::os::unix::ffi::OsStringExt; +use crate::os::hermit::ffi::OsStringExt; use crate::ptr; use crate::sync::atomic::{ AtomicIsize, AtomicPtr, diff --git a/library/std/src/sys/hermit/fd/mod.rs b/library/std/src/sys/hermit/fd.rs index 7f3c7ea1030..ea981b9fa97 100644 --- a/library/std/src/sys/hermit/fd/mod.rs +++ b/library/std/src/sys/hermit/fd.rs @@ -1,16 +1,13 @@ #![unstable(reason = "not public", issue = "none", feature = "fd")] -mod owned; -mod raw; - use crate::io::{self, Read}; +use crate::os::hermit::io::{FromRawFd, OwnedFd, RawFd}; use crate::sys::cvt; use crate::sys::hermit::abi; use crate::sys::unsupported; use crate::sys_common::{AsInner, FromInner, IntoInner}; -pub use self::owned::*; -pub use self::raw::*; +use crate::os::hermit::io::*; #[derive(Debug)] pub struct FileDesc { diff --git a/library/std/src/sys/hermit/fd/owned.rs b/library/std/src/sys/hermit/fd/owned.rs deleted file mode 100644 index 7746cb3b259..00000000000 --- a/library/std/src/sys/hermit/fd/owned.rs +++ /dev/null @@ -1,238 +0,0 @@ -use super::raw::RawFd; - -use crate::marker::PhantomData; -use crate::mem::forget; -use crate::sys::fd::{AsRawFd, FromRawFd, IntoRawFd}; -use crate::sys::hermit::abi; -use crate::sys_common::{AsInner, FromInner, IntoInner}; - -/// A borrowed file descriptor. -/// -/// This has a lifetime parameter to tie it to the lifetime of something that -/// owns the file descriptor. -/// -/// This uses `repr(transparent)` and has the representation of a host file -/// 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`. -/// -/// This type's `.to_owned()` implementation returns another `BorrowedFd` -/// rather than an `OwnedFd`. It just makes a trivial copy of the raw file -/// descriptor, which is then borrowed under the same lifetime. -#[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)] -#[rustc_nonnull_optimization_guaranteed] -#[stable(feature = "io_safety", since = "1.63.0")] -pub struct BorrowedFd<'fd> { - fd: RawFd, - _phantom: PhantomData<&'fd OwnedFd>, -} - -/// An owned file descriptor. -/// -/// This closes the file descriptor on drop. -/// -/// This uses `repr(transparent)` and has the representation of a host file -/// 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)] -#[rustc_nonnull_optimization_guaranteed] -#[stable(feature = "io_safety", since = "1.63.0")] -#[derive(Debug)] -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] - #[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) - unsafe { Self { fd, _phantom: PhantomData } } - } -} - -#[stable(feature = "io_safety", since = "1.63.0")] -impl AsRawFd for BorrowedFd<'_> { - #[inline] - fn as_raw_fd(&self) -> RawFd { - self.fd - } -} - -#[stable(feature = "io_safety", since = "1.63.0")] -impl AsRawFd for OwnedFd { - #[inline] - fn as_raw_fd(&self) -> RawFd { - self.fd - } -} - -#[stable(feature = "io_safety", since = "1.63.0")] -impl IntoRawFd for OwnedFd { - #[inline] - fn into_raw_fd(self) -> RawFd { - let fd = self.fd; - forget(self); - fd - } -} - -#[stable(feature = "io_safety", since = "1.63.0")] -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 } } - } -} - -#[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() - } -} - -#[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() - } -} - -#[stable(feature = "io_safety", since = "1.63.0")] -impl From<OwnedFd> for crate::net::TcpStream { - #[inline] - fn from(owned_fd: OwnedFd) -> Self { - Self::from_inner(FromInner::from_inner(FromInner::from_inner(FromInner::from_inner( - owned_fd, - )))) - } -} - -#[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() - } -} - -#[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() - } -} - -#[stable(feature = "io_safety", since = "1.63.0")] -impl From<OwnedFd> for crate::net::TcpListener { - #[inline] - fn from(owned_fd: OwnedFd) -> Self { - Self::from_inner(FromInner::from_inner(FromInner::from_inner(FromInner::from_inner( - owned_fd, - )))) - } -} - -#[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() - } -} - -#[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() - } -} - -#[stable(feature = "io_safety", since = "1.63.0")] -impl From<OwnedFd> for crate::net::UdpSocket { - #[inline] - fn from(owned_fd: OwnedFd) -> Self { - Self::from_inner(FromInner::from_inner(FromInner::from_inner(FromInner::from_inner( - owned_fd, - )))) - } -} - -#[stable(feature = "io_safety", since = "1.63.0")] -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 _ = abi::close(self.fd); - } - } -} - -pub trait AsFd { - /// Borrows the file descriptor. - /// - /// # Example - /// - /// ```rust,no_run - /// use std::fs::File; - /// # use std::io; - /// # #[cfg(any(unix, target_os = "wasi"))] - /// # use std::os::fd::{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>(()) - /// ``` - #[stable(feature = "io_safety", since = "1.63.0")] - fn as_fd(&self) -> BorrowedFd<'_>; -} - -#[stable(feature = "io_safety", since = "1.63.0")] -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(self.as_raw_fd()) } - } -} diff --git a/library/std/src/sys/hermit/fd/raw.rs b/library/std/src/sys/hermit/fd/raw.rs deleted file mode 100644 index cdec90b4b87..00000000000 --- a/library/std/src/sys/hermit/fd/raw.rs +++ /dev/null @@ -1,115 +0,0 @@ -/// Raw file descriptors. -#[rustc_allowed_through_unstable_modules] -#[stable(feature = "rust1", since = "1.0.0")] -pub type RawFd = i32; - -/// A trait to extract the raw file descriptor from an underlying object. -/// -/// This is only available on unix and WASI platforms and must be imported in -/// order to call the method. Windows platforms have a corresponding -/// `AsRawHandle` and `AsRawSocket` set of traits. -#[rustc_allowed_through_unstable_modules] -#[stable(feature = "rust1", since = "1.0.0")] -pub trait AsRawFd { - /// Extracts the raw file descriptor. - /// - /// This function is typically used to **borrow** an owned file descriptor. - /// When used in this way, this method does **not** pass ownership of the - /// raw file descriptor to the caller, and the file descriptor is only - /// guaranteed to be valid while the original object has not yet been - /// destroyed. - /// - /// However, borrowing is not strictly required. See [`AsFd::as_fd`] - /// for an API which strictly borrows a file descriptor. - /// - /// # Example - /// - /// ```no_run - /// use std::fs::File; - /// # use std::io; - /// #[cfg(any(unix, target_os = "wasi"))] - /// use std::os::fd::{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; -} - -/// A trait to express the ability to consume an object and acquire ownership of -/// its raw file descriptor. -#[rustc_allowed_through_unstable_modules] -#[stable(feature = "into_raw_os", since = "1.4.0")] -pub trait IntoRawFd { - /// Consumes this object, returning the raw underlying file descriptor. - /// - /// This function is typically used to **transfer ownership** of the underlying - /// file descriptor to the caller. When used in this way, callers are then the unique - /// owners of the file descriptor and must close it once it's no longer needed. - /// - /// However, transferring ownership is not strictly required. Use a - /// [`Into<OwnedFd>::into`] implementation for an API which strictly - /// transfers ownership. - /// - /// # Example - /// - /// ```no_run - /// use std::fs::File; - /// # use std::io; - /// #[cfg(any(unix, target_os = "wasi"))] - /// use std::os::fd::{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; -} - -/// A trait to express the ability to construct an object from a raw file -/// descriptor. -#[rustc_allowed_through_unstable_modules] -#[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 is typically used to **consume ownership** of the - /// specified file descriptor. When used in this way, the returned object - /// will take responsibility for closing it when the object goes out of - /// scope. - /// - /// However, consuming ownership is not strictly required. Use a - /// [`From<OwnedFd>::from`] implementation for an API which strictly - /// consumes ownership. - /// - /// # Safety - /// - /// The `fd` passed in must be a valid and open file descriptor. - /// - /// # Example - /// - /// ```no_run - /// use std::fs::File; - /// # use std::io; - /// #[cfg(any(unix, target_os = "wasi"))] - /// use std::os::fd::{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; -} diff --git a/library/std/src/sys/hermit/fs.rs b/library/std/src/sys/hermit/fs.rs index 6ae44484bce..cf9f9e06264 100644 --- a/library/std/src/sys/hermit/fs.rs +++ b/library/std/src/sys/hermit/fs.rs @@ -3,12 +3,14 @@ use crate::fmt; use crate::hash::{Hash, Hasher}; use crate::io::{self, Error, ErrorKind}; use crate::io::{BorrowedCursor, IoSlice, IoSliceMut, SeekFrom}; +use crate::os::hermit::io::FromRawFd; use crate::path::{Path, PathBuf}; use crate::sys::common::small_c_string::run_path_with_cstr; use crate::sys::cvt; -use crate::sys::hermit::abi; -use crate::sys::hermit::abi::{O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY}; -use crate::sys::hermit::fd::{FileDesc, FromRawFd}; +use crate::sys::hermit::abi::{ + self, O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY, +}; +use crate::sys::hermit::fd::FileDesc; use crate::sys::time::SystemTime; use crate::sys::unsupported; diff --git a/library/std/src/sys/hermit/mod.rs b/library/std/src/sys/hermit/mod.rs index a5956194eec..d34a4cfedea 100644 --- a/library/std/src/sys/hermit/mod.rs +++ b/library/std/src/sys/hermit/mod.rs @@ -57,9 +57,7 @@ pub mod locks { } use crate::io::ErrorKind; - -#[allow(unused_extern_crates)] -pub extern crate hermit_abi as abi; +use crate::os::hermit::abi; pub fn unsupported<T>() -> crate::io::Result<T> { Err(unsupported_err()) diff --git a/library/std/src/sys/hermit/net.rs b/library/std/src/sys/hermit/net.rs index 2d92068bca6..5fb6281aa1e 100644 --- a/library/std/src/sys/hermit/net.rs +++ b/library/std/src/sys/hermit/net.rs @@ -4,7 +4,8 @@ use crate::cmp; use crate::io::{self, IoSlice, IoSliceMut}; use crate::mem; use crate::net::{Shutdown, SocketAddr}; -use crate::sys::fd::{AsFd, AsRawFd, BorrowedFd, FileDesc, FromRawFd, RawFd}; +use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, RawFd}; +use crate::sys::hermit::fd::FileDesc; use crate::sys::time::Instant; use crate::sys_common::net::{getsockopt, setsockopt, sockaddr_to_addr}; use crate::sys_common::{AsInner, FromInner, IntoInner}; diff --git a/library/std/src/sys/hermit/os.rs b/library/std/src/sys/hermit/os.rs index 8f927df85be..e53dbae6119 100644 --- a/library/std/src/sys/hermit/os.rs +++ b/library/std/src/sys/hermit/os.rs @@ -4,7 +4,7 @@ use crate::ffi::{CStr, OsStr, OsString}; use crate::fmt; use crate::io; use crate::marker::PhantomData; -use crate::os::unix::ffi::OsStringExt; +use crate::os::hermit::ffi::OsStringExt; use crate::path::{self, PathBuf}; use crate::str; use crate::sync::Mutex; |
