Skip to content

Commit 90162a7

Browse files
committed
remove code duplications
1 parent b5fb4f3 commit 90162a7

File tree

8 files changed

+129
-393
lines changed

8 files changed

+129
-393
lines changed

library/std/src/os/fd/owned.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::fs;
99
use crate::io;
1010
use crate::marker::PhantomData;
1111
use crate::mem::forget;
12-
#[cfg(not(any(target_arch = "wasm32", target_env = "sgx")))]
12+
#[cfg(not(any(target_arch = "wasm32", target_env = "sgx", target_os = "hermit")))]
1313
use crate::sys::cvt;
1414
use crate::sys_common::{AsInner, FromInner, IntoInner};
1515

@@ -89,7 +89,7 @@ impl OwnedFd {
8989
impl BorrowedFd<'_> {
9090
/// Creates a new `OwnedFd` instance that shares the same underlying file
9191
/// description as the existing `BorrowedFd` instance.
92-
#[cfg(not(target_arch = "wasm32"))]
92+
#[cfg(not(any(target_arch = "wasm32", target_os = "hermit")))]
9393
#[stable(feature = "io_safety", since = "1.63.0")]
9494
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
9595
// We want to atomically duplicate this file descriptor and set the
@@ -112,7 +112,7 @@ impl BorrowedFd<'_> {
112112

113113
/// Creates a new `OwnedFd` instance that shares the same underlying file
114114
/// description as the existing `BorrowedFd` instance.
115-
#[cfg(target_arch = "wasm32")]
115+
#[cfg(any(target_arch = "wasm32", target_os = "hermit"))]
116116
#[stable(feature = "io_safety", since = "1.63.0")]
117117
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
118118
Err(crate::io::const_io_error!(
@@ -174,7 +174,10 @@ impl Drop for OwnedFd {
174174
// the file descriptor was closed or not, and if we retried (for
175175
// something like EINTR), we might close another valid file descriptor
176176
// opened after we closed ours.
177+
#[cfg(not(target_os = "hermit"))]
177178
let _ = libc::close(self.fd);
179+
#[cfg(target_os = "hermit")]
180+
let _ = hermit_abi::close(self.fd);
178181
}
179182
}
180183
}

library/std/src/os/fd/raw.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use crate::fs;
66
use crate::io;
7+
#[cfg(target_os = "hermit")]
8+
use crate::os::hermit::io::OwnedFd;
9+
#[cfg(not(target_os = "hermit"))]
710
use crate::os::raw;
811
#[cfg(all(doc, not(target_arch = "wasm32")))]
912
use crate::os::unix::io::AsFd;
@@ -12,11 +15,20 @@ use crate::os::unix::io::OwnedFd;
1215
#[cfg(target_os = "wasi")]
1316
use crate::os::wasi::io::OwnedFd;
1417
use crate::sys_common::{AsInner, IntoInner};
18+
#[cfg(target_os = "hermit")]
19+
use hermit_abi::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
20+
#[cfg(not(target_os = "hermit"))]
21+
use libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
1522

1623
/// Raw file descriptors.
1724
#[rustc_allowed_through_unstable_modules]
1825
#[stable(feature = "rust1", since = "1.0.0")]
26+
#[cfg(not(target_os = "hermit"))]
1927
pub type RawFd = raw::c_int;
28+
#[rustc_allowed_through_unstable_modules]
29+
#[stable(feature = "rust1", since = "1.0.0")]
30+
#[cfg(target_os = "hermit")]
31+
pub type RawFd = i32;
2032

2133
/// A trait to extract the raw file descriptor from an underlying object.
2234
///
@@ -177,47 +189,47 @@ impl IntoRawFd for fs::File {
177189
impl AsRawFd for io::Stdin {
178190
#[inline]
179191
fn as_raw_fd(&self) -> RawFd {
180-
libc::STDIN_FILENO
192+
STDIN_FILENO
181193
}
182194
}
183195

184196
#[stable(feature = "asraw_stdio", since = "1.21.0")]
185197
impl AsRawFd for io::Stdout {
186198
#[inline]
187199
fn as_raw_fd(&self) -> RawFd {
188-
libc::STDOUT_FILENO
200+
STDOUT_FILENO
189201
}
190202
}
191203

192204
#[stable(feature = "asraw_stdio", since = "1.21.0")]
193205
impl AsRawFd for io::Stderr {
194206
#[inline]
195207
fn as_raw_fd(&self) -> RawFd {
196-
libc::STDERR_FILENO
208+
STDERR_FILENO
197209
}
198210
}
199211

200212
#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
201213
impl<'a> AsRawFd for io::StdinLock<'a> {
202214
#[inline]
203215
fn as_raw_fd(&self) -> RawFd {
204-
libc::STDIN_FILENO
216+
STDIN_FILENO
205217
}
206218
}
207219

208220
#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
209221
impl<'a> AsRawFd for io::StdoutLock<'a> {
210222
#[inline]
211223
fn as_raw_fd(&self) -> RawFd {
212-
libc::STDOUT_FILENO
224+
STDOUT_FILENO
213225
}
214226
}
215227

216228
#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
217229
impl<'a> AsRawFd for io::StderrLock<'a> {
218230
#[inline]
219231
fn as_raw_fd(&self) -> RawFd {
220-
libc::STDERR_FILENO
232+
STDERR_FILENO
221233
}
222234
}
223235

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

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#![stable(feature = "os_fd", since = "1.66.0")]
22

3+
mod net;
4+
#[path = "../../fd/owned.rs"]
35
mod owned;
6+
#[path = "../../fd/raw.rs"]
47
mod raw;
58

69
// Export the types and traits for the public API.

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

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use crate::os::hermit::io::OwnedFd;
2+
use crate::os::hermit::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
3+
use crate::sys_common::{self, AsInner, FromInner, IntoInner};
4+
use crate::{net, sys};
5+
6+
macro_rules! impl_as_raw_fd {
7+
($($t:ident)*) => {$(
8+
#[stable(feature = "rust1", since = "1.0.0")]
9+
impl AsRawFd for net::$t {
10+
#[inline]
11+
fn as_raw_fd(&self) -> RawFd {
12+
self.as_inner().socket().as_raw_fd()
13+
}
14+
}
15+
)*};
16+
}
17+
impl_as_raw_fd! { TcpStream TcpListener UdpSocket }
18+
19+
macro_rules! impl_from_raw_fd {
20+
($($t:ident)*) => {$(
21+
#[stable(feature = "from_raw_os", since = "1.1.0")]
22+
impl FromRawFd for net::$t {
23+
#[inline]
24+
unsafe fn from_raw_fd(fd: RawFd) -> net::$t {
25+
unsafe {
26+
let socket = sys::net::Socket::from_inner(FromInner::from_inner(OwnedFd::from_raw_fd(fd)));
27+
net::$t::from_inner(sys_common::net::$t::from_inner(socket))
28+
}
29+
}
30+
}
31+
)*};
32+
}
33+
impl_from_raw_fd! { TcpStream TcpListener UdpSocket }
34+
35+
macro_rules! impl_into_raw_fd {
36+
($($t:ident)*) => {$(
37+
#[stable(feature = "into_raw_os", since = "1.4.0")]
38+
impl IntoRawFd for net::$t {
39+
#[inline]
40+
fn into_raw_fd(self) -> RawFd {
41+
self.into_inner().into_socket().into_inner().into_inner().into_raw_fd()
42+
}
43+
}
44+
)*};
45+
}
46+
impl_into_raw_fd! { TcpStream TcpListener UdpSocket }

0 commit comments

Comments
 (0)