]> git.proxmox.com Git - rustc.git/blame - library/std/src/sys/unix/stdio.rs
New upstream version 1.71.1+dfsg1
[rustc.git] / library / std / src / sys / unix / stdio.rs
CommitLineData
353b0b11 1use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
48663c56 2use crate::mem::ManuallyDrop;
2b03887a 3use crate::os::unix::io::FromRawFd;
60c5eb7d 4use crate::sys::fd::FileDesc;
c34b1796
AL
5
6pub struct Stdin(());
7pub struct Stdout(());
8pub struct Stderr(());
9
10impl Stdin {
3dfed10e
XL
11 pub const fn new() -> Stdin {
12 Stdin(())
60c5eb7d 13 }
9fa01778 14}
c34b1796 15
9fa01778
XL
16impl io::Read for Stdin {
17 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
94222f64 18 unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDIN_FILENO)).read(buf) }
48663c56
XL
19 }
20
353b0b11
FG
21 fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> io::Result<()> {
22 unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDIN_FILENO)).read_buf(buf) }
23 }
24
48663c56 25 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
94222f64 26 unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDIN_FILENO)).read_vectored(bufs) }
c34b1796 27 }
f9f354fc
XL
28
29 #[inline]
30 fn is_read_vectored(&self) -> bool {
31 true
32 }
c34b1796
AL
33}
34
35impl Stdout {
3dfed10e
XL
36 pub const fn new() -> Stdout {
37 Stdout(())
60c5eb7d 38 }
9fa01778 39}
c34b1796 40
9fa01778
XL
41impl io::Write for Stdout {
42 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
94222f64 43 unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDOUT_FILENO)).write(buf) }
48663c56
XL
44 }
45
46 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
94222f64
XL
47 unsafe {
48 ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDOUT_FILENO)).write_vectored(bufs)
49 }
c34b1796 50 }
476ff2be 51
f9f354fc
XL
52 #[inline]
53 fn is_write_vectored(&self) -> bool {
54 true
55 }
56
9fa01778 57 fn flush(&mut self) -> io::Result<()> {
476ff2be
SL
58 Ok(())
59 }
c34b1796
AL
60}
61
62impl Stderr {
3dfed10e
XL
63 pub const fn new() -> Stderr {
64 Stderr(())
60c5eb7d 65 }
c34b1796
AL
66}
67
c34b1796 68impl io::Write for Stderr {
9fa01778 69 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
94222f64 70 unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDERR_FILENO)).write(buf) }
48663c56
XL
71 }
72
73 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
94222f64
XL
74 unsafe {
75 ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDERR_FILENO)).write_vectored(bufs)
76 }
c34b1796 77 }
476ff2be 78
f9f354fc
XL
79 #[inline]
80 fn is_write_vectored(&self) -> bool {
81 true
82 }
83
476ff2be 84 fn flush(&mut self) -> io::Result<()> {
9fa01778 85 Ok(())
476ff2be 86 }
c34b1796 87}
c30ab7b3 88
abe05a73
XL
89pub fn is_ebadf(err: &io::Error) -> bool {
90 err.raw_os_error() == Some(libc::EBADF as i32)
91}
92
532ac7d7 93pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
83c7162d 94
0731742a 95pub fn panic_output() -> Option<impl io::Write> {
3dfed10e 96 Some(Stderr::new())
83c7162d 97}