]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unix/stdio.rs
Rollup merge of #80543 - LeSeulArtichaut:notify-close, r=spastorino
[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::{AsFd, BorrowedFd, 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 }
94
95 #[unstable(feature = "io_safety", issue = "87074")]
96 impl AsFd for io::Stdin {
97     #[inline]
98     fn as_fd(&self) -> BorrowedFd<'_> {
99         unsafe { BorrowedFd::borrow_raw_fd(libc::STDIN_FILENO) }
100     }
101 }
102
103 #[unstable(feature = "io_safety", issue = "87074")]
104 impl<'a> AsFd for io::StdinLock<'a> {
105     #[inline]
106     fn as_fd(&self) -> BorrowedFd<'_> {
107         unsafe { BorrowedFd::borrow_raw_fd(libc::STDIN_FILENO) }
108     }
109 }
110
111 #[unstable(feature = "io_safety", issue = "87074")]
112 impl AsFd for io::Stdout {
113     #[inline]
114     fn as_fd(&self) -> BorrowedFd<'_> {
115         unsafe { BorrowedFd::borrow_raw_fd(libc::STDOUT_FILENO) }
116     }
117 }
118
119 #[unstable(feature = "io_safety", issue = "87074")]
120 impl<'a> AsFd for io::StdoutLock<'a> {
121     #[inline]
122     fn as_fd(&self) -> BorrowedFd<'_> {
123         unsafe { BorrowedFd::borrow_raw_fd(libc::STDOUT_FILENO) }
124     }
125 }
126
127 #[unstable(feature = "io_safety", issue = "87074")]
128 impl AsFd for io::Stderr {
129     #[inline]
130     fn as_fd(&self) -> BorrowedFd<'_> {
131         unsafe { BorrowedFd::borrow_raw_fd(libc::STDERR_FILENO) }
132     }
133 }
134
135 #[unstable(feature = "io_safety", issue = "87074")]
136 impl<'a> AsFd for io::StderrLock<'a> {
137     #[inline]
138     fn as_fd(&self) -> BorrowedFd<'_> {
139         unsafe { BorrowedFd::borrow_raw_fd(libc::STDERR_FILENO) }
140     }
141 }