]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/hermit/stdio.rs
Auto merge of #102334 - compiler-errors:rpitit-substs-issue, r=cjgillot
[rust.git] / library / std / src / sys / hermit / stdio.rs
1 use crate::io;
2 use crate::io::{IoSlice, IoSliceMut};
3 use crate::sys::hermit::abi;
4
5 pub struct Stdin;
6 pub struct Stdout;
7 pub struct Stderr;
8
9 impl Stdin {
10     pub const fn new() -> Stdin {
11         Stdin
12     }
13 }
14
15 impl io::Read for Stdin {
16     fn read(&mut self, data: &mut [u8]) -> io::Result<usize> {
17         self.read_vectored(&mut [IoSliceMut::new(data)])
18     }
19
20     fn read_vectored(&mut self, _data: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
21         Ok(0)
22     }
23
24     #[inline]
25     fn is_read_vectored(&self) -> bool {
26         true
27     }
28 }
29
30 impl Stdout {
31     pub const fn new() -> Stdout {
32         Stdout
33     }
34 }
35
36 impl io::Write for Stdout {
37     fn write(&mut self, data: &[u8]) -> io::Result<usize> {
38         let len;
39
40         unsafe { len = abi::write(1, data.as_ptr() as *const u8, data.len()) }
41
42         if len < 0 {
43             Err(io::const_io_error!(io::ErrorKind::Uncategorized, "Stdout is not able to print"))
44         } else {
45             Ok(len as usize)
46         }
47     }
48
49     fn write_vectored(&mut self, data: &[IoSlice<'_>]) -> io::Result<usize> {
50         let len;
51
52         unsafe { len = abi::write(1, data.as_ptr() as *const u8, data.len()) }
53
54         if len < 0 {
55             Err(io::const_io_error!(io::ErrorKind::Uncategorized, "Stdout is not able to print"))
56         } else {
57             Ok(len as usize)
58         }
59     }
60
61     #[inline]
62     fn is_write_vectored(&self) -> bool {
63         true
64     }
65
66     fn flush(&mut self) -> io::Result<()> {
67         Ok(())
68     }
69 }
70
71 impl Stderr {
72     pub const fn new() -> Stderr {
73         Stderr
74     }
75 }
76
77 impl io::Write for Stderr {
78     fn write(&mut self, data: &[u8]) -> io::Result<usize> {
79         let len;
80
81         unsafe { len = abi::write(2, data.as_ptr() as *const u8, data.len()) }
82
83         if len < 0 {
84             Err(io::const_io_error!(io::ErrorKind::Uncategorized, "Stderr is not able to print"))
85         } else {
86             Ok(len as usize)
87         }
88     }
89
90     fn write_vectored(&mut self, data: &[IoSlice<'_>]) -> io::Result<usize> {
91         let len;
92
93         unsafe { len = abi::write(2, data.as_ptr() as *const u8, data.len()) }
94
95         if len < 0 {
96             Err(io::const_io_error!(io::ErrorKind::Uncategorized, "Stderr is not able to print"))
97         } else {
98             Ok(len as usize)
99         }
100     }
101
102     #[inline]
103     fn is_write_vectored(&self) -> bool {
104         true
105     }
106
107     fn flush(&mut self) -> io::Result<()> {
108         Ok(())
109     }
110 }
111
112 pub const STDIN_BUF_SIZE: usize = 0;
113
114 pub fn is_ebadf(_err: &io::Error) -> bool {
115     true
116 }
117
118 pub fn panic_output() -> Option<impl io::Write> {
119     Some(Stderr::new())
120 }