Skip to content

Commit cba1178

Browse files
committed
Replace use of static mut in cortex-m-semihosting.
Will avoid a bunch of clippy warnings that are hard to get rid of in both the MSRV and latest stable.
1 parent 4b6e7f2 commit cba1178

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

cortex-m-semihosting/src/export.rs

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,68 @@
11
//! IMPLEMENTATION DETAILS USED BY MACROS
22
3-
// This must be replaced by a different solution before rust edition 2024
4-
// https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html
5-
#![allow(static_mut_refs)]
6-
3+
use core::cell::RefCell;
74
use core::fmt::{self, Write};
85

96
use crate::hio::{self, HostStream};
107

11-
static mut HSTDOUT: Option<HostStream> = None;
8+
static HSTDOUT: critical_section::Mutex<RefCell<Option<HostStream>>> =
9+
critical_section::Mutex::new(RefCell::new(None));
1210

1311
pub fn hstdout_str(s: &str) {
14-
let _result = critical_section::with(|_| unsafe {
15-
if HSTDOUT.is_none() {
16-
HSTDOUT = Some(hio::hstdout()?);
12+
critical_section::with(|cs| {
13+
let mut hstdout_opt = HSTDOUT.borrow_ref_mut(cs);
14+
if hstdout_opt.is_none() {
15+
if let Ok(hstdout) = hio::hstdout() {
16+
hstdout_opt.replace(hstdout);
17+
} else {
18+
return;
19+
}
1720
}
18-
19-
HSTDOUT.as_mut().unwrap().write_str(s).map_err(drop)
21+
let hstdout = hstdout_opt.as_mut().unwrap();
22+
let _ = hstdout.write_str(s);
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+
critical_section::with(|cs| {
28+
let mut hstdout_opt = HSTDOUT.borrow_ref_mut(cs);
29+
if hstdout_opt.is_none() {
30+
if let Ok(hstdout) = hio::hstdout() {
31+
hstdout_opt.replace(hstdout);
32+
} else {
33+
return;
34+
}
2735
}
28-
29-
HSTDOUT.as_mut().unwrap().write_fmt(args).map_err(drop)
36+
let hstdout = hstdout_opt.as_mut().unwrap();
37+
let _ = hstdout.write_fmt(args);
3038
});
3139
}
3240

33-
static mut HSTDERR: Option<HostStream> = None;
41+
static HSTDERR: critical_section::Mutex<RefCell<Option<HostStream>>> =
42+
critical_section::Mutex::new(RefCell::new(None));
3443

3544
pub fn hstderr_str(s: &str) {
36-
let _result = critical_section::with(|_| unsafe {
37-
if HSTDERR.is_none() {
38-
HSTDERR = Some(hio::hstderr()?);
45+
critical_section::with(|cs| {
46+
let mut hstderr_opt = HSTDERR.borrow_ref_mut(cs);
47+
if let Ok(hstderr) = hio::hstderr() {
48+
hstderr_opt.replace(hstderr);
49+
} else {
50+
return;
3951
}
40-
41-
HSTDERR.as_mut().unwrap().write_str(s).map_err(drop)
52+
let hstderr = hstderr_opt.as_mut().unwrap();
53+
let _ = hstderr.write_str(s);
4254
});
4355
}
4456

4557
pub fn hstderr_fmt(args: fmt::Arguments) {
46-
let _result = critical_section::with(|_| unsafe {
47-
if HSTDERR.is_none() {
48-
HSTDERR = Some(hio::hstderr()?);
58+
critical_section::with(|cs| {
59+
let mut hstderr_opt = HSTDERR.borrow_ref_mut(cs);
60+
if let Ok(hstderr) = hio::hstderr() {
61+
hstderr_opt.replace(hstderr);
62+
} else {
63+
return;
4964
}
50-
51-
HSTDERR.as_mut().unwrap().write_fmt(args).map_err(drop)
65+
let hstderr = hstderr_opt.as_mut().unwrap();
66+
let _ = hstderr.write_fmt(args);
5267
});
5368
}

0 commit comments

Comments
 (0)