]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/wasi/stdio.rs
Fix font color for help button in ayu and dark themes
[rust.git] / library / std / src / sys / wasi / stdio.rs
1 use crate::io::{self, IoSlice, IoSliceMut};
2 use crate::mem::ManuallyDrop;
3 use crate::sys::fd::WasiFd;
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     #[inline]
15     pub fn as_raw_fd(&self) -> u32 {
16         0
17     }
18 }
19
20 impl io::Read for Stdin {
21     fn read(&mut self, data: &mut [u8]) -> io::Result<usize> {
22         self.read_vectored(&mut [IoSliceMut::new(data)])
23     }
24
25     fn read_vectored(&mut self, data: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
26         ManuallyDrop::new(unsafe { WasiFd::from_raw(self.as_raw_fd()) }).read(data)
27     }
28
29     #[inline]
30     fn is_read_vectored(&self) -> bool {
31         true
32     }
33 }
34
35 impl Stdout {
36     pub fn new() -> io::Result<Stdout> {
37         Ok(Stdout)
38     }
39
40     #[inline]
41     pub fn as_raw_fd(&self) -> u32 {
42         1
43     }
44 }
45
46 impl io::Write for Stdout {
47     fn write(&mut self, data: &[u8]) -> io::Result<usize> {
48         self.write_vectored(&[IoSlice::new(data)])
49     }
50
51     fn write_vectored(&mut self, data: &[IoSlice<'_>]) -> io::Result<usize> {
52         ManuallyDrop::new(unsafe { WasiFd::from_raw(self.as_raw_fd()) }).write(data)
53     }
54
55     #[inline]
56     fn is_write_vectored(&self) -> bool {
57         true
58     }
59     fn flush(&mut self) -> io::Result<()> {
60         Ok(())
61     }
62 }
63
64 impl Stderr {
65     pub fn new() -> io::Result<Stderr> {
66         Ok(Stderr)
67     }
68
69     #[inline]
70     pub fn as_raw_fd(&self) -> u32 {
71         2
72     }
73 }
74
75 impl io::Write for Stderr {
76     fn write(&mut self, data: &[u8]) -> io::Result<usize> {
77         self.write_vectored(&[IoSlice::new(data)])
78     }
79
80     fn write_vectored(&mut self, data: &[IoSlice<'_>]) -> io::Result<usize> {
81         ManuallyDrop::new(unsafe { WasiFd::from_raw(self.as_raw_fd()) }).write(data)
82     }
83
84     #[inline]
85     fn is_write_vectored(&self) -> bool {
86         true
87     }
88
89     fn flush(&mut self) -> io::Result<()> {
90         Ok(())
91     }
92 }
93
94 pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
95
96 pub fn is_ebadf(err: &io::Error) -> bool {
97     err.raw_os_error() == Some(wasi::ERRNO_BADF.into())
98 }
99
100 pub fn panic_output() -> Option<impl io::Write> {
101     Stderr::new().ok()
102 }