]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/stdio.rs
SGX target: implement streams
[rust.git] / src / libstd / sys / sgx / stdio.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use fortanix_sgx_abi as abi;
12
13 use io;
14 use sys::fd::FileDesc;
15
16 pub struct Stdin(());
17 pub struct Stdout(());
18 pub struct Stderr(());
19
20 fn with_std_fd<F: FnOnce(&FileDesc) -> R, R>(fd: abi::Fd, f: F) -> R {
21     let fd = FileDesc::new(fd);
22     let ret = f(&fd);
23     fd.into_raw();
24     ret
25 }
26
27 impl Stdin {
28     pub fn new() -> io::Result<Stdin> { Ok(Stdin(())) }
29
30     pub fn read(&self, data: &mut [u8]) -> io::Result<usize> {
31         with_std_fd(abi::FD_STDIN, |fd| fd.read(data))
32     }
33 }
34
35 impl Stdout {
36     pub fn new() -> io::Result<Stdout> { Ok(Stdout(())) }
37
38     pub fn write(&self, data: &[u8]) -> io::Result<usize> {
39         with_std_fd(abi::FD_STDOUT, |fd| fd.write(data))
40     }
41
42     pub fn flush(&self) -> io::Result<()> {
43         with_std_fd(abi::FD_STDOUT, |fd| fd.flush())
44     }
45 }
46
47 impl Stderr {
48     pub fn new() -> io::Result<Stderr> { Ok(Stderr(())) }
49
50     pub fn write(&self, data: &[u8]) -> io::Result<usize> {
51         with_std_fd(abi::FD_STDERR, |fd| fd.write(data))
52     }
53
54     pub fn flush(&self) -> io::Result<()> {
55         with_std_fd(abi::FD_STDERR, |fd| fd.flush())
56     }
57 }
58
59 // FIXME: right now this raw stderr handle is used in a few places because
60 //        std::io::stderr_raw isn't exposed, but once that's exposed this impl
61 //        should go away
62 impl io::Write for Stderr {
63     fn write(&mut self, data: &[u8]) -> io::Result<usize> {
64         Stderr::write(self, data)
65     }
66
67     fn flush(&mut self) -> io::Result<()> {
68         Stderr::flush(self)
69     }
70 }
71
72 pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE;
73
74 pub fn is_ebadf(err: &io::Error) -> bool {
75     // FIXME: Rust normally maps Unix EBADF to `Other`
76     err.raw_os_error() == Some(abi::Error::BrokenPipe as _)
77 }
78
79 pub fn panic_output() -> Option<impl io::Write> {
80     super::abi::panic::SgxPanicOutput::new()
81 }