Skip to content

Commit 55d67fa

Browse files
bors[bot]m-ou-se
andauthored
Merge #269
269: Merge HStdout and HStderr into the same type r=jonas-schievink a=adamgreig As the next step in #263 (review), this PR applies rust-embedded/cortex-m-semihosting#41 to this repository. @m-ou-se, I tried exporting a patch from cortex-m-semihosting and applying it here but it didn't work first time, so I ended up just copying the changes. I've set you as the author of the commit and marked it as committed-by me, I hope you're OK with that but please let me know if you'd like it changed. (Also, thanks again for the PR, sorry it's taken over a year...). Co-authored-by: Mara Bos <[email protected]>
2 parents 432e5f5 + 320881e commit 55d67fa

File tree

4 files changed

+17
-38
lines changed

4 files changed

+17
-38
lines changed

cortex-m-semihosting/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.5] - 2019-08-29
911

1012
### Added

cortex-m-semihosting/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) {
1212
let _result = interrupt::free(|_| unsafe {
@@ -28,7 +28,7 @@ pub fn hstdout_fmt(args: fmt::Arguments) {
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) {
3434
let _result = interrupt::free(|_| unsafe {

cortex-m-semihosting/src/hio.rs

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

6-
/// Host's standard error
6+
/// A byte stream to the host (e.g., host's stdout or stderr).
77
#[derive(Clone, Copy)]
8-
pub struct HStderr {
8+
pub struct HostStream {
99
fd: usize,
1010
}
1111

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

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 {
3920
fn write_str(&mut self, s: &str) -> fmt::Result {
4021
self.write_all(s.as_bytes()).map_err(|_| fmt::Error)
4122
}
4223
}
4324

4425
/// Construct a new handle to the host's standard error.
45-
pub fn hstderr() -> Result<HStderr, ()> {
26+
pub fn hstderr() -> Result<HostStream, ()> {
4627
// There is actually no stderr access in ARM Semihosting documentation. Use
4728
// convention used in libgloss.
4829
// See: libgloss/arm/syscalls.c, line 139.
4930
// 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)
5132
}
5233

5334
/// 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)
5637
}
5738

58-
fn open(name: &str, mode: usize) -> Result<usize, ()> {
39+
fn open(name: &str, mode: usize) -> Result<HostStream, ()> {
5940
let name = name.as_bytes();
6041
match unsafe { syscall!(OPEN, name.as_ptr(), mode, name.len() - 1) } as
6142
isize {
6243
-1 => Err(()),
63-
fd => Ok(fd as usize),
44+
fd => Ok(HostStream { fd: fd as usize }),
6445
}
6546
}
6647

cortex-m-semihosting/src/lib.rs

Lines changed: 2 additions & 6 deletions
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
//!
@@ -35,11 +35,7 @@
3535
//!
3636
//! // This function will be called by the application
3737
//! fn print() -> Result<(), core::fmt::Error> {
38-
//! let mut stdout = match hio::hstdout() {
39-
//! Ok(fd) => fd,
40-
//! Err(()) => return Err(core::fmt::Error),
41-
//! };
42-
//!
38+
//! let mut stdout = hio::hstdout().map_err(|_| core::fmt::Error)?;
4339
//! let language = "Rust";
4440
//! let ranking = 1;
4541
//!

0 commit comments

Comments
 (0)