Skip to content

Commit b5fb4f3

Browse files
committed
move IO traits to std/src/os/hermit
By moving the IO traits, the RustyHermit support is harmonized to of other operating systems.
1 parent 7143379 commit b5fb4f3

File tree

12 files changed

+102
-73
lines changed

12 files changed

+102
-73
lines changed

library/std/src/os/hermit/io.rs

-6
This file was deleted.

library/std/src/os/hermit/io/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![stable(feature = "os_fd", since = "1.66.0")]
2+
3+
mod owned;
4+
mod raw;
5+
6+
// Export the types and traits for the public API.
7+
#[stable(feature = "os_fd", since = "1.66.0")]
8+
pub use owned::*;
9+
#[stable(feature = "os_fd", since = "1.66.0")]
10+
pub use raw::*;

library/std/src/sys/hermit/fd/owned.rs renamed to library/std/src/os/hermit/io/owned.rs

+73-43
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use super::raw::RawFd;
2-
1+
use super::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
2+
use crate::fmt;
33
use crate::marker::PhantomData;
44
use crate::mem::forget;
5-
use crate::sys::fd::{AsRawFd, FromRawFd, IntoRawFd};
6-
use crate::sys::hermit::abi;
5+
use crate::os::hermit::abi;
76
use crate::sys_common::{AsInner, FromInner, IntoInner};
87

98
/// A borrowed file descriptor.
@@ -49,7 +48,6 @@ pub struct BorrowedFd<'fd> {
4948
#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
5049
#[rustc_nonnull_optimization_guaranteed]
5150
#[stable(feature = "io_safety", since = "1.63.0")]
52-
#[derive(Debug)]
5351
pub struct OwnedFd {
5452
fd: RawFd,
5553
}
@@ -71,6 +69,35 @@ impl BorrowedFd<'_> {
7169
}
7270
}
7371

72+
#[stable(feature = "io_safety", since = "1.63.0")]
73+
impl Drop for OwnedFd {
74+
#[inline]
75+
fn drop(&mut self) {
76+
unsafe {
77+
// Note that errors are ignored when closing a file descriptor. The
78+
// reason for this is that if an error occurs we don't actually know if
79+
// the file descriptor was closed or not, and if we retried (for
80+
// something like EINTR), we might close another valid file descriptor
81+
// opened after we closed ours.
82+
let _ = abi::close(self.fd);
83+
}
84+
}
85+
}
86+
87+
#[stable(feature = "io_safety", since = "1.63.0")]
88+
impl fmt::Debug for BorrowedFd<'_> {
89+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90+
f.debug_struct("BorrowedFd").field("fd", &self.fd).finish()
91+
}
92+
}
93+
94+
#[stable(feature = "io_safety", since = "1.63.0")]
95+
impl fmt::Debug for OwnedFd {
96+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97+
f.debug_struct("OwnedFd").field("fd", &self.fd).finish()
98+
}
99+
}
100+
74101
#[stable(feature = "io_safety", since = "1.63.0")]
75102
impl AsRawFd for BorrowedFd<'_> {
76103
#[inline]
@@ -113,14 +140,6 @@ impl FromRawFd for OwnedFd {
113140
}
114141
}
115142

116-
#[stable(feature = "io_safety", since = "1.63.0")]
117-
impl AsFd for crate::net::TcpStream {
118-
#[inline]
119-
fn as_fd(&self) -> BorrowedFd<'_> {
120-
self.as_inner().socket().as_fd()
121-
}
122-
}
123-
124143
#[stable(feature = "io_safety", since = "1.63.0")]
125144
impl From<crate::net::TcpStream> for OwnedFd {
126145
#[inline]
@@ -139,14 +158,6 @@ impl From<OwnedFd> for crate::net::TcpStream {
139158
}
140159
}
141160

142-
#[stable(feature = "io_safety", since = "1.63.0")]
143-
impl AsFd for crate::net::TcpListener {
144-
#[inline]
145-
fn as_fd(&self) -> BorrowedFd<'_> {
146-
self.as_inner().socket().as_fd()
147-
}
148-
}
149-
150161
#[stable(feature = "io_safety", since = "1.63.0")]
151162
impl From<crate::net::TcpListener> for OwnedFd {
152163
#[inline]
@@ -165,14 +176,6 @@ impl From<OwnedFd> for crate::net::TcpListener {
165176
}
166177
}
167178

168-
#[stable(feature = "io_safety", since = "1.63.0")]
169-
impl AsFd for crate::net::UdpSocket {
170-
#[inline]
171-
fn as_fd(&self) -> BorrowedFd<'_> {
172-
self.as_inner().socket().as_fd()
173-
}
174-
}
175-
176179
#[stable(feature = "io_safety", since = "1.63.0")]
177180
impl From<crate::net::UdpSocket> for OwnedFd {
178181
#[inline]
@@ -191,21 +194,8 @@ impl From<OwnedFd> for crate::net::UdpSocket {
191194
}
192195
}
193196

197+
/// A trait to borrow the file descriptor from an underlying object.
194198
#[stable(feature = "io_safety", since = "1.63.0")]
195-
impl Drop for OwnedFd {
196-
#[inline]
197-
fn drop(&mut self) {
198-
unsafe {
199-
// Note that errors are ignored when closing a file descriptor. The
200-
// reason for this is that if an error occurs we don't actually know if
201-
// the file descriptor was closed or not, and if we retried (for
202-
// something like EINTR), we might close another valid file descriptor
203-
// opened after we closed ours.
204-
let _ = abi::close(self.fd);
205-
}
206-
}
207-
}
208-
209199
pub trait AsFd {
210200
/// Borrows the file descriptor.
211201
///
@@ -226,6 +216,22 @@ pub trait AsFd {
226216
fn as_fd(&self) -> BorrowedFd<'_>;
227217
}
228218

219+
#[stable(feature = "io_safety", since = "1.63.0")]
220+
impl<T: AsFd> AsFd for &T {
221+
#[inline]
222+
fn as_fd(&self) -> BorrowedFd<'_> {
223+
T::as_fd(self)
224+
}
225+
}
226+
227+
#[stable(feature = "io_safety", since = "1.63.0")]
228+
impl<T: AsFd> AsFd for &mut T {
229+
#[inline]
230+
fn as_fd(&self) -> BorrowedFd<'_> {
231+
T::as_fd(self)
232+
}
233+
}
234+
229235
#[stable(feature = "io_safety", since = "1.63.0")]
230236
impl AsFd for OwnedFd {
231237
#[inline]
@@ -236,3 +242,27 @@ impl AsFd for OwnedFd {
236242
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
237243
}
238244
}
245+
246+
#[stable(feature = "io_safety", since = "1.63.0")]
247+
impl AsFd for crate::net::UdpSocket {
248+
#[inline]
249+
fn as_fd(&self) -> BorrowedFd<'_> {
250+
self.as_inner().socket().as_fd()
251+
}
252+
}
253+
254+
#[stable(feature = "io_safety", since = "1.63.0")]
255+
impl AsFd for crate::net::TcpListener {
256+
#[inline]
257+
fn as_fd(&self) -> BorrowedFd<'_> {
258+
self.as_inner().socket().as_fd()
259+
}
260+
}
261+
262+
#[stable(feature = "io_safety", since = "1.63.0")]
263+
impl AsFd for crate::net::TcpStream {
264+
#[inline]
265+
fn as_fd(&self) -> BorrowedFd<'_> {
266+
self.as_inner().socket().as_fd()
267+
}
268+
}

library/std/src/os/hermit/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#![stable(feature = "rust1", since = "1.0.0")]
22

3+
#[allow(unused_extern_crates)]
4+
#[stable(feature = "rust1", since = "1.0.0")]
5+
pub extern crate hermit_abi as abi;
6+
37
pub mod ffi;
8+
pub mod io;
49

510
/// A prelude for conveniently writing platform-specific code.
611
///

library/std/src/os/mod.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,6 @@ pub mod windows {}
6060
all(target_vendor = "fortanix", target_env = "sgx")
6161
)
6262
)))]
63-
#[cfg(target_os = "hermit")]
64-
#[path = "hermit/mod.rs"]
65-
pub mod unix;
66-
#[cfg(not(all(
67-
doc,
68-
any(
69-
all(target_arch = "wasm32", not(target_os = "wasi")),
70-
all(target_vendor = "fortanix", target_env = "sgx")
71-
)
72-
)))]
7363
#[cfg(all(not(target_os = "hermit"), any(unix, doc)))]
7464
pub mod unix;
7565

@@ -123,6 +113,8 @@ pub mod freebsd;
123113
pub mod fuchsia;
124114
#[cfg(target_os = "haiku")]
125115
pub mod haiku;
116+
#[cfg(target_os = "hermit")]
117+
pub mod hermit;
126118
#[cfg(target_os = "horizon")]
127119
pub mod horizon;
128120
#[cfg(target_os = "illumos")]

library/std/src/sys/hermit/args.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::ffi::{c_char, CStr, OsString};
22
use crate::fmt;
3-
use crate::os::unix::ffi::OsStringExt;
3+
use crate::os::hermit::ffi::OsStringExt;
44
use crate::ptr;
55
use crate::sync::atomic::{
66
AtomicIsize, AtomicPtr,

library/std/src/sys/hermit/fd/mod.rs renamed to library/std/src/sys/hermit/fd.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
#![unstable(reason = "not public", issue = "none", feature = "fd")]
22

3-
mod owned;
4-
mod raw;
5-
63
use crate::io::{self, Read};
4+
use crate::os::hermit::io::{FromRawFd, OwnedFd, RawFd};
75
use crate::sys::cvt;
86
use crate::sys::hermit::abi;
97
use crate::sys::unsupported;
108
use crate::sys_common::{AsInner, FromInner, IntoInner};
119

12-
pub use self::owned::*;
13-
pub use self::raw::*;
10+
use crate::os::hermit::io::*;
1411

1512
#[derive(Debug)]
1613
pub struct FileDesc {

library/std/src/sys/hermit/fs.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ use crate::fmt;
33
use crate::hash::{Hash, Hasher};
44
use crate::io::{self, Error, ErrorKind};
55
use crate::io::{BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
6+
use crate::os::hermit::io::FromRawFd;
67
use crate::path::{Path, PathBuf};
78
use crate::sys::common::small_c_string::run_path_with_cstr;
89
use crate::sys::cvt;
9-
use crate::sys::hermit::abi;
10-
use crate::sys::hermit::abi::{O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY};
11-
use crate::sys::hermit::fd::{FileDesc, FromRawFd};
10+
use crate::sys::hermit::abi::{
11+
self, O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY,
12+
};
13+
use crate::sys::hermit::fd::FileDesc;
1214
use crate::sys::time::SystemTime;
1315
use crate::sys::unsupported;
1416

library/std/src/sys/hermit/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ pub mod locks {
5757
}
5858

5959
use crate::io::ErrorKind;
60-
61-
#[allow(unused_extern_crates)]
62-
pub extern crate hermit_abi as abi;
60+
use crate::os::hermit::abi;
6361

6462
pub fn unsupported<T>() -> crate::io::Result<T> {
6563
Err(unsupported_err())

library/std/src/sys/hermit/net.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use crate::cmp;
44
use crate::io::{self, IoSlice, IoSliceMut};
55
use crate::mem;
66
use crate::net::{Shutdown, SocketAddr};
7-
use crate::sys::fd::{AsFd, AsRawFd, BorrowedFd, FileDesc, FromRawFd, RawFd};
7+
use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, RawFd};
8+
use crate::sys::hermit::fd::FileDesc;
89
use crate::sys::time::Instant;
910
use crate::sys_common::net::{getsockopt, setsockopt, sockaddr_to_addr};
1011
use crate::sys_common::{AsInner, FromInner, IntoInner};

library/std/src/sys/hermit/os.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::ffi::{CStr, OsStr, OsString};
44
use crate::fmt;
55
use crate::io;
66
use crate::marker::PhantomData;
7-
use crate::os::unix::ffi::OsStringExt;
7+
use crate::os::hermit::ffi::OsStringExt;
88
use crate::path::{self, PathBuf};
99
use crate::str;
1010
use crate::sync::Mutex;

0 commit comments

Comments
 (0)