]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys/sgx/stdio.rs
New upstream version 1.34.2+dfsg1
[rustc.git] / src / libstd / sys / sgx / stdio.rs
CommitLineData
0731742a
XL
1use fortanix_sgx_abi as abi;
2
3use io;
4use sys::fd::FileDesc;
5
6pub struct Stdin(());
7pub struct Stdout(());
8pub struct Stderr(());
9
10fn 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
17impl Stdin {
18 pub fn new() -> io::Result<Stdin> { Ok(Stdin(())) }
9fa01778 19}
0731742a 20
9fa01778
XL
21impl 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
27impl Stdout {
28 pub fn new() -> io::Result<Stdout> { Ok(Stdout(())) }
9fa01778 29}
0731742a 30
9fa01778
XL
31impl 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
41impl Stderr {
42 pub fn new() -> io::Result<Stderr> { Ok(Stderr(())) }
0731742a
XL
43}
44
0731742a 45impl 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
55pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE;
56
57pub 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
62pub fn panic_output() -> Option<impl io::Write> {
63 super::abi::panic::SgxPanicOutput::new()
64}