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

Commit 2c2d70f

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 3a944ac commit 2c2d70f

File tree

4 files changed

+15
-32
lines changed

4 files changed

+15
-32
lines changed

CHANGELOG.md

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

66
## [Unreleased]
77

8+
- Merge `HStdout` and `HStderr` into one type: `HostStream`
89
- Adds a feature to work around JLink quirks
910
- Adds a dbg! macro using heprintln
1011
- Now Rust 2018 edition

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 crate::hio::{self, HStderr, HStdout};
7+
use crate::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
@@ -4,63 +4,45 @@ use core::{fmt, slice};
44
use crate::nr;
55

66
/// Host's standard error
7+
/// A byte stream to the host (e.g. host's stdout or stderr).
78
#[derive(Clone, Copy)]
8-
pub struct HStderr {
9+
pub struct HostStream {
910
fd: usize,
1011
}
1112

12-
impl HStderr {
13+
impl HostStream {
1314
/// Attempts to write an entire `buffer` into this sink
1415
pub fn write_all(&mut self, buffer: &[u8]) -> Result<(), ()> {
1516
write_all(self.fd, buffer)
1617
}
1718
}
1819

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 {
20+
impl fmt::Write for HostStream {
3921
fn write_str(&mut self, s: &str) -> fmt::Result {
4022
self.write_all(s.as_bytes()).map_err(|_| fmt::Error)
4123
}
4224
}
4325

4426
/// Construct a new handle to the host's standard error.
45-
pub fn hstderr() -> Result<HStderr, ()> {
27+
pub fn hstderr() -> Result<HostStream, ()> {
4628
// There is actually no stderr access in ARM Semihosting documentation. Use
4729
// convention used in libgloss.
4830
// See: libgloss/arm/syscalls.c, line 139.
4931
// 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 })
32+
open(":tt\0", nr::open::W_APPEND)
5133
}
5234

5335
/// 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 })
36+
pub fn hstdout() -> Result<HostStream, ()> {
37+
open(":tt\0", nr::open::W_TRUNC)
5638
}
5739

58-
fn open(name: &str, mode: usize) -> Result<usize, ()> {
40+
fn open(name: &str, mode: usize) -> Result<HostStream, ()> {
5941
let name = name.as_bytes();
6042
match unsafe { syscall!(OPEN, name.as_ptr(), mode, name.len() - 1) } as
6143
isize {
6244
-1 => Err(()),
63-
fd => Ok(fd as usize),
45+
fd => Ok(HostStream { fd: fd as usize}),
6446
}
6547
}
6648

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)