]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/stdio.rs
Ensure `record_layout_for_printing()` is inlined.
[rust.git] / src / libstd / sys / unix / stdio.rs
1 use io;
2 use libc;
3 use 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> { Ok(Stdin(())) }
11 }
12
13 impl io::Read for Stdin {
14     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
15         let fd = FileDesc::new(libc::STDIN_FILENO);
16         let ret = fd.read(buf);
17         fd.into_raw(); // do not close this FD
18         ret
19     }
20 }
21
22 impl Stdout {
23     pub fn new() -> io::Result<Stdout> { Ok(Stdout(())) }
24 }
25
26 impl io::Write for Stdout {
27     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
28         let fd = FileDesc::new(libc::STDOUT_FILENO);
29         let ret = fd.write(buf);
30         fd.into_raw(); // do not close this FD
31         ret
32     }
33
34     fn flush(&mut self) -> io::Result<()> {
35         Ok(())
36     }
37 }
38
39 impl Stderr {
40     pub fn new() -> io::Result<Stderr> { Ok(Stderr(())) }
41 }
42
43 impl io::Write for Stderr {
44     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
45         let fd = FileDesc::new(libc::STDERR_FILENO);
46         let ret = fd.write(buf);
47         fd.into_raw(); // do not close this FD
48         ret
49     }
50
51     fn flush(&mut self) -> io::Result<()> {
52         Ok(())
53     }
54 }
55
56 pub fn is_ebadf(err: &io::Error) -> bool {
57     err.raw_os_error() == Some(libc::EBADF as i32)
58 }
59
60 pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE;
61
62 pub fn panic_output() -> Option<impl io::Write> {
63     Stderr::new().ok()
64 }