Skip to content

Commit dd05273

Browse files
committed
add RustyHermit support
RusytHermit is a library operating system, which is completely written in Rust. It provides unix-like file descriptors. Consequently, the implementation has some similarites to the Unix support.
1 parent 2009f82 commit dd05273

File tree

9 files changed

+133
-100
lines changed

9 files changed

+133
-100
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ socket2 = { version = "0.4.0", optional = true }
4343
# Optionally depend on mio to implement traits for its types.
4444
mio = { version = "0.8.0", features = ["net", "os-ext"], optional = true }
4545

46+
[target.'cfg(target_os = "hermit")'.dependencies]
47+
hermit-abi = { version = "0.3", optional = true }
48+
4649
[target.'cfg(not(windows))'.dependencies]
4750
libc = { version = "0.2.96", optional = true }
4851

@@ -60,4 +63,4 @@ features = [
6063

6164
[features]
6265
default = ["close"]
63-
close = ["libc", "windows-sys"]
66+
close = ["libc", "hermit-abi", "windows-sys"]

src/example_ffi.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
#![cfg_attr(not(io_safety_is_in_std), allow(unused_imports))]
44
#![allow(missing_docs)]
55

6-
#[cfg(any(unix, target_os = "wasi"))]
6+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
77
use crate::{BorrowedFd, OwnedFd};
88
#[cfg(windows)]
99
use crate::{BorrowedHandle, HandleOrInvalid};
1010

11-
#[cfg(any(unix, target_os = "wasi"))]
11+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
1212
use libc::{c_char, c_int, c_void, size_t, ssize_t};
1313
#[cfg(windows)]
1414
use {
@@ -23,7 +23,10 @@ use {
2323
};
2424

2525
// Declare a few FFI functions ourselves, to show off the FFI ergonomics.
26-
#[cfg(all(io_safety_is_in_std, any(unix, target_os = "wasi")))]
26+
#[cfg(all(
27+
io_safety_is_in_std,
28+
any(unix, target_os = "wasi", target_os = "hermit")
29+
))]
2730
extern "C" {
2831
pub fn open(pathname: *const c_char, flags: c_int, ...) -> Option<OwnedFd>;
2932
}

src/impls_std.rs

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#![allow(deprecated)] // Don't warn on `IntoFd` and `FromFd` impls.
22

3-
#[cfg(any(unix, target_os = "wasi"))]
3+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
44
use crate::{AsFd, FromFd, IntoFd};
55
#[cfg(windows)]
66
use crate::{AsHandle, AsSocket, FromHandle, FromSocket, IntoHandle, IntoSocket};
7-
#[cfg(any(unix, target_os = "wasi"))]
7+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
88
use crate::{BorrowedFd, OwnedFd};
99
#[cfg(windows)]
1010
use crate::{BorrowedHandle, BorrowedSocket, HandleOrInvalid, OwnedHandle, OwnedSocket};
11+
#[cfg(target_os = "hermit")]
12+
use std::os::hermit::io::{AsRawFd, FromRawFd, IntoRawFd};
1113
#[cfg(unix)]
1214
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
1315
#[cfg(target_os = "wasi")]
@@ -17,7 +19,7 @@ use std::os::windows::io::{
1719
AsRawHandle, AsRawSocket, FromRawHandle, FromRawSocket, IntoRawHandle, IntoRawSocket,
1820
};
1921

20-
#[cfg(any(unix, target_os = "wasi"))]
22+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
2123
impl AsFd for BorrowedFd<'_> {
2224
#[inline]
2325
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -41,7 +43,7 @@ impl AsSocket for BorrowedSocket<'_> {
4143
}
4244
}
4345

44-
#[cfg(any(unix, target_os = "wasi"))]
46+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
4547
impl AsFd for OwnedFd {
4648
#[inline]
4749
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -65,7 +67,7 @@ impl AsSocket for OwnedSocket {
6567
}
6668
}
6769

68-
#[cfg(any(unix, target_os = "wasi"))]
70+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
6971
impl IntoFd for OwnedFd {
7072
#[inline]
7173
fn into_fd(self) -> OwnedFd {
@@ -89,7 +91,7 @@ impl IntoSocket for OwnedSocket {
8991
}
9092
}
9193

92-
#[cfg(any(unix, target_os = "wasi"))]
94+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
9395
impl FromFd for OwnedFd {
9496
#[inline]
9597
fn from_fd(owned: OwnedFd) -> Self {
@@ -129,7 +131,7 @@ impl From<OwnedHandle> for HandleOrInvalid {
129131
}
130132
}
131133

132-
#[cfg(any(unix, target_os = "wasi"))]
134+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
133135
impl AsFd for std::fs::File {
134136
#[inline]
135137
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -145,15 +147,15 @@ impl AsHandle for std::fs::File {
145147
}
146148
}
147149

148-
#[cfg(any(unix, target_os = "wasi"))]
150+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
149151
impl IntoFd for std::fs::File {
150152
#[inline]
151153
fn into_fd(self) -> OwnedFd {
152154
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
153155
}
154156
}
155157

156-
#[cfg(any(unix, target_os = "wasi"))]
158+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
157159
impl From<std::fs::File> for OwnedFd {
158160
#[inline]
159161
fn from(owned: std::fs::File) -> Self {
@@ -177,15 +179,15 @@ impl From<std::fs::File> for OwnedHandle {
177179
}
178180
}
179181

180-
#[cfg(any(unix, target_os = "wasi"))]
182+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
181183
impl FromFd for std::fs::File {
182184
#[inline]
183185
fn from_fd(owned: OwnedFd) -> Self {
184186
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
185187
}
186188
}
187189

188-
#[cfg(any(unix, target_os = "wasi"))]
190+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
189191
impl From<OwnedFd> for std::fs::File {
190192
#[inline]
191193
fn from(owned: OwnedFd) -> Self {
@@ -209,7 +211,7 @@ impl From<OwnedHandle> for std::fs::File {
209211
}
210212
}
211213

212-
#[cfg(any(unix, target_os = "wasi"))]
214+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
213215
impl AsFd for std::net::TcpStream {
214216
#[inline]
215217
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -225,15 +227,15 @@ impl AsSocket for std::net::TcpStream {
225227
}
226228
}
227229

228-
#[cfg(any(unix, target_os = "wasi"))]
230+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
229231
impl IntoFd for std::net::TcpStream {
230232
#[inline]
231233
fn into_fd(self) -> OwnedFd {
232234
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
233235
}
234236
}
235237

236-
#[cfg(any(unix, target_os = "wasi"))]
238+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
237239
impl From<std::net::TcpStream> for OwnedFd {
238240
#[inline]
239241
fn from(owned: std::net::TcpStream) -> Self {
@@ -257,15 +259,15 @@ impl From<std::net::TcpStream> for OwnedSocket {
257259
}
258260
}
259261

260-
#[cfg(any(unix, target_os = "wasi"))]
262+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
261263
impl FromFd for std::net::TcpStream {
262264
#[inline]
263265
fn from_fd(owned: OwnedFd) -> Self {
264266
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
265267
}
266268
}
267269

268-
#[cfg(any(unix, target_os = "wasi"))]
270+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
269271
impl From<OwnedFd> for std::net::TcpStream {
270272
#[inline]
271273
fn from(owned: OwnedFd) -> Self {
@@ -289,7 +291,7 @@ impl From<OwnedSocket> for std::net::TcpStream {
289291
}
290292
}
291293

292-
#[cfg(any(unix, target_os = "wasi"))]
294+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
293295
impl AsFd for std::net::TcpListener {
294296
#[inline]
295297
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -305,15 +307,15 @@ impl AsSocket for std::net::TcpListener {
305307
}
306308
}
307309

308-
#[cfg(any(unix, target_os = "wasi"))]
310+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
309311
impl IntoFd for std::net::TcpListener {
310312
#[inline]
311313
fn into_fd(self) -> OwnedFd {
312314
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
313315
}
314316
}
315317

316-
#[cfg(any(unix, target_os = "wasi"))]
318+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
317319
impl From<std::net::TcpListener> for OwnedFd {
318320
#[inline]
319321
fn from(owned: std::net::TcpListener) -> Self {
@@ -337,15 +339,15 @@ impl From<std::net::TcpListener> for OwnedSocket {
337339
}
338340
}
339341

340-
#[cfg(any(unix, target_os = "wasi"))]
342+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
341343
impl FromFd for std::net::TcpListener {
342344
#[inline]
343345
fn from_fd(owned: OwnedFd) -> Self {
344346
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
345347
}
346348
}
347349

348-
#[cfg(any(unix, target_os = "wasi"))]
350+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
349351
impl From<OwnedFd> for std::net::TcpListener {
350352
#[inline]
351353
fn from(owned: OwnedFd) -> Self {
@@ -369,7 +371,7 @@ impl From<OwnedSocket> for std::net::TcpListener {
369371
}
370372
}
371373

372-
#[cfg(any(unix, target_os = "wasi"))]
374+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
373375
impl AsFd for std::net::UdpSocket {
374376
#[inline]
375377
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -385,15 +387,15 @@ impl AsSocket for std::net::UdpSocket {
385387
}
386388
}
387389

388-
#[cfg(any(unix, target_os = "wasi"))]
390+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
389391
impl IntoFd for std::net::UdpSocket {
390392
#[inline]
391393
fn into_fd(self) -> OwnedFd {
392394
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
393395
}
394396
}
395397

396-
#[cfg(any(unix, target_os = "wasi"))]
398+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
397399
impl From<std::net::UdpSocket> for OwnedFd {
398400
#[inline]
399401
fn from(owned: std::net::UdpSocket) -> Self {
@@ -417,15 +419,15 @@ impl From<std::net::UdpSocket> for OwnedSocket {
417419
}
418420
}
419421

420-
#[cfg(any(unix, target_os = "wasi"))]
422+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
421423
impl FromFd for std::net::UdpSocket {
422424
#[inline]
423425
fn from_fd(owned: OwnedFd) -> Self {
424426
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
425427
}
426428
}
427429

428-
#[cfg(any(unix, target_os = "wasi"))]
430+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
429431
impl From<OwnedFd> for std::net::UdpSocket {
430432
#[inline]
431433
fn from(owned: OwnedFd) -> Self {
@@ -449,7 +451,7 @@ impl From<OwnedSocket> for std::net::UdpSocket {
449451
}
450452
}
451453

452-
#[cfg(any(unix, target_os = "wasi"))]
454+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
453455
impl AsFd for std::io::Stdin {
454456
#[inline]
455457
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -465,7 +467,7 @@ impl AsHandle for std::io::Stdin {
465467
}
466468
}
467469

468-
#[cfg(any(unix, target_os = "wasi"))]
470+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
469471
impl<'a> AsFd for std::io::StdinLock<'a> {
470472
#[inline]
471473
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -481,7 +483,7 @@ impl<'a> AsHandle for std::io::StdinLock<'a> {
481483
}
482484
}
483485

484-
#[cfg(any(unix, target_os = "wasi"))]
486+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
485487
impl AsFd for std::io::Stdout {
486488
#[inline]
487489
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -497,7 +499,7 @@ impl AsHandle for std::io::Stdout {
497499
}
498500
}
499501

500-
#[cfg(any(unix, target_os = "wasi"))]
502+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
501503
impl<'a> AsFd for std::io::StdoutLock<'a> {
502504
#[inline]
503505
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -513,7 +515,7 @@ impl<'a> AsHandle for std::io::StdoutLock<'a> {
513515
}
514516
}
515517

516-
#[cfg(any(unix, target_os = "wasi"))]
518+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
517519
impl AsFd for std::io::Stderr {
518520
#[inline]
519521
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -529,7 +531,7 @@ impl AsHandle for std::io::Stderr {
529531
}
530532
}
531533

532-
#[cfg(any(unix, target_os = "wasi"))]
534+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
533535
impl<'a> AsFd for std::io::StderrLock<'a> {
534536
#[inline]
535537
fn as_fd(&self) -> BorrowedFd<'_> {

src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
// Work around https://github.com/rust-lang/rust/issues/103306.
3232
#![cfg_attr(all(wasi_ext, target_os = "wasi"), feature(wasi_ext))]
3333
// Currently supported platforms.
34-
#![cfg(any(unix, windows, target_os = "wasi"))]
34+
#![cfg(any(unix, windows, target_os = "wasi", target_os = "hermit"))]
3535

3636
mod portability;
3737
mod traits;
@@ -42,20 +42,20 @@ mod types;
4242
mod impls_std;
4343

4444
#[cfg(not(io_safety_is_in_std))]
45-
#[cfg(any(unix, target_os = "wasi"))]
45+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
4646
pub use traits::AsFd;
4747
#[cfg(not(io_safety_is_in_std))]
4848
#[cfg(windows)]
4949
pub use traits::{AsHandle, AsSocket};
50-
#[cfg(any(unix, target_os = "wasi"))]
50+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
5151
#[allow(deprecated)]
5252
pub use traits::{FromFd, IntoFd};
5353
#[cfg(windows)]
5454
#[allow(deprecated)]
5555
pub use traits::{FromHandle, FromSocket, IntoHandle, IntoSocket};
5656

5757
#[cfg(not(io_safety_is_in_std))]
58-
#[cfg(any(unix, target_os = "wasi"))]
58+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
5959
pub use types::{BorrowedFd, OwnedFd};
6060
#[cfg(not(io_safety_is_in_std))]
6161
#[cfg(windows)]
@@ -64,6 +64,9 @@ pub use types::{
6464
OwnedHandle, OwnedSocket,
6565
};
6666

67+
#[cfg(io_safety_is_in_std)]
68+
#[cfg(target_os = "hermit")]
69+
pub use std::os::hermit::io::{AsFd, BorrowedFd, OwnedFd};
6770
#[cfg(io_safety_is_in_std)]
6871
#[cfg(unix)]
6972
pub use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd};
@@ -89,7 +92,7 @@ pub use std::os::windows::io::{
8992
// So we define `FromFd`/`IntoFd` traits, and implement them in terms of
9093
// `From`/`Into`,
9194
#[cfg(io_safety_is_in_std)]
92-
#[cfg(any(unix, target_os = "wasi"))]
95+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
9396
#[allow(deprecated)]
9497
impl<T: From<OwnedFd>> FromFd for T {
9598
#[inline]
@@ -98,7 +101,7 @@ impl<T: From<OwnedFd>> FromFd for T {
98101
}
99102
}
100103
#[cfg(io_safety_is_in_std)]
101-
#[cfg(any(unix, target_os = "wasi"))]
104+
#[cfg(any(unix, target_os = "wasi", target_os = "hermit"))]
102105
#[allow(deprecated)]
103106
impl<T> IntoFd for T
104107
where

0 commit comments

Comments
 (0)