1 | use crate::io::{self, BorrowedCursor, 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_buf(&mut self, buf: BorrowedCursor<'_>) -> io::Result<()> { |
22 | unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDIN_FILENO)).read_buf(cursor: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 | |
35 | impl Stdout { |
36 | pub const fn new() -> Stdout { |
37 | Stdout(()) |
38 | } |
39 | } |
40 | |
41 | impl 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 | |
63 | impl Stderr { |
64 | pub const fn new() -> Stderr { |
65 | Stderr(()) |
66 | } |
67 | } |
68 | |
69 | impl 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 | |
91 | pub fn is_ebadf(err: &io::Error) -> bool { |
92 | err.raw_os_error() == Some(libc::EBADF as i32) |
93 | } |
94 | |
95 | pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE; |
96 | |
97 | pub fn panic_output() -> Option<impl io::Write> { |
98 | Some(Stderr::new()) |
99 | } |
100 | |