|
3 | 3 | use core::{fmt, slice}; |
4 | 4 | use crate::nr; |
5 | 5 |
|
6 | | -/// Host's standard error |
| 6 | +/// A byte stream to the host (e.g., host's stdout or stderr). |
7 | 7 | #[derive(Clone, Copy)] |
8 | | -pub struct HStderr { |
| 8 | +pub struct HostStream { |
9 | 9 | fd: usize, |
10 | 10 | } |
11 | 11 |
|
12 | | -impl HStderr { |
| 12 | +impl HostStream { |
13 | 13 | /// Attempts to write an entire `buffer` into this sink |
14 | 14 | pub fn write_all(&mut self, buffer: &[u8]) -> Result<(), ()> { |
15 | 15 | write_all(self.fd, buffer) |
16 | 16 | } |
17 | 17 | } |
18 | 18 |
|
19 | | -impl fmt::Write for HStderr { |
20 | | - fn write_str(&mut self, s: &str) -> fmt::Result { |
21 | | - self.write_all(s.as_bytes()).map_err(|_| fmt::Error) |
22 | | - } |
23 | | -} |
24 | | - |
25 | | -/// Host's standard output |
26 | | -#[derive(Clone, Copy)] |
27 | | -pub struct HStdout { |
28 | | - fd: usize, |
29 | | -} |
30 | | - |
31 | | -impl HStdout { |
32 | | - /// Attempts to write an entire `buffer` into this sink |
33 | | - pub fn write_all(&mut self, buffer: &[u8]) -> Result<(), ()> { |
34 | | - write_all(self.fd, buffer) |
35 | | - } |
36 | | -} |
37 | | - |
38 | | -impl fmt::Write for HStdout { |
| 19 | +impl fmt::Write for HostStream { |
39 | 20 | fn write_str(&mut self, s: &str) -> fmt::Result { |
40 | 21 | self.write_all(s.as_bytes()).map_err(|_| fmt::Error) |
41 | 22 | } |
42 | 23 | } |
43 | 24 |
|
44 | 25 | /// Construct a new handle to the host's standard error. |
45 | | -pub fn hstderr() -> Result<HStderr, ()> { |
| 26 | +pub fn hstderr() -> Result<HostStream, ()> { |
46 | 27 | // There is actually no stderr access in ARM Semihosting documentation. Use |
47 | 28 | // convention used in libgloss. |
48 | 29 | // See: libgloss/arm/syscalls.c, line 139. |
49 | 30 | // https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=libgloss/arm/syscalls.c#l139 |
50 | | - open(":tt\0", nr::open::W_APPEND).map(|fd| HStderr { fd }) |
| 31 | + open(":tt\0", nr::open::W_APPEND) |
51 | 32 | } |
52 | 33 |
|
53 | 34 | /// Construct a new handle to the host's standard output. |
54 | | -pub fn hstdout() -> Result<HStdout, ()> { |
55 | | - open(":tt\0", nr::open::W_TRUNC).map(|fd| HStdout { fd }) |
| 35 | +pub fn hstdout() -> Result<HostStream, ()> { |
| 36 | + open(":tt\0", nr::open::W_TRUNC) |
56 | 37 | } |
57 | 38 |
|
58 | | -fn open(name: &str, mode: usize) -> Result<usize, ()> { |
| 39 | +fn open(name: &str, mode: usize) -> Result<HostStream, ()> { |
59 | 40 | let name = name.as_bytes(); |
60 | 41 | match unsafe { syscall!(OPEN, name.as_ptr(), mode, name.len() - 1) } as |
61 | 42 | isize { |
62 | 43 | -1 => Err(()), |
63 | | - fd => Ok(fd as usize), |
| 44 | + fd => Ok(HostStream { fd: fd as usize }), |
64 | 45 | } |
65 | 46 | } |
66 | 47 |
|
|
0 commit comments