]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/stdio.rs
Rollup merge of #60609 - spastorino:be-explicit-on-mem-replace-doc, r=Centril
[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 #[cfg(not(test))]
6 use crate::slice;
7 #[cfg(not(test))]
8 use crate::str;
9
10 pub struct Stdin(());
11 pub struct Stdout(());
12 pub struct Stderr(());
13
14 fn with_std_fd<F: FnOnce(&FileDesc) -> R, R>(fd: abi::Fd, f: F) -> R {
15     let fd = FileDesc::new(fd);
16     let ret = f(&fd);
17     fd.into_raw();
18     ret
19 }
20
21 impl Stdin {
22     pub fn new() -> io::Result<Stdin> { Ok(Stdin(())) }
23 }
24
25 impl io::Read for Stdin {
26     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
27         with_std_fd(abi::FD_STDIN, |fd| fd.read(buf))
28     }
29 }
30
31 impl Stdout {
32     pub fn new() -> io::Result<Stdout> { Ok(Stdout(())) }
33 }
34
35 impl io::Write for Stdout {
36     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
37         with_std_fd(abi::FD_STDOUT, |fd| fd.write(buf))
38     }
39
40     fn flush(&mut self) -> io::Result<()> {
41         with_std_fd(abi::FD_STDOUT, |fd| fd.flush())
42     }
43 }
44
45 impl Stderr {
46     pub fn new() -> io::Result<Stderr> { Ok(Stderr(())) }
47 }
48
49 impl io::Write for Stderr {
50     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
51         with_std_fd(abi::FD_STDERR, |fd| fd.write(buf))
52     }
53
54     fn flush(&mut self) -> io::Result<()> {
55         with_std_fd(abi::FD_STDERR, |fd| fd.flush())
56     }
57 }
58
59 pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
60
61 pub fn is_ebadf(err: &io::Error) -> bool {
62     // FIXME: Rust normally maps Unix EBADF to `Other`
63     err.raw_os_error() == Some(abi::Error::BrokenPipe as _)
64 }
65
66 pub fn panic_output() -> Option<impl io::Write> {
67     super::abi::panic::SgxPanicOutput::new()
68 }
69
70 // This function is needed by libunwind. The symbol is named in pre-link args
71 // for the target specification, so keep that in sync.
72 #[cfg(not(test))]
73 #[no_mangle]
74 pub unsafe extern "C" fn __rust_print_err(m: *mut u8, s: i32) {
75     if s < 0 {
76         return;
77     }
78     let buf = slice::from_raw_parts(m as *const u8, s as _);
79     if let Ok(s) = str::from_utf8(&buf[..buf.iter().position(|&b| b == 0).unwrap_or(buf.len())]) {
80         eprint!("{}", s);
81     }
82 }