Skip to content

Commit fd77cf9

Browse files
committed
Apply clippy changes in riscv-semihosting
1 parent 483a012 commit fd77cf9

File tree

3 files changed

+42
-31
lines changed

3 files changed

+42
-31
lines changed

riscv-semihosting/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77

88
### Changed
99

10+
- Apply clippy changes
1011
- Bump riscv dependency version
1112
- Made `cfg` variable selection more robust for custom targets
1213
- Fixed debug::exit() on riscv64 QEMU simulation

riscv-semihosting/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Given this, the
1717
generally sufficient for using this library.
1818

1919
A major difference between this library and `cortex-m-semihosting` is that there
20-
are mandatory features to choose the privilege level at which the semihosting
20+
are features to choose the privilege level at which the semihosting
2121
calls are executed. The *machine-mode (M-mode)* feature will cause the macros in `export`
2222
to execute the semihosting operation in an interrupt-free context, while
2323
*user-mode (U-mode)* causes them to just execute the operation.

riscv-semihosting/src/export.rs

+40-30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! IMPLEMENTATION DETAILS USED BY MACROS
2-
32
use crate::hio::{self, HostStream};
4-
use core::fmt::{self, Write};
3+
use core::{
4+
fmt::{self, Write},
5+
ptr::addr_of_mut,
6+
};
57

68
static mut HSTDOUT: Option<HostStream> = None;
79
static mut HSTDERR: Option<HostStream> = None;
@@ -11,42 +13,46 @@ mod machine {
1113
use super::*;
1214

1315
pub fn hstdout_str(s: &str) {
14-
let _result = critical_section::with(|_| unsafe {
15-
if HSTDOUT.is_none() {
16-
HSTDOUT = Some(hio::hstdout()?);
16+
let _result = critical_section::with(|_| {
17+
let hstdout = unsafe { &mut *addr_of_mut!(HSTDOUT) };
18+
if hstdout.is_none() {
19+
*hstdout = Some(hio::hstdout()?);
1720
}
1821

19-
HSTDOUT.as_mut().unwrap().write_str(s).map_err(drop)
22+
hstdout.as_mut().unwrap().write_str(s).map_err(drop)
2023
});
2124
}
2225

2326
pub fn hstdout_fmt(args: fmt::Arguments) {
24-
let _result = critical_section::with(|_| unsafe {
25-
if HSTDOUT.is_none() {
26-
HSTDOUT = Some(hio::hstdout()?);
27+
let _result = critical_section::with(|_| {
28+
let hstdout = unsafe { &mut *addr_of_mut!(HSTDOUT) };
29+
if hstdout.is_none() {
30+
*hstdout = Some(hio::hstdout()?);
2731
}
2832

29-
HSTDOUT.as_mut().unwrap().write_fmt(args).map_err(drop)
33+
hstdout.as_mut().unwrap().write_fmt(args).map_err(drop)
3034
});
3135
}
3236

3337
pub fn hstderr_str(s: &str) {
34-
let _result = critical_section::with(|_| unsafe {
35-
if HSTDERR.is_none() {
36-
HSTDERR = Some(hio::hstderr()?);
38+
let _result = critical_section::with(|_| {
39+
let hstderr = unsafe { &mut *addr_of_mut!(HSTDERR) };
40+
if hstderr.is_none() {
41+
*hstderr = Some(hio::hstderr()?);
3742
}
3843

39-
HSTDERR.as_mut().unwrap().write_str(s).map_err(drop)
44+
hstderr.as_mut().unwrap().write_str(s).map_err(drop)
4045
});
4146
}
4247

4348
pub fn hstderr_fmt(args: fmt::Arguments) {
44-
let _result = critical_section::with(|_| unsafe {
45-
if HSTDERR.is_none() {
46-
HSTDERR = Some(hio::hstderr()?);
49+
let _result = critical_section::with(|_| {
50+
let hstderr = unsafe { &mut *addr_of_mut!(HSTDERR) };
51+
if hstderr.is_none() {
52+
*hstderr = Some(hio::hstderr()?);
4753
}
4854

49-
HSTDERR.as_mut().unwrap().write_fmt(args).map_err(drop)
55+
hstderr.as_mut().unwrap().write_fmt(args).map_err(drop)
5056
});
5157
}
5258
}
@@ -59,41 +65,45 @@ mod user {
5965

6066
pub fn hstdout_str(s: &str) {
6167
let _result = unsafe {
62-
if HSTDOUT.is_none() {
63-
HSTDOUT = Some(hio::hstdout().unwrap());
68+
let hstdout = &mut *addr_of_mut!(HSTDOUT);
69+
if hstdout.is_none() {
70+
*hstdout = Some(hio::hstdout().unwrap());
6471
}
6572

66-
HSTDOUT.as_mut().unwrap().write_str(s).map_err(drop)
73+
hstdout.as_mut().unwrap().write_str(s).map_err(drop)
6774
};
6875
}
6976

7077
pub fn hstdout_fmt(args: fmt::Arguments) {
7178
let _result = unsafe {
72-
if HSTDOUT.is_none() {
73-
HSTDOUT = Some(hio::hstdout().unwrap());
79+
let hstdout = &mut *addr_of_mut!(HSTDOUT);
80+
if hstdout.is_none() {
81+
*hstdout = Some(hio::hstdout().unwrap());
7482
}
7583

76-
HSTDOUT.as_mut().unwrap().write_fmt(args).map_err(drop)
84+
hstdout.as_mut().unwrap().write_fmt(args).map_err(drop)
7785
};
7886
}
7987

8088
pub fn hstderr_str(s: &str) {
8189
let _result = unsafe {
82-
if HSTDERR.is_none() {
83-
HSTDERR = Some(hio::hstderr().unwrap());
90+
let hstderr = &mut *addr_of_mut!(HSTDERR);
91+
if hstderr.is_none() {
92+
*hstderr = Some(hio::hstderr().unwrap());
8493
}
8594

86-
HSTDERR.as_mut().unwrap().write_str(s).map_err(drop)
95+
hstderr.as_mut().unwrap().write_str(s).map_err(drop)
8796
};
8897
}
8998

9099
pub fn hstderr_fmt(args: fmt::Arguments) {
91100
let _result = unsafe {
92-
if HSTDERR.is_none() {
93-
HSTDERR = Some(hio::hstderr().unwrap());
101+
let hstderr = &mut *addr_of_mut!(HSTDERR);
102+
if hstderr.is_none() {
103+
*hstderr = Some(hio::hstderr().unwrap());
94104
}
95105

96-
HSTDERR.as_mut().unwrap().write_fmt(args).map_err(drop)
106+
hstderr.as_mut().unwrap().write_fmt(args).map_err(drop)
97107
};
98108
}
99109
}

0 commit comments

Comments
 (0)