Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

Commit c522bf0

Browse files
committed
Merge HStdout and HStderr into the same type.
They were exactly the same anyway. Now it is easier to pass either stdout or stderr to a function, for example.
1 parent 25cb553 commit c522bf0

File tree

4 files changed

+16
-32
lines changed

4 files changed

+16
-32
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
## [Unreleased]
77

8+
- Merge `HStdout` and `HStderr` into one type: `HostStream`
9+
810
## [v0.3.4] - 2019-04-22
911

1012
### Fixed

src/export.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use core::fmt::{self, Write};
44

55
use cortex_m::interrupt;
66

7-
use hio::{self, HStderr, HStdout};
7+
use hio::{self, HostStream};
88

9-
static mut HSTDOUT: Option<HStdout> = None;
9+
static mut HSTDOUT: Option<HostStream> = None;
1010

1111
pub fn hstdout_str(s: &str) -> Result<(), ()> {
1212
interrupt::free(|_| unsafe {
@@ -28,7 +28,7 @@ pub fn hstdout_fmt(args: fmt::Arguments) -> Result<(), ()> {
2828
})
2929
}
3030

31-
static mut HSTDERR: Option<HStderr> = None;
31+
static mut HSTDERR: Option<HostStream> = None;
3232

3333
pub fn hstderr_str(s: &str) -> Result<(), ()> {
3434
interrupt::free(|_| unsafe {

src/hio.rs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,44 @@
33
use core::{fmt, slice};
44
use nr;
55

6-
/// Host's standard error
7-
pub struct HStderr {
6+
/// A byte stream to the host (e.g. host's stdout or stderr).
7+
pub struct HostStream {
88
fd: usize,
99
}
1010

11-
impl HStderr {
11+
impl HostStream {
1212
/// Attempts to write an entire `buffer` into this sink
1313
pub fn write_all(&mut self, buffer: &[u8]) -> Result<(), ()> {
1414
write_all(self.fd, buffer)
1515
}
1616
}
1717

18-
impl fmt::Write for HStderr {
19-
fn write_str(&mut self, s: &str) -> fmt::Result {
20-
self.write_all(s.as_bytes()).map_err(|_| fmt::Error)
21-
}
22-
}
23-
24-
/// Host's standard output
25-
pub struct HStdout {
26-
fd: usize,
27-
}
28-
29-
impl HStdout {
30-
/// Attempts to write an entire `buffer` into this sink
31-
pub fn write_all(&mut self, buffer: &[u8]) -> Result<(), ()> {
32-
write_all(self.fd, buffer)
33-
}
34-
}
35-
36-
impl fmt::Write for HStdout {
18+
impl fmt::Write for HostStream {
3719
fn write_str(&mut self, s: &str) -> fmt::Result {
3820
self.write_all(s.as_bytes()).map_err(|_| fmt::Error)
3921
}
4022
}
4123

4224
/// Construct a new handle to the host's standard error.
43-
pub fn hstderr() -> Result<HStderr, ()> {
25+
pub fn hstderr() -> Result<HostStream, ()> {
4426
// There is actually no stderr access in ARM Semihosting documentation. Use
4527
// convention used in libgloss.
4628
// See: libgloss/arm/syscalls.c, line 139.
4729
// https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=libgloss/arm/syscalls.c#l139
48-
open(":tt\0", nr::open::W_APPEND).map(|fd| HStderr { fd })
30+
open(":tt\0", nr::open::W_APPEND)
4931
}
5032

5133
/// Construct a new handle to the host's standard output.
52-
pub fn hstdout() -> Result<HStdout, ()> {
53-
open(":tt\0", nr::open::W_TRUNC).map(|fd| HStdout { fd })
34+
pub fn hstdout() -> Result<HostStream, ()> {
35+
open(":tt\0", nr::open::W_TRUNC)
5436
}
5537

56-
fn open(name: &str, mode: usize) -> Result<usize, ()> {
38+
fn open(name: &str, mode: usize) -> Result<HostStream, ()> {
5739
let name = name.as_bytes();
5840
match unsafe { syscall!(OPEN, name.as_ptr(), mode, name.len() - 1) } as
5941
isize {
6042
-1 => Err(()),
61-
fd => Ok(fd as usize),
43+
fd => Ok(HostStream { fd: fd as usize}),
6244
}
6345
}
6446

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//!
2626
//! # Example
2727
//!
28-
//! ## Using `hio::HStdout`
28+
//! ## Using `hio::hstdout`
2929
//!
3030
//! This example will demonstrate how to print formatted strings.
3131
//!

0 commit comments

Comments
 (0)