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

Commit 5fbd612

Browse files
Make hprint macros unwrap any errors
1 parent 7a961f0 commit 5fbd612

File tree

2 files changed

+51
-25
lines changed

2 files changed

+51
-25
lines changed

src/export.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,65 @@ use crate::hio::{self, HStderr, HStdout};
88

99
static mut HSTDOUT: Option<HStdout> = None;
1010

11-
pub fn hstdout_str(s: &str) -> Result<(), ()> {
12-
interrupt::free(|_| unsafe {
11+
pub fn hstdout_str(s: &str) {
12+
let result = interrupt::free(|_| unsafe {
1313
if HSTDOUT.is_none() {
1414
HSTDOUT = Some(hio::hstdout()?);
1515
}
1616

1717
HSTDOUT.as_mut().unwrap().write_str(s).map_err(drop)
18-
})
18+
});
19+
20+
if result.is_err() {
21+
error("hstdout");
22+
}
1923
}
2024

21-
pub fn hstdout_fmt(args: fmt::Arguments) -> Result<(), ()> {
22-
interrupt::free(|_| unsafe {
25+
pub fn hstdout_fmt(args: fmt::Arguments) {
26+
let result = interrupt::free(|_| unsafe {
2327
if HSTDOUT.is_none() {
2428
HSTDOUT = Some(hio::hstdout()?);
2529
}
2630

2731
HSTDOUT.as_mut().unwrap().write_fmt(args).map_err(drop)
28-
})
32+
});
33+
34+
if result.is_err() {
35+
error("hstdout");
36+
}
2937
}
3038

3139
static mut HSTDERR: Option<HStderr> = None;
3240

33-
pub fn hstderr_str(s: &str) -> Result<(), ()> {
34-
interrupt::free(|_| unsafe {
41+
pub fn hstderr_str(s: &str) {
42+
let result = interrupt::free(|_| unsafe {
3543
if HSTDERR.is_none() {
3644
HSTDERR = Some(hio::hstderr()?);
3745
}
3846

3947
HSTDERR.as_mut().unwrap().write_str(s).map_err(drop)
40-
})
48+
});
49+
50+
if result.is_err() {
51+
error("hstderr");
52+
}
4153
}
4254

43-
pub fn hstderr_fmt(args: fmt::Arguments) -> Result<(), ()> {
44-
interrupt::free(|_| unsafe {
55+
pub fn hstderr_fmt(args: fmt::Arguments) {
56+
let result = interrupt::free(|_| unsafe {
4557
if HSTDERR.is_none() {
4658
HSTDERR = Some(hio::hstderr()?);
4759
}
4860

4961
HSTDERR.as_mut().unwrap().write_fmt(args).map_err(drop)
50-
})
62+
});
63+
64+
if result.is_err() {
65+
error("hstderr");
66+
}
67+
}
68+
69+
#[cold]
70+
fn error(label: &str) {
71+
panic!("failed to print to {}", label);
5172
}

src/macros.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ macro_rules! syscall {
2020
};
2121
}
2222

23-
/// Macro version of `syscall1`
23+
/// Macro version of `syscall1`.
2424
#[macro_export]
2525
macro_rules! syscall1 {
2626
($nr:ident, $a1:expr) => {
2727
$crate::syscall1($crate::nr::$nr, $a1 as usize)
2828
};
2929
}
3030

31-
/// Macro for printing to the HOST standard output
31+
/// Macro for printing to the HOST standard output.
3232
///
33-
/// This macro returns a `Result<(), ()>` value
33+
/// This is similar to the `print!` macro in the standard library. Both will panic on any failure to
34+
/// print.
3435
#[macro_export]
3536
macro_rules! hprint {
3637
($s:expr) => {
@@ -43,7 +44,8 @@ macro_rules! hprint {
4344

4445
/// Macro for printing to the HOST standard output, with a newline.
4546
///
46-
/// This macro returns a `Result<(), ()>` value
47+
/// This is similar to the `println!` macro in the standard library. Both will panic on any failure to
48+
/// print.
4749
#[macro_export]
4850
macro_rules! hprintln {
4951
() => {
@@ -57,9 +59,10 @@ macro_rules! hprintln {
5759
};
5860
}
5961

60-
/// Macro for printing to the HOST standard error
62+
/// Macro for printing to the HOST standard error.
6163
///
62-
/// This macro returns a `Result<(), ()>` value
64+
/// This is similar to the `eprint!` macro in the standard library. Both will panic on any failure
65+
/// to print.
6366
#[macro_export]
6467
macro_rules! heprint {
6568
($s:expr) => {
@@ -72,7 +75,8 @@ macro_rules! heprint {
7275

7376
/// Macro for printing to the HOST standard error, with a newline.
7477
///
75-
/// This macro returns a `Result<(), ()>` value
78+
/// This is similar to the `eprintln!` macro in the standard library. Both will panic on any failure
79+
/// to print.
7680
#[macro_export]
7781
macro_rules! heprintln {
7882
() => {
@@ -86,22 +90,23 @@ macro_rules! heprintln {
8690
};
8791
}
8892

89-
/// Macro that prints and returns the value of a given expression
90-
/// for quick and dirty debugging. Works exactly like `dbg!` in
91-
/// the standard library, replacing `eprintln` with `heprintln`,
92-
/// which it unwraps.
93+
/// Macro that prints and returns the value of a given expression for quick and
94+
/// dirty debugging.
95+
///
96+
/// Works exactly like `dbg!` in the standard library, replacing `eprintln!`
97+
/// with `heprintln!`.
9398
#[macro_export]
9499
macro_rules! dbg {
95100
() => {
96-
$crate::heprintln!("[{}:{}]", file!(), line!()).unwrap();
101+
$crate::heprintln!("[{}:{}]", file!(), line!());
97102
};
98103
($val:expr) => {
99104
// Use of `match` here is intentional because it affects the lifetimes
100105
// of temporaries - https://stackoverflow.com/a/48732525/1063961
101106
match $val {
102107
tmp => {
103108
$crate::heprintln!("[{}:{}] {} = {:#?}",
104-
file!(), line!(), stringify!($val), &tmp).unwrap();
109+
file!(), line!(), stringify!($val), &tmp);
105110
tmp
106111
}
107112
}

0 commit comments

Comments
 (0)