]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/stdio.rs
f2c6892bfb7fde6f773ea8613b0ba927745063cd
[rust.git] / src / libstd / sys / sgx / stdio.rs
1 use fortanix_sgx_abi as abi;
2
3 use crate::io;
4 use crate::sys::fd::FileDesc;
5
6 pub struct Stdin(());
7 pub struct Stdout(());
8 pub struct Stderr(());
9
10 fn with_std_fd<F: FnOnce(&FileDesc) -> R, R>(fd: abi::Fd, f: F) -> R {
11     let fd = FileDesc::new(fd);
12     let ret = f(&fd);
13     fd.into_raw();
14     ret
15 }
16
17 impl Stdin {
18     pub fn new() -> io::Result<Stdin> { Ok(Stdin(())) }
19 }
20
21 impl io::Read for Stdin {
22     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
23         with_std_fd(abi::FD_STDIN, |fd| fd.read(buf))
24     }
25 }
26
27 impl Stdout {
28     pub fn new() -> io::Result<Stdout> { Ok(Stdout(())) }
29 }
30
31 impl io::Write for Stdout {
32     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
33         with_std_fd(abi::FD_STDOUT, |fd| fd.write(buf))
34     }
35
36     fn flush(&mut self) -> io::Result<()> {
37         with_std_fd(abi::FD_STDOUT, |fd| fd.flush())
38     }
39 }
40
41 impl Stderr {
42     pub fn new() -> io::Result<Stderr> { Ok(Stderr(())) }
43 }
44
45 impl io::Write for Stderr {
46     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
47         with_std_fd(abi::FD_STDERR, |fd| fd.write(buf))
48     }
49
50     fn flush(&mut self) -> io::Result<()> {
51         with_std_fd(abi::FD_STDERR, |fd| fd.flush())
52     }
53 }
54
55 pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
56
57 pub fn is_ebadf(err: &io::Error) -> bool {
58     // FIXME: Rust normally maps Unix EBADF to `Other`
59     err.raw_os_error() == Some(abi::Error::BrokenPipe as _)
60 }
61
62 pub fn panic_output() -> Option<impl io::Write> {
63     super::abi::panic::SgxPanicOutput::new()
64 }