mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-16 14:07:04 +00:00
Auto merge of #136780 - joboet:move_pal_stdio, r=Amanieu
std: move stdio to `sys` As per #117276, this moves the platform definitions of `Stdout` and friends into `sys`. This PR also unifies the UNIX and Hermit implementations and moves the `__rust_print_err` function needed by libunwind on SGX into the dedicated module for such helper functions.
This commit is contained in:
commit
2c6a12ec44
@ -18,6 +18,7 @@ pub mod net;
|
||||
pub mod os_str;
|
||||
pub mod path;
|
||||
pub mod random;
|
||||
pub mod stdio;
|
||||
pub mod sync;
|
||||
pub mod thread_local;
|
||||
|
||||
|
@ -27,7 +27,6 @@ pub mod os;
|
||||
pub mod pipe;
|
||||
#[path = "../unsupported/process.rs"]
|
||||
pub mod process;
|
||||
pub mod stdio;
|
||||
pub mod thread;
|
||||
pub mod time;
|
||||
|
||||
|
@ -1,97 +0,0 @@
|
||||
use super::hermit_abi;
|
||||
use crate::io;
|
||||
use crate::io::{IoSlice, IoSliceMut};
|
||||
use crate::mem::ManuallyDrop;
|
||||
use crate::os::hermit::io::FromRawFd;
|
||||
use crate::sys::fd::FileDesc;
|
||||
|
||||
pub struct Stdin;
|
||||
pub struct Stdout;
|
||||
pub struct Stderr;
|
||||
|
||||
impl Stdin {
|
||||
pub const fn new() -> Stdin {
|
||||
Stdin
|
||||
}
|
||||
}
|
||||
|
||||
impl io::Read for Stdin {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(hermit_abi::STDIN_FILENO)).read(buf) }
|
||||
}
|
||||
|
||||
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
||||
unsafe {
|
||||
ManuallyDrop::new(FileDesc::from_raw_fd(hermit_abi::STDIN_FILENO)).read_vectored(bufs)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_read_vectored(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl Stdout {
|
||||
pub const fn new() -> Stdout {
|
||||
Stdout
|
||||
}
|
||||
}
|
||||
|
||||
impl io::Write for Stdout {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(hermit_abi::STDOUT_FILENO)).write(buf) }
|
||||
}
|
||||
|
||||
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
||||
unsafe {
|
||||
ManuallyDrop::new(FileDesc::from_raw_fd(hermit_abi::STDOUT_FILENO)).write_vectored(bufs)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_write_vectored(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Stderr {
|
||||
pub const fn new() -> Stderr {
|
||||
Stderr
|
||||
}
|
||||
}
|
||||
|
||||
impl io::Write for Stderr {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(hermit_abi::STDERR_FILENO)).write(buf) }
|
||||
}
|
||||
|
||||
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
||||
unsafe {
|
||||
ManuallyDrop::new(FileDesc::from_raw_fd(hermit_abi::STDERR_FILENO)).write_vectored(bufs)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_write_vectored(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub const STDIN_BUF_SIZE: usize = 128;
|
||||
|
||||
pub fn is_ebadf(err: &io::Error) -> bool {
|
||||
err.raw_os_error() == Some(hermit_abi::EBADF)
|
||||
}
|
||||
|
||||
pub fn panic_output() -> Option<impl io::Write> {
|
||||
Some(Stderr::new())
|
||||
}
|
@ -6,7 +6,7 @@ use core::sync::atomic::{AtomicUsize, Ordering};
|
||||
use crate::io::Write;
|
||||
|
||||
// runtime features
|
||||
pub(super) mod panic;
|
||||
pub mod panic;
|
||||
mod reloc;
|
||||
|
||||
// library features
|
||||
|
@ -4,6 +4,7 @@
|
||||
#![cfg(not(test))]
|
||||
|
||||
use crate::sys::sync::RwLock;
|
||||
use crate::{slice, str};
|
||||
|
||||
// Verify that the byte pattern libunwind uses to initialize an RwLock is
|
||||
// equivalent to the value of RwLock::new(). If the value changes,
|
||||
@ -44,3 +45,14 @@ pub unsafe extern "C" fn __rust_rwlock_unlock(p: *mut RwLock) -> i32 {
|
||||
unsafe { (*p).write_unlock() };
|
||||
return 0;
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn __rust_print_err(m: *mut u8, s: i32) {
|
||||
if s < 0 {
|
||||
return;
|
||||
}
|
||||
let buf = unsafe { slice::from_raw_parts(m as *const u8, s as _) };
|
||||
if let Ok(s) = str::from_utf8(&buf[..buf.iter().position(|&b| b == 0).unwrap_or(buf.len())]) {
|
||||
eprint!("{s}");
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,6 @@ pub mod os;
|
||||
pub mod pipe;
|
||||
#[path = "../unsupported/process.rs"]
|
||||
pub mod process;
|
||||
pub mod stdio;
|
||||
pub mod thread;
|
||||
pub mod thread_parking;
|
||||
pub mod time;
|
||||
|
@ -27,7 +27,6 @@ pub mod os;
|
||||
pub mod pipe;
|
||||
#[path = "../unsupported/process.rs"]
|
||||
pub mod process;
|
||||
pub mod stdio;
|
||||
pub use self::itron::{thread, thread_parking};
|
||||
pub mod time;
|
||||
|
||||
|
@ -16,7 +16,6 @@ pub mod os;
|
||||
pub mod pipe;
|
||||
#[path = "../unsupported/process.rs"]
|
||||
pub mod process;
|
||||
pub mod stdio;
|
||||
pub mod thread;
|
||||
#[allow(non_upper_case_globals)]
|
||||
#[path = "../unix/time.rs"]
|
||||
|
@ -20,7 +20,6 @@ pub mod os;
|
||||
#[path = "../unsupported/pipe.rs"]
|
||||
pub mod pipe;
|
||||
pub mod process;
|
||||
pub mod stdio;
|
||||
pub mod thread;
|
||||
pub mod time;
|
||||
|
||||
|
@ -18,7 +18,6 @@ pub mod os;
|
||||
pub mod pipe;
|
||||
pub mod process;
|
||||
pub mod stack_overflow;
|
||||
pub mod stdio;
|
||||
pub mod sync;
|
||||
pub mod thread;
|
||||
pub mod thread_parking;
|
||||
|
@ -5,7 +5,6 @@ pub mod env;
|
||||
pub mod os;
|
||||
pub mod pipe;
|
||||
pub mod process;
|
||||
pub mod stdio;
|
||||
pub mod thread;
|
||||
pub mod time;
|
||||
|
||||
|
@ -25,7 +25,6 @@ pub mod os;
|
||||
pub mod pipe;
|
||||
#[path = "../unsupported/process.rs"]
|
||||
pub mod process;
|
||||
pub mod stdio;
|
||||
pub mod thread;
|
||||
pub mod time;
|
||||
|
||||
|
@ -22,8 +22,6 @@ pub mod os;
|
||||
pub mod pipe;
|
||||
#[path = "../unsupported/process.rs"]
|
||||
pub mod process;
|
||||
#[path = "../wasi/stdio.rs"]
|
||||
pub mod stdio;
|
||||
#[path = "../wasi/thread.rs"]
|
||||
pub mod thread;
|
||||
#[path = "../wasi/time.rs"]
|
||||
|
@ -25,8 +25,6 @@ pub mod os;
|
||||
pub mod pipe;
|
||||
#[path = "../unsupported/process.rs"]
|
||||
pub mod process;
|
||||
#[path = "../unsupported/stdio.rs"]
|
||||
pub mod stdio;
|
||||
#[path = "../unsupported/time.rs"]
|
||||
pub mod time;
|
||||
|
||||
|
@ -23,7 +23,6 @@ pub mod handle;
|
||||
pub mod os;
|
||||
pub mod pipe;
|
||||
pub mod process;
|
||||
pub mod stdio;
|
||||
pub mod thread;
|
||||
pub mod time;
|
||||
cfg_if::cfg_if! {
|
||||
|
@ -8,7 +8,6 @@ pub mod os;
|
||||
pub mod pipe;
|
||||
#[path = "../unsupported/process.rs"]
|
||||
pub mod process;
|
||||
pub mod stdio;
|
||||
pub mod thread;
|
||||
pub mod time;
|
||||
|
||||
|
@ -19,7 +19,6 @@ pub mod os;
|
||||
pub mod pipe;
|
||||
#[path = "../unsupported/process.rs"]
|
||||
pub mod process;
|
||||
pub mod stdio;
|
||||
#[path = "../unsupported/thread.rs"]
|
||||
pub mod thread;
|
||||
#[path = "../unsupported/time.rs"]
|
||||
|
38
library/std/src/sys/stdio/mod.rs
Normal file
38
library/std/src/sys/stdio/mod.rs
Normal file
@ -0,0 +1,38 @@
|
||||
#![forbid(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(any(
|
||||
target_family = "unix",
|
||||
target_os = "hermit"
|
||||
))] {
|
||||
mod unix;
|
||||
pub use unix::*;
|
||||
} else if #[cfg(target_os = "windows")] {
|
||||
mod windows;
|
||||
pub use windows::*;
|
||||
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
|
||||
mod sgx;
|
||||
pub use sgx::*;
|
||||
} else if #[cfg(target_os = "solid_asp3")] {
|
||||
mod solid;
|
||||
pub use solid::*;
|
||||
} else if #[cfg(target_os = "teeos")] {
|
||||
mod teeos;
|
||||
pub use teeos::*;
|
||||
} else if #[cfg(target_os = "uefi")] {
|
||||
mod uefi;
|
||||
pub use uefi::*;
|
||||
} else if #[cfg(target_os = "wasi")] {
|
||||
mod wasi;
|
||||
pub use wasi::*;
|
||||
} else if #[cfg(target_os = "xous")] {
|
||||
mod xous;
|
||||
pub use xous::*;
|
||||
} else if #[cfg(target_os = "zkvm")] {
|
||||
mod zkvm;
|
||||
pub use zkvm::*;
|
||||
} else {
|
||||
mod unsupported;
|
||||
pub use unsupported::*;
|
||||
}
|
||||
}
|
@ -1,10 +1,6 @@
|
||||
use fortanix_sgx_abi as abi;
|
||||
|
||||
use crate::io;
|
||||
#[cfg(not(test))]
|
||||
use crate::slice;
|
||||
#[cfg(not(test))]
|
||||
use crate::str;
|
||||
use crate::sys::fd::FileDesc;
|
||||
|
||||
pub struct Stdin(());
|
||||
@ -70,19 +66,5 @@ pub fn is_ebadf(err: &io::Error) -> bool {
|
||||
}
|
||||
|
||||
pub fn panic_output() -> Option<impl io::Write> {
|
||||
super::abi::panic::SgxPanicOutput::new()
|
||||
}
|
||||
|
||||
// This function is needed by libunwind. The symbol is named in pre-link args
|
||||
// for the target specification, so keep that in sync.
|
||||
#[cfg(not(test))]
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn __rust_print_err(m: *mut u8, s: i32) {
|
||||
if s < 0 {
|
||||
return;
|
||||
}
|
||||
let buf = unsafe { slice::from_raw_parts(m as *const u8, s as _) };
|
||||
if let Ok(s) = str::from_utf8(&buf[..buf.iter().position(|&b| b == 0).unwrap_or(buf.len())]) {
|
||||
eprint!("{s}");
|
||||
}
|
||||
crate::sys::pal::abi::panic::SgxPanicOutput::new()
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
use super::abi;
|
||||
use crate::io;
|
||||
use crate::sys::pal::abi;
|
||||
|
||||
pub struct Stdin;
|
||||
pub struct Stdout;
|
@ -1,5 +1,15 @@
|
||||
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
|
||||
#[cfg(target_os = "hermit")]
|
||||
use hermit_abi::{EBADF, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
|
||||
#[cfg(target_family = "unix")]
|
||||
use libc::{EBADF, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
|
||||
|
||||
#[cfg(target_family = "unix")]
|
||||
use crate::io::BorrowedCursor;
|
||||
use crate::io::{self, IoSlice, IoSliceMut};
|
||||
use crate::mem::ManuallyDrop;
|
||||
#[cfg(target_os = "hermit")]
|
||||
use crate::os::hermit::io::FromRawFd;
|
||||
#[cfg(target_family = "unix")]
|
||||
use crate::os::unix::io::FromRawFd;
|
||||
use crate::sys::fd::FileDesc;
|
||||
|
||||
@ -15,15 +25,16 @@ impl Stdin {
|
||||
|
||||
impl io::Read for Stdin {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDIN_FILENO)).read(buf) }
|
||||
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDIN_FILENO)).read(buf) }
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "hermit"))]
|
||||
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> io::Result<()> {
|
||||
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDIN_FILENO)).read_buf(buf) }
|
||||
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDIN_FILENO)).read_buf(buf) }
|
||||
}
|
||||
|
||||
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
||||
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDIN_FILENO)).read_vectored(bufs) }
|
||||
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDIN_FILENO)).read_vectored(bufs) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -40,13 +51,11 @@ impl Stdout {
|
||||
|
||||
impl io::Write for Stdout {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDOUT_FILENO)).write(buf) }
|
||||
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDOUT_FILENO)).write(buf) }
|
||||
}
|
||||
|
||||
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
||||
unsafe {
|
||||
ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDOUT_FILENO)).write_vectored(bufs)
|
||||
}
|
||||
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDOUT_FILENO)).write_vectored(bufs) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -68,13 +77,11 @@ impl Stderr {
|
||||
|
||||
impl io::Write for Stderr {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDERR_FILENO)).write(buf) }
|
||||
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDERR_FILENO)).write(buf) }
|
||||
}
|
||||
|
||||
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
||||
unsafe {
|
||||
ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDERR_FILENO)).write_vectored(bufs)
|
||||
}
|
||||
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDERR_FILENO)).write_vectored(bufs) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -89,7 +96,7 @@ impl io::Write for Stderr {
|
||||
}
|
||||
|
||||
pub fn is_ebadf(err: &io::Error) -> bool {
|
||||
err.raw_os_error() == Some(libc::EBADF as i32)
|
||||
err.raw_os_error() == Some(EBADF as i32)
|
||||
}
|
||||
|
||||
pub const STDIN_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE;
|
@ -1,10 +1,10 @@
|
||||
#![forbid(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
use super::fd::WasiFd;
|
||||
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
|
||||
use crate::mem::ManuallyDrop;
|
||||
use crate::os::raw;
|
||||
use crate::os::wasi::io::{AsRawFd, FromRawFd};
|
||||
use crate::sys::pal::fd::WasiFd;
|
||||
|
||||
pub struct Stdin;
|
||||
pub struct Stdout;
|
@ -3,10 +3,10 @@
|
||||
use core::char::MAX_LEN_UTF8;
|
||||
use core::str::utf8_char_width;
|
||||
|
||||
use super::api::{self, WinError};
|
||||
use crate::mem::MaybeUninit;
|
||||
use crate::os::windows::io::{FromRawHandle, IntoRawHandle};
|
||||
use crate::sys::handle::Handle;
|
||||
use crate::sys::pal::api::{self, WinError};
|
||||
use crate::sys::{c, cvt};
|
||||
use crate::{cmp, io, ptr, str};
|
||||
|
@ -1,6 +1,5 @@
|
||||
use super::abi;
|
||||
use super::abi::fileno;
|
||||
use crate::io::{self, BorrowedCursor};
|
||||
use crate::sys::pal::abi::{self, fileno};
|
||||
|
||||
pub struct Stdin;
|
||||
pub struct Stdout;
|
Loading…
Reference in New Issue
Block a user