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