Skip to content

Commit e7a37be

Browse files
joboetgitbot
authored and
gitbot
committed
std: move io module out of pal
1 parent 3365190 commit e7a37be

File tree

24 files changed

+81
-230
lines changed

24 files changed

+81
-230
lines changed

std/src/sys/pal/hermit/io.rs std/src/sys/io/io_slice/iovec.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
use hermit_abi::{c_void, iovec};
1+
#[cfg(target_os = "hermit")]
2+
use hermit_abi::iovec;
3+
#[cfg(target_family = "unix")]
4+
use libc::iovec;
25

6+
use crate::ffi::c_void;
37
use crate::marker::PhantomData;
4-
use crate::os::hermit::io::{AsFd, AsRawFd};
58
use crate::slice;
9+
#[cfg(target_os = "solid_asp3")]
10+
use crate::sys::pal::abi::sockets::iovec;
611

712
#[derive(Copy, Clone)]
813
#[repr(transparent)]
@@ -80,8 +85,3 @@ impl<'a> IoSliceMut<'a> {
8085
unsafe { slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len) }
8186
}
8287
}
83-
84-
pub fn is_terminal(fd: &impl AsFd) -> bool {
85-
let fd = fd.as_fd();
86-
hermit_abi::isatty(fd.as_raw_fd())
87-
}

std/src/sys/pal/unsupported/io.rs std/src/sys/io/io_slice/unsupported.rs

-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,3 @@ impl<'a> IoSliceMut<'a> {
5050
self.0
5151
}
5252
}
53-
54-
pub fn is_terminal<T>(_: &T) -> bool {
55-
false
56-
}

std/src/sys/pal/wasi/io.rs std/src/sys/io/io_slice/wasi.rs

-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
#![forbid(unsafe_op_in_unsafe_fn)]
2-
31
use crate::marker::PhantomData;
4-
use crate::os::fd::{AsFd, AsRawFd};
52
use crate::slice;
63

74
#[derive(Copy, Clone)]
@@ -77,8 +74,3 @@ impl<'a> IoSliceMut<'a> {
7774
unsafe { slice::from_raw_parts_mut(self.vec.buf as *mut u8, self.vec.buf_len) }
7875
}
7976
}
80-
81-
pub fn is_terminal(fd: &impl AsFd) -> bool {
82-
let fd = fd.as_fd();
83-
unsafe { libc::isatty(fd.as_raw_fd()) != 0 }
84-
}
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,82 @@
1-
use libc::c_void;
2-
3-
use super::abi::sockets::iovec;
41
use crate::marker::PhantomData;
52
use crate::slice;
3+
use crate::sys::c;
64

75
#[derive(Copy, Clone)]
86
#[repr(transparent)]
97
pub struct IoSlice<'a> {
10-
vec: iovec,
8+
vec: c::WSABUF,
119
_p: PhantomData<&'a [u8]>,
1210
}
1311

1412
impl<'a> IoSlice<'a> {
1513
#[inline]
1614
pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
15+
assert!(buf.len() <= u32::MAX as usize);
1716
IoSlice {
18-
vec: iovec { iov_base: buf.as_ptr() as *mut u8 as *mut c_void, iov_len: buf.len() },
17+
vec: c::WSABUF { len: buf.len() as u32, buf: buf.as_ptr() as *mut u8 },
1918
_p: PhantomData,
2019
}
2120
}
2221

2322
#[inline]
2423
pub fn advance(&mut self, n: usize) {
25-
if self.vec.iov_len < n {
24+
if (self.vec.len as usize) < n {
2625
panic!("advancing IoSlice beyond its length");
2726
}
2827

2928
unsafe {
30-
self.vec.iov_len -= n;
31-
self.vec.iov_base = self.vec.iov_base.add(n);
29+
self.vec.len -= n as u32;
30+
self.vec.buf = self.vec.buf.add(n);
3231
}
3332
}
3433

3534
#[inline]
3635
pub const fn as_slice(&self) -> &'a [u8] {
37-
unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len) }
36+
unsafe { slice::from_raw_parts(self.vec.buf, self.vec.len as usize) }
3837
}
3938
}
4039

4140
#[repr(transparent)]
4241
pub struct IoSliceMut<'a> {
43-
vec: iovec,
42+
vec: c::WSABUF,
4443
_p: PhantomData<&'a mut [u8]>,
4544
}
4645

4746
impl<'a> IoSliceMut<'a> {
4847
#[inline]
4948
pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
49+
assert!(buf.len() <= u32::MAX as usize);
5050
IoSliceMut {
51-
vec: iovec { iov_base: buf.as_mut_ptr() as *mut c_void, iov_len: buf.len() },
51+
vec: c::WSABUF { len: buf.len() as u32, buf: buf.as_mut_ptr() },
5252
_p: PhantomData,
5353
}
5454
}
5555

5656
#[inline]
5757
pub fn advance(&mut self, n: usize) {
58-
if self.vec.iov_len < n {
58+
if (self.vec.len as usize) < n {
5959
panic!("advancing IoSliceMut beyond its length");
6060
}
6161

6262
unsafe {
63-
self.vec.iov_len -= n;
64-
self.vec.iov_base = self.vec.iov_base.add(n);
63+
self.vec.len -= n as u32;
64+
self.vec.buf = self.vec.buf.add(n);
6565
}
6666
}
6767

6868
#[inline]
6969
pub fn as_slice(&self) -> &[u8] {
70-
unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len) }
70+
unsafe { slice::from_raw_parts(self.vec.buf, self.vec.len as usize) }
7171
}
7272

7373
#[inline]
7474
pub const fn into_slice(self) -> &'a mut [u8] {
75-
unsafe { slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len) }
75+
unsafe { slice::from_raw_parts_mut(self.vec.buf, self.vec.len as usize) }
7676
}
7777

7878
#[inline]
7979
pub fn as_mut_slice(&mut self) -> &mut [u8] {
80-
unsafe { slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len) }
80+
unsafe { slice::from_raw_parts_mut(self.vec.buf, self.vec.len as usize) }
8181
}
8282
}
83-
84-
pub fn is_terminal<T>(_: &T) -> bool {
85-
false
86-
}

std/src/sys/io/is_terminal/hermit.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use crate::os::fd::{AsFd, AsRawFd};
2+
3+
pub fn is_terminal(fd: &impl AsFd) -> bool {
4+
let fd = fd.as_fd();
5+
hermit_abi::isatty(fd.as_raw_fd())
6+
}

std/src/sys/io/is_terminal/isatty.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use crate::os::fd::{AsFd, AsRawFd};
2+
3+
pub fn is_terminal(fd: &impl AsFd) -> bool {
4+
let fd = fd.as_fd();
5+
unsafe { libc::isatty(fd.as_raw_fd()) != 0 }
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn is_terminal<T>(_: &T) -> bool {
2+
false
3+
}

std/src/sys/pal/windows/io.rs std/src/sys/io/is_terminal/windows.rs

+1-83
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,8 @@
1-
use core::ffi::c_void;
2-
3-
use crate::marker::PhantomData;
1+
use crate::ffi::c_void;
42
use crate::mem::size_of;
53
use crate::os::windows::io::{AsHandle, AsRawHandle, BorrowedHandle};
6-
use crate::slice;
74
use crate::sys::c;
85

9-
#[derive(Copy, Clone)]
10-
#[repr(transparent)]
11-
pub struct IoSlice<'a> {
12-
vec: c::WSABUF,
13-
_p: PhantomData<&'a [u8]>,
14-
}
15-
16-
impl<'a> IoSlice<'a> {
17-
#[inline]
18-
pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
19-
assert!(buf.len() <= u32::MAX as usize);
20-
IoSlice {
21-
vec: c::WSABUF { len: buf.len() as u32, buf: buf.as_ptr() as *mut u8 },
22-
_p: PhantomData,
23-
}
24-
}
25-
26-
#[inline]
27-
pub fn advance(&mut self, n: usize) {
28-
if (self.vec.len as usize) < n {
29-
panic!("advancing IoSlice beyond its length");
30-
}
31-
32-
unsafe {
33-
self.vec.len -= n as u32;
34-
self.vec.buf = self.vec.buf.add(n);
35-
}
36-
}
37-
38-
#[inline]
39-
pub const fn as_slice(&self) -> &'a [u8] {
40-
unsafe { slice::from_raw_parts(self.vec.buf, self.vec.len as usize) }
41-
}
42-
}
43-
44-
#[repr(transparent)]
45-
pub struct IoSliceMut<'a> {
46-
vec: c::WSABUF,
47-
_p: PhantomData<&'a mut [u8]>,
48-
}
49-
50-
impl<'a> IoSliceMut<'a> {
51-
#[inline]
52-
pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
53-
assert!(buf.len() <= u32::MAX as usize);
54-
IoSliceMut {
55-
vec: c::WSABUF { len: buf.len() as u32, buf: buf.as_mut_ptr() },
56-
_p: PhantomData,
57-
}
58-
}
59-
60-
#[inline]
61-
pub fn advance(&mut self, n: usize) {
62-
if (self.vec.len as usize) < n {
63-
panic!("advancing IoSliceMut beyond its length");
64-
}
65-
66-
unsafe {
67-
self.vec.len -= n as u32;
68-
self.vec.buf = self.vec.buf.add(n);
69-
}
70-
}
71-
72-
#[inline]
73-
pub fn as_slice(&self) -> &[u8] {
74-
unsafe { slice::from_raw_parts(self.vec.buf, self.vec.len as usize) }
75-
}
76-
77-
#[inline]
78-
pub const fn into_slice(self) -> &'a mut [u8] {
79-
unsafe { slice::from_raw_parts_mut(self.vec.buf, self.vec.len as usize) }
80-
}
81-
82-
#[inline]
83-
pub fn as_mut_slice(&mut self) -> &mut [u8] {
84-
unsafe { slice::from_raw_parts_mut(self.vec.buf, self.vec.len as usize) }
85-
}
86-
}
87-
886
pub fn is_terminal(h: &impl AsHandle) -> bool {
897
handle_is_console(h.as_handle())
908
}

std/src/sys/io/mod.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#![forbid(unsafe_op_in_unsafe_fn)]
2+
3+
mod io_slice {
4+
cfg_if::cfg_if! {
5+
if #[cfg(any(target_family = "unix", target_os = "hermit", target_os = "solid_asp3"))] {
6+
mod iovec;
7+
pub use iovec::*;
8+
} else if #[cfg(target_os = "windows")] {
9+
mod windows;
10+
pub use windows::*;
11+
} else if #[cfg(target_os = "wasi")] {
12+
mod wasi;
13+
pub use wasi::*;
14+
} else {
15+
mod unsupported;
16+
pub use unsupported::*;
17+
}
18+
}
19+
}
20+
21+
mod is_terminal {
22+
cfg_if::cfg_if! {
23+
if #[cfg(any(target_family = "unix", target_os = "wasi"))] {
24+
mod isatty;
25+
pub use isatty::*;
26+
} else if #[cfg(target_os = "windows")] {
27+
mod windows;
28+
pub use windows::*;
29+
} else if #[cfg(target_os = "hermit")] {
30+
mod hermit;
31+
pub use hermit::*;
32+
} else {
33+
mod unsupported;
34+
pub use unsupported::*;
35+
}
36+
}
37+
}
38+
39+
pub use io_slice::{IoSlice, IoSliceMut};
40+
pub use is_terminal::is_terminal;

std/src/sys/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod anonymous_pipe;
1212
pub mod backtrace;
1313
pub mod cmath;
1414
pub mod exit_guard;
15+
pub mod io;
1516
pub mod net;
1617
pub mod os_str;
1718
pub mod path;

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

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pub mod env;
2323
pub mod fd;
2424
pub mod fs;
2525
pub mod futex;
26-
pub mod io;
2726
pub mod os;
2827
#[path = "../unsupported/pipe.rs"]
2928
pub mod pipe;

std/src/sys/pal/sgx/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ pub mod env;
1414
pub mod fd;
1515
#[path = "../unsupported/fs.rs"]
1616
pub mod fs;
17-
#[path = "../unsupported/io.rs"]
18-
pub mod io;
1917
mod libunwind_integration;
2018
pub mod os;
2119
#[path = "../unsupported/pipe.rs"]

std/src/sys/pal/solid/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pub mod env;
2323
// `crate::sys::error`
2424
pub(crate) mod error;
2525
pub mod fs;
26-
pub mod io;
2726
pub mod os;
2827
#[path = "../unsupported/pipe.rs"]
2928
pub mod pipe;

std/src/sys/pal/teeos/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ pub mod env;
1313
//pub mod fd;
1414
#[path = "../unsupported/fs.rs"]
1515
pub mod fs;
16-
#[path = "../unsupported/io.rs"]
17-
pub mod io;
1816
pub mod os;
1917
#[path = "../unsupported/pipe.rs"]
2018
pub mod pipe;

std/src/sys/pal/uefi/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ pub mod args;
1717
pub mod env;
1818
pub mod fs;
1919
pub mod helpers;
20-
#[path = "../unsupported/io.rs"]
21-
pub mod io;
2220
pub mod os;
2321
#[path = "../unsupported/pipe.rs"]
2422
pub mod pipe;

0 commit comments

Comments
 (0)