1 | #[cfg (target_os = "hermit" )] |
2 | use hermit_abi::{EBADF, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO}; |
3 | #[cfg (target_family = "unix" )] |
4 | use libc::{EBADF, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO}; |
5 | |
6 | use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut}; |
7 | use crate::mem::ManuallyDrop; |
8 | #[cfg (target_os = "hermit" )] |
9 | use crate::os::hermit::io::FromRawFd; |
10 | #[cfg (target_family = "unix" )] |
11 | use crate::os::unix::io::FromRawFd; |
12 | use crate::sys::fd::FileDesc; |
13 | |
14 | pub struct Stdin; |
15 | pub struct Stdout; |
16 | pub struct Stderr; |
17 | |
18 | impl Stdin { |
19 | pub const fn new() -> Stdin { |
20 | Stdin |
21 | } |
22 | } |
23 | |
24 | impl io::Read for Stdin { |
25 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
26 | unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDIN_FILENO)).read(buf) } |
27 | } |
28 | |
29 | fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> io::Result<()> { |
30 | unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDIN_FILENO)).read_buf(cursor:buf) } |
31 | } |
32 | |
33 | fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { |
34 | unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDIN_FILENO)).read_vectored(bufs) } |
35 | } |
36 | |
37 | #[inline ] |
38 | fn is_read_vectored(&self) -> bool { |
39 | true |
40 | } |
41 | } |
42 | |
43 | impl Stdout { |
44 | pub const fn new() -> Stdout { |
45 | Stdout |
46 | } |
47 | } |
48 | |
49 | impl io::Write for Stdout { |
50 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
51 | unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDOUT_FILENO)).write(buf) } |
52 | } |
53 | |
54 | fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { |
55 | unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDOUT_FILENO)).write_vectored(bufs) } |
56 | } |
57 | |
58 | #[inline ] |
59 | fn is_write_vectored(&self) -> bool { |
60 | true |
61 | } |
62 | |
63 | #[inline ] |
64 | fn flush(&mut self) -> io::Result<()> { |
65 | Ok(()) |
66 | } |
67 | } |
68 | |
69 | impl Stderr { |
70 | pub const fn new() -> Stderr { |
71 | Stderr |
72 | } |
73 | } |
74 | |
75 | impl io::Write for Stderr { |
76 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
77 | unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDERR_FILENO)).write(buf) } |
78 | } |
79 | |
80 | fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { |
81 | unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDERR_FILENO)).write_vectored(bufs) } |
82 | } |
83 | |
84 | #[inline ] |
85 | fn is_write_vectored(&self) -> bool { |
86 | true |
87 | } |
88 | |
89 | #[inline ] |
90 | fn flush(&mut self) -> io::Result<()> { |
91 | Ok(()) |
92 | } |
93 | } |
94 | |
95 | pub fn is_ebadf(err: &io::Error) -> bool { |
96 | err.raw_os_error() == Some(EBADF as i32) |
97 | } |
98 | |
99 | pub const STDIN_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE; |
100 | |
101 | pub fn panic_output() -> Option<impl io::Write> { |
102 | Some(Stderr::new()) |
103 | } |
104 | |