]>
Commit | Line | Data |
---|---|---|
0731742a XL |
1 | use fortanix_sgx_abi as abi; |
2 | ||
3 | use io; | |
4 | use 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(())) } | |
9fa01778 | 19 | } |
0731742a | 20 | |
9fa01778 XL |
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)) | |
0731742a XL |
24 | } |
25 | } | |
26 | ||
27 | impl Stdout { | |
28 | pub fn new() -> io::Result<Stdout> { Ok(Stdout(())) } | |
9fa01778 | 29 | } |
0731742a | 30 | |
9fa01778 XL |
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)) | |
0731742a XL |
34 | } |
35 | ||
9fa01778 | 36 | fn flush(&mut self) -> io::Result<()> { |
0731742a XL |
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(())) } | |
0731742a XL |
43 | } |
44 | ||
0731742a | 45 | impl io::Write for Stderr { |
9fa01778 XL |
46 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
47 | with_std_fd(abi::FD_STDERR, |fd| fd.write(buf)) | |
0731742a XL |
48 | } |
49 | ||
50 | fn flush(&mut self) -> io::Result<()> { | |
9fa01778 | 51 | with_std_fd(abi::FD_STDERR, |fd| fd.flush()) |
0731742a XL |
52 | } |
53 | } | |
54 | ||
55 | pub const STDIN_BUF_SIZE: usize = ::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 | } |