]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/sgx/stdio.rs
New upstream version 1.49.0~beta.4+dfsg1
[rustc.git] / library / std / src / sys / sgx / stdio.rs
1 use fortanix_sgx_abi as abi;
2
3 use crate::io;
4 #[cfg(not(test))]
5 use crate::slice;
6 #[cfg(not(test))]
7 use crate::str;
8 use crate::sys::fd::FileDesc;
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 const fn new() -> Stdin {
23 Stdin(())
24 }
25 }
26
27 impl io::Read for Stdin {
28 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
29 with_std_fd(abi::FD_STDIN, |fd| fd.read(buf))
30 }
31 }
32
33 impl Stdout {
34 pub const fn new() -> Stdout {
35 Stdout(())
36 }
37 }
38
39 impl io::Write for Stdout {
40 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
41 with_std_fd(abi::FD_STDOUT, |fd| fd.write(buf))
42 }
43
44 fn flush(&mut self) -> io::Result<()> {
45 with_std_fd(abi::FD_STDOUT, |fd| fd.flush())
46 }
47 }
48
49 impl Stderr {
50 pub const fn new() -> Stderr {
51 Stderr(())
52 }
53 }
54
55 impl io::Write for Stderr {
56 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
57 with_std_fd(abi::FD_STDERR, |fd| fd.write(buf))
58 }
59
60 fn flush(&mut self) -> io::Result<()> {
61 with_std_fd(abi::FD_STDERR, |fd| fd.flush())
62 }
63 }
64
65 pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
66
67 pub fn is_ebadf(err: &io::Error) -> bool {
68 // FIXME: Rust normally maps Unix EBADF to `Other`
69 err.raw_os_error() == Some(abi::Error::BrokenPipe as _)
70 }
71
72 pub fn panic_output() -> Option<impl io::Write> {
73 super::abi::panic::SgxPanicOutput::new()
74 }
75
76 // This function is needed by libunwind. The symbol is named in pre-link args
77 // for the target specification, so keep that in sync.
78 #[cfg(not(test))]
79 #[no_mangle]
80 pub unsafe extern "C" fn __rust_print_err(m: *mut u8, s: i32) {
81 if s < 0 {
82 return;
83 }
84 let buf = unsafe { slice::from_raw_parts(m as *const u8, s as _) };
85 if let Ok(s) = str::from_utf8(&buf[..buf.iter().position(|&b| b == 0).unwrap_or(buf.len())]) {
86 eprint!("{}", s);
87 }
88 }