]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/stdio.rs
Fix wrong argument in autoderef process
[rust.git] / src / libstd / sys / unix / stdio.rs
1 use crate::io::{self, IoSlice, IoSliceMut};
2 use crate::mem::ManuallyDrop;
3 use crate::sys::fd::FileDesc;
4
5 pub struct Stdin(());
6 pub struct Stdout(());
7 pub struct Stderr(());
8
9 impl Stdin {
10     pub fn new() -> io::Result<Stdin> {
11         Ok(Stdin(()))
12     }
13 }
14
15 impl io::Read for Stdin {
16     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
17         ManuallyDrop::new(FileDesc::new(libc::STDIN_FILENO)).read(buf)
18     }
19
20     fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
21         ManuallyDrop::new(FileDesc::new(libc::STDIN_FILENO)).read_vectored(bufs)
22     }
23 }
24
25 impl Stdout {
26     pub fn new() -> io::Result<Stdout> {
27         Ok(Stdout(()))
28     }
29 }
30
31 impl io::Write for Stdout {
32     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
33         ManuallyDrop::new(FileDesc::new(libc::STDOUT_FILENO)).write(buf)
34     }
35
36     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
37         ManuallyDrop::new(FileDesc::new(libc::STDOUT_FILENO)).write_vectored(bufs)
38     }
39
40     fn flush(&mut self) -> io::Result<()> {
41         Ok(())
42     }
43 }
44
45 impl Stderr {
46     pub fn new() -> io::Result<Stderr> {
47         Ok(Stderr(()))
48     }
49 }
50
51 impl io::Write for Stderr {
52     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
53         ManuallyDrop::new(FileDesc::new(libc::STDERR_FILENO)).write(buf)
54     }
55
56     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
57         ManuallyDrop::new(FileDesc::new(libc::STDERR_FILENO)).write_vectored(bufs)
58     }
59
60     fn flush(&mut self) -> io::Result<()> {
61         Ok(())
62     }
63 }
64
65 pub fn is_ebadf(err: &io::Error) -> bool {
66     err.raw_os_error() == Some(libc::EBADF as i32)
67 }
68
69 pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
70
71 pub fn panic_output() -> Option<impl io::Write> {
72     Stderr::new().ok()
73 }