diff options
| -rw-r--r-- | library/std/src/os/hermit/io.rs | 6 | ||||
| -rw-r--r-- | library/std/src/os/hermit/io/mod.rs | 10 | ||||
| -rw-r--r-- | library/std/src/os/hermit/io/owned.rs (renamed from library/std/src/sys/hermit/fd/owned.rs) | 116 | ||||
| -rw-r--r-- | library/std/src/os/hermit/io/raw.rs (renamed from library/std/src/sys/hermit/fd/raw.rs) | 0 | ||||
| -rw-r--r-- | library/std/src/os/hermit/mod.rs | 5 | ||||
| -rw-r--r-- | library/std/src/os/mod.rs | 12 | ||||
| -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/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 |
12 files changed, 102 insertions, 73 deletions
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 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, } @@ -72,6 +70,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] fn as_raw_fd(&self) -> RawFd { @@ -114,14 +141,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<crate::net::TcpStream> for OwnedFd { #[inline] fn from(tcp_stream: crate::net::TcpStream) -> OwnedFd { @@ -140,14 +159,6 @@ impl From<OwnedFd> 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<crate::net::TcpListener> for OwnedFd { #[inline] fn from(tcp_listener: crate::net::TcpListener) -> OwnedFd { @@ -166,14 +177,6 @@ impl From<OwnedFd> 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<crate::net::UdpSocket> for OwnedFd { #[inline] fn from(udp_socket: crate::net::UdpSocket) -> OwnedFd { @@ -191,21 +194,8 @@ impl From<OwnedFd> 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. /// @@ -227,6 +217,22 @@ pub trait AsFd { } #[stable(feature = "io_safety", since = "1.63.0")] +impl<T: AsFd> AsFd for &T { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + T::as_fd(self) + } +} + +#[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) + } +} + +#[stable(feature = "io_safety", since = "1.63.0")] impl AsFd for OwnedFd { #[inline] fn as_fd(&self) -> BorrowedFd<'_> { @@ -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 index cdec90b4b87..cdec90b4b87 100644 --- a/library/std/src/sys/hermit/fd/raw.rs +++ b/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 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<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; |
