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.
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.
11 use fortanix_sgx_abi as abi;
14 use sys::fd::FileDesc;
17 pub struct Stdout(());
18 pub struct Stderr(());
20 fn with_std_fd<F: FnOnce(&FileDesc) -> R, R>(fd: abi::Fd, f: F) -> R {
21 let fd = FileDesc::new(fd);
28 pub fn new() -> io::Result<Stdin> { Ok(Stdin(())) }
30 pub fn read(&self, data: &mut [u8]) -> io::Result<usize> {
31 with_std_fd(abi::FD_STDIN, |fd| fd.read(data))
36 pub fn new() -> io::Result<Stdout> { Ok(Stdout(())) }
38 pub fn write(&self, data: &[u8]) -> io::Result<usize> {
39 with_std_fd(abi::FD_STDOUT, |fd| fd.write(data))
42 pub fn flush(&self) -> io::Result<()> {
43 with_std_fd(abi::FD_STDOUT, |fd| fd.flush())
48 pub fn new() -> io::Result<Stderr> { Ok(Stderr(())) }
50 pub fn write(&self, data: &[u8]) -> io::Result<usize> {
51 with_std_fd(abi::FD_STDERR, |fd| fd.write(data))
54 pub fn flush(&self) -> io::Result<()> {
55 with_std_fd(abi::FD_STDERR, |fd| fd.flush())
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
62 impl io::Write for Stderr {
63 fn write(&mut self, data: &[u8]) -> io::Result<usize> {
64 Stderr::write(self, data)
67 fn flush(&mut self) -> io::Result<()> {
72 pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE;
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 _)
79 pub fn panic_output() -> Option<impl io::Write> {
80 super::abi::panic::SgxPanicOutput::new()