From b5fb4f3d9b1b308d59cab24ef2f9bf23dad948aa Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Sat, 28 Jan 2023 23:42:08 +0100 Subject: [PATCH] move IO traits to std/src/os/hermit By moving the IO traits, the RustyHermit support is harmonized to of other operating systems. --- library/std/src/os/hermit/io.rs | 6 - library/std/src/os/hermit/io/mod.rs | 10 ++ .../{sys/hermit/fd => os/hermit/io}/owned.rs | 116 +++++++++++------- .../{sys/hermit/fd => os/hermit/io}/raw.rs | 0 library/std/src/os/hermit/mod.rs | 5 + library/std/src/os/mod.rs | 12 +- library/std/src/sys/hermit/args.rs | 2 +- .../std/src/sys/hermit/{fd/mod.rs => fd.rs} | 7 +- library/std/src/sys/hermit/fs.rs | 8 +- library/std/src/sys/hermit/mod.rs | 4 +- library/std/src/sys/hermit/net.rs | 3 +- library/std/src/sys/hermit/os.rs | 2 +- 12 files changed, 102 insertions(+), 73 deletions(-) delete mode 100644 library/std/src/os/hermit/io.rs create mode 100644 library/std/src/os/hermit/io/mod.rs rename library/std/src/{sys/hermit/fd => os/hermit/io}/owned.rs (88%) rename library/std/src/{sys/hermit/fd => os/hermit/io}/raw.rs (100%) rename library/std/src/sys/hermit/{fd/mod.rs => fd.rs} (96%) diff --git a/library/std/src/os/hermit/io.rs b/library/std/src/os/hermit/io.rs deleted file mode 100644 index d8c741f7f4e..00000000000 --- a/library/std/src/os/hermit/io.rs +++ /dev/null @@ -1,6 +0,0 @@ -#![stable(feature = "rust1", since = "1.0.0")] - -use hermit_abi as abi; - -#[stable(feature = "rust1", since = "1.0.0")] -pub type RawFd = abi::FileDescriptor; diff --git a/library/std/src/os/hermit/io/mod.rs b/library/std/src/os/hermit/io/mod.rs new file mode 100644 index 00000000000..f2091672801 --- /dev/null +++ b/library/std/src/os/hermit/io/mod.rs @@ -0,0 +1,10 @@ +#![stable(feature = "os_fd", since = "1.66.0")] + +mod owned; +mod raw; + +// Export the types and traits for the public API. +#[stable(feature = "os_fd", since = "1.66.0")] +pub use owned::*; +#[stable(feature = "os_fd", since = "1.66.0")] +pub use raw::*; diff --git a/library/std/src/sys/hermit/fd/owned.rs b/library/std/src/os/hermit/io/owned.rs similarity index 88% rename from library/std/src/sys/hermit/fd/owned.rs rename to library/std/src/os/hermit/io/owned.rs index 7746cb3b259..4add29b1374 100644 --- a/library/std/src/sys/hermit/fd/owned.rs +++ b/library/std/src/os/hermit/io/owned.rs @@ -1,9 +1,8 @@ -use super::raw::RawFd; - +use super::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; +use crate::fmt; use crate::marker::PhantomData; use crate::mem::forget; -use crate::sys::fd::{AsRawFd, FromRawFd, IntoRawFd}; -use crate::sys::hermit::abi; +use crate::os::hermit::abi; use crate::sys_common::{AsInner, FromInner, IntoInner}; /// A borrowed file descriptor. @@ -49,7 +48,6 @@ pub struct BorrowedFd<'fd> { #[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, } @@ -71,6 +69,35 @@ impl BorrowedFd<'_> { } } +#[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); + } + } +} + +#[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() + } +} + +#[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() + } +} + #[stable(feature = "io_safety", since = "1.63.0")] impl AsRawFd for BorrowedFd<'_> { #[inline] @@ -113,14 +140,6 @@ impl FromRawFd for OwnedFd { } } -#[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 for OwnedFd { #[inline] @@ -139,14 +158,6 @@ impl From for crate::net::TcpStream { } } -#[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 for OwnedFd { #[inline] @@ -165,14 +176,6 @@ impl From for crate::net::TcpListener { } } -#[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 for OwnedFd { #[inline] @@ -191,21 +194,8 @@ impl From for crate::net::UdpSocket { } } +/// A trait to borrow the file descriptor from an underlying object. #[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. /// @@ -226,6 +216,22 @@ pub trait AsFd { fn as_fd(&self) -> BorrowedFd<'_>; } +#[stable(feature = "io_safety", since = "1.63.0")] +impl AsFd for &T { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + T::as_fd(self) + } +} + +#[stable(feature = "io_safety", since = "1.63.0")] +impl AsFd for &mut T { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + T::as_fd(self) + } +} + #[stable(feature = "io_safety", since = "1.63.0")] impl AsFd for OwnedFd { #[inline] @@ -236,3 +242,27 @@ impl AsFd for OwnedFd { unsafe { BorrowedFd::borrow_raw(self.as_raw_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 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 AsFd for crate::net::TcpStream { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + self.as_inner().socket().as_fd() + } +} diff --git a/library/std/src/sys/hermit/fd/raw.rs b/library/std/src/os/hermit/io/raw.rs similarity index 100% rename from library/std/src/sys/hermit/fd/raw.rs rename to library/std/src/os/hermit/io/raw.rs diff --git a/library/std/src/os/hermit/mod.rs b/library/std/src/os/hermit/mod.rs index 4657b545a1b..89b1b831912 100644 --- a/library/std/src/os/hermit/mod.rs +++ b/library/std/src/os/hermit/mod.rs @@ -1,6 +1,11 @@ #![stable(feature = "rust1", since = "1.0.0")] +#[allow(unused_extern_crates)] +#[stable(feature = "rust1", since = "1.0.0")] +pub extern crate hermit_abi as abi; + pub mod ffi; +pub mod io; /// A prelude for conveniently writing platform-specific code. /// diff --git a/library/std/src/os/mod.rs b/library/std/src/os/mod.rs index 42773805cdb..af137c9bd85 100644 --- a/library/std/src/os/mod.rs +++ b/library/std/src/os/mod.rs @@ -60,16 +60,6 @@ pub mod windows {} all(target_vendor = "fortanix", target_env = "sgx") ) )))] -#[cfg(target_os = "hermit")] -#[path = "hermit/mod.rs"] -pub mod unix; -#[cfg(not(all( - doc, - any( - all(target_arch = "wasm32", not(target_os = "wasi")), - all(target_vendor = "fortanix", target_env = "sgx") - ) -)))] #[cfg(all(not(target_os = "hermit"), any(unix, doc)))] pub mod unix; @@ -123,6 +113,8 @@ pub mod freebsd; pub mod fuchsia; #[cfg(target_os = "haiku")] pub mod haiku; +#[cfg(target_os = "hermit")] +pub mod hermit; #[cfg(target_os = "horizon")] pub mod horizon; #[cfg(target_os = "illumos")] 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 similarity index 96% rename from library/std/src/sys/hermit/fd/mod.rs rename to 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/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() -> crate::io::Result { 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;