std/sys/pal/unix/
stdio.rs

1use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
2use crate::mem::ManuallyDrop;
3use crate::os::unix::io::FromRawFd;
4use crate::sys::fd::FileDesc;
5
6pub struct Stdin(());
7pub struct Stdout(());
8pub struct Stderr(());
9
10impl Stdin {
11    pub const fn new() -> Stdin {
12        Stdin(())
13    }
14}
15
16impl io::Read for Stdin {
17    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
18        unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDIN_FILENO)).read(buf) }
19    }
20
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
25    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
26        unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDIN_FILENO)).read_vectored(bufs) }
27    }
28
29    #[inline]
30    fn is_read_vectored(&self) -> bool {
31        true
32    }
33}
34
35impl Stdout {
36    pub const fn new() -> Stdout {
37        Stdout(())
38    }
39}
40
41impl io::Write for Stdout {
42    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
43        unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDOUT_FILENO)).write(buf) }
44    }
45
46    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
47        unsafe {
48            ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDOUT_FILENO)).write_vectored(bufs)
49        }
50    }
51
52    #[inline]
53    fn is_write_vectored(&self) -> bool {
54        true
55    }
56
57    #[inline]
58    fn flush(&mut self) -> io::Result<()> {
59        Ok(())
60    }
61}
62
63impl Stderr {
64    pub const fn new() -> Stderr {
65        Stderr(())
66    }
67}
68
69impl io::Write for Stderr {
70    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
71        unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDERR_FILENO)).write(buf) }
72    }
73
74    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
75        unsafe {
76            ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDERR_FILENO)).write_vectored(bufs)
77        }
78    }
79
80    #[inline]
81    fn is_write_vectored(&self) -> bool {
82        true
83    }
84
85    #[inline]
86    fn flush(&mut self) -> io::Result<()> {
87        Ok(())
88    }
89}
90
91pub fn is_ebadf(err: &io::Error) -> bool {
92    err.raw_os_error() == Some(libc::EBADF as i32)
93}
94
95pub const STDIN_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE;
96
97pub fn panic_output() -> Option<impl io::Write> {
98    Some(Stderr::new())
99}