Skip to content

Commit

Permalink
std: move io module out of pal
Browse files Browse the repository at this point in the history
  • Loading branch information
joboet committed Feb 7, 2025
1 parent 5ff18d0 commit a9df224
Show file tree
Hide file tree
Showing 24 changed files with 81 additions and 230 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use hermit_abi::{c_void, iovec};
#[cfg(target_os = "hermit")]
use hermit_abi::iovec;
#[cfg(target_family = "unix")]
use libc::iovec;

use crate::ffi::c_void;
use crate::marker::PhantomData;
use crate::os::hermit::io::{AsFd, AsRawFd};
use crate::slice;
#[cfg(target_os = "solid_asp3")]
use crate::sys::pal::abi::sockets::iovec;

#[derive(Copy, Clone)]
#[repr(transparent)]
Expand Down Expand Up @@ -80,8 +85,3 @@ impl<'a> IoSliceMut<'a> {
unsafe { slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len) }
}
}

pub fn is_terminal(fd: &impl AsFd) -> bool {
let fd = fd.as_fd();
hermit_abi::isatty(fd.as_raw_fd())
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,3 @@ impl<'a> IoSliceMut<'a> {
self.0
}
}

pub fn is_terminal<T>(_: &T) -> bool {
false
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#![forbid(unsafe_op_in_unsafe_fn)]

use crate::marker::PhantomData;
use crate::os::fd::{AsFd, AsRawFd};
use crate::slice;

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -77,8 +74,3 @@ impl<'a> IoSliceMut<'a> {
unsafe { slice::from_raw_parts_mut(self.vec.buf as *mut u8, self.vec.buf_len) }
}
}

pub fn is_terminal(fd: &impl AsFd) -> bool {
let fd = fd.as_fd();
unsafe { libc::isatty(fd.as_raw_fd()) != 0 }
}
Original file line number Diff line number Diff line change
@@ -1,86 +1,82 @@
use libc::c_void;

use super::abi::sockets::iovec;
use crate::marker::PhantomData;
use crate::slice;
use crate::sys::c;

#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct IoSlice<'a> {
vec: iovec,
vec: c::WSABUF,
_p: PhantomData<&'a [u8]>,
}

impl<'a> IoSlice<'a> {
#[inline]
pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
assert!(buf.len() <= u32::MAX as usize);
IoSlice {
vec: iovec { iov_base: buf.as_ptr() as *mut u8 as *mut c_void, iov_len: buf.len() },
vec: c::WSABUF { len: buf.len() as u32, buf: buf.as_ptr() as *mut u8 },
_p: PhantomData,
}
}

#[inline]
pub fn advance(&mut self, n: usize) {
if self.vec.iov_len < n {
if (self.vec.len as usize) < n {
panic!("advancing IoSlice beyond its length");
}

unsafe {
self.vec.iov_len -= n;
self.vec.iov_base = self.vec.iov_base.add(n);
self.vec.len -= n as u32;
self.vec.buf = self.vec.buf.add(n);
}
}

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

#[repr(transparent)]
pub struct IoSliceMut<'a> {
vec: iovec,
vec: c::WSABUF,
_p: PhantomData<&'a mut [u8]>,
}

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

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

unsafe {
self.vec.iov_len -= n;
self.vec.iov_base = self.vec.iov_base.add(n);
self.vec.len -= n as u32;
self.vec.buf = self.vec.buf.add(n);
}
}

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

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

#[inline]
pub fn as_mut_slice(&mut self) -> &mut [u8] {
unsafe { slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len) }
unsafe { slice::from_raw_parts_mut(self.vec.buf, self.vec.len as usize) }
}
}

pub fn is_terminal<T>(_: &T) -> bool {
false
}
6 changes: 6 additions & 0 deletions library/std/src/sys/io/is_terminal/hermit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::os::fd::{AsFd, AsRawFd};

pub fn is_terminal(fd: &impl AsFd) -> bool {
let fd = fd.as_fd();
hermit_abi::isatty(fd.as_raw_fd())
}
6 changes: 6 additions & 0 deletions library/std/src/sys/io/is_terminal/isatty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::os::fd::{AsFd, AsRawFd};

pub fn is_terminal(fd: &impl AsFd) -> bool {
let fd = fd.as_fd();
unsafe { libc::isatty(fd.as_raw_fd()) != 0 }
}
3 changes: 3 additions & 0 deletions library/std/src/sys/io/is_terminal/unsupported.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn is_terminal<T>(_: &T) -> bool {
false
}
Original file line number Diff line number Diff line change
@@ -1,90 +1,8 @@
use core::ffi::c_void;

use crate::marker::PhantomData;
use crate::ffi::c_void;
use crate::mem::size_of;
use crate::os::windows::io::{AsHandle, AsRawHandle, BorrowedHandle};
use crate::slice;
use crate::sys::c;

#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct IoSlice<'a> {
vec: c::WSABUF,
_p: PhantomData<&'a [u8]>,
}

impl<'a> IoSlice<'a> {
#[inline]
pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
assert!(buf.len() <= u32::MAX as usize);
IoSlice {
vec: c::WSABUF { len: buf.len() as u32, buf: buf.as_ptr() as *mut u8 },
_p: PhantomData,
}
}

#[inline]
pub fn advance(&mut self, n: usize) {
if (self.vec.len as usize) < n {
panic!("advancing IoSlice beyond its length");
}

unsafe {
self.vec.len -= n as u32;
self.vec.buf = self.vec.buf.add(n);
}
}

#[inline]
pub const fn as_slice(&self) -> &'a [u8] {
unsafe { slice::from_raw_parts(self.vec.buf, self.vec.len as usize) }
}
}

#[repr(transparent)]
pub struct IoSliceMut<'a> {
vec: c::WSABUF,
_p: PhantomData<&'a mut [u8]>,
}

impl<'a> IoSliceMut<'a> {
#[inline]
pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
assert!(buf.len() <= u32::MAX as usize);
IoSliceMut {
vec: c::WSABUF { len: buf.len() as u32, buf: buf.as_mut_ptr() },
_p: PhantomData,
}
}

#[inline]
pub fn advance(&mut self, n: usize) {
if (self.vec.len as usize) < n {
panic!("advancing IoSliceMut beyond its length");
}

unsafe {
self.vec.len -= n as u32;
self.vec.buf = self.vec.buf.add(n);
}
}

#[inline]
pub fn as_slice(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self.vec.buf, self.vec.len as usize) }
}

#[inline]
pub const fn into_slice(self) -> &'a mut [u8] {
unsafe { slice::from_raw_parts_mut(self.vec.buf, self.vec.len as usize) }
}

#[inline]
pub fn as_mut_slice(&mut self) -> &mut [u8] {
unsafe { slice::from_raw_parts_mut(self.vec.buf, self.vec.len as usize) }
}
}

pub fn is_terminal(h: &impl AsHandle) -> bool {
handle_is_console(h.as_handle())
}
Expand Down
40 changes: 40 additions & 0 deletions library/std/src/sys/io/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#![forbid(unsafe_op_in_unsafe_fn)]

mod io_slice {
cfg_if::cfg_if! {
if #[cfg(any(target_family = "unix", target_os = "hermit", target_os = "solid_asp3"))] {
mod iovec;
pub use iovec::*;
} else if #[cfg(target_os = "windows")] {
mod windows;
pub use windows::*;
} else if #[cfg(target_os = "wasi")] {
mod wasi;
pub use wasi::*;
} else {
mod unsupported;
pub use unsupported::*;
}
}
}

mod is_terminal {
cfg_if::cfg_if! {
if #[cfg(any(target_family = "unix", target_os = "wasi"))] {
mod isatty;
pub use isatty::*;
} else if #[cfg(target_os = "windows")] {
mod windows;
pub use windows::*;
} else if #[cfg(target_os = "hermit")] {
mod hermit;
pub use hermit::*;
} else {
mod unsupported;
pub use unsupported::*;
}
}
}

pub use io_slice::{IoSlice, IoSliceMut};
pub use is_terminal::is_terminal;
1 change: 1 addition & 0 deletions library/std/src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod anonymous_pipe;
pub mod backtrace;
pub mod cmath;
pub mod exit_guard;
pub mod io;
pub mod net;
pub mod os_str;
pub mod path;
Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/pal/hermit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub mod env;
pub mod fd;
pub mod fs;
pub mod futex;
pub mod io;
pub mod os;
#[path = "../unsupported/pipe.rs"]
pub mod pipe;
Expand Down
2 changes: 0 additions & 2 deletions library/std/src/sys/pal/sgx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ pub mod env;
pub mod fd;
#[path = "../unsupported/fs.rs"]
pub mod fs;
#[path = "../unsupported/io.rs"]
pub mod io;
mod libunwind_integration;
pub mod os;
#[path = "../unsupported/pipe.rs"]
Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/pal/solid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub mod env;
// `crate::sys::error`
pub(crate) mod error;
pub mod fs;
pub mod io;
pub mod os;
#[path = "../unsupported/pipe.rs"]
pub mod pipe;
Expand Down
2 changes: 0 additions & 2 deletions library/std/src/sys/pal/teeos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub mod env;
//pub mod fd;
#[path = "../unsupported/fs.rs"]
pub mod fs;
#[path = "../unsupported/io.rs"]
pub mod io;
pub mod os;
#[path = "../unsupported/pipe.rs"]
pub mod pipe;
Expand Down
2 changes: 0 additions & 2 deletions library/std/src/sys/pal/uefi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ pub mod args;
pub mod env;
pub mod fs;
pub mod helpers;
#[path = "../unsupported/io.rs"]
pub mod io;
pub mod os;
#[path = "../unsupported/pipe.rs"]
pub mod pipe;
Expand Down
Loading

0 comments on commit a9df224

Please sign in to comment.