|
1 | 1 | //! IMPLEMENTATION DETAILS USED BY MACROS
|
2 | 2 |
|
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; |
7 | 4 | use core::fmt::{self, Write};
|
8 | 5 |
|
9 | 6 | use crate::hio::{self, HostStream};
|
10 | 7 |
|
11 |
| -static mut HSTDOUT: Option<HostStream> = None; |
| 8 | +static HSTDOUT: critical_section::Mutex<RefCell<Option<HostStream>>> = |
| 9 | + critical_section::Mutex::new(RefCell::new(None)); |
12 | 10 |
|
13 | 11 | 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 | + } |
17 | 20 | }
|
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); |
20 | 23 | });
|
21 | 24 | }
|
22 | 25 |
|
23 | 26 | 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 | + } |
27 | 35 | }
|
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); |
30 | 38 | });
|
31 | 39 | }
|
32 | 40 |
|
33 |
| -static mut HSTDERR: Option<HostStream> = None; |
| 41 | +static HSTDERR: critical_section::Mutex<RefCell<Option<HostStream>>> = |
| 42 | + critical_section::Mutex::new(RefCell::new(None)); |
34 | 43 |
|
35 | 44 | 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; |
39 | 51 | }
|
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); |
42 | 54 | });
|
43 | 55 | }
|
44 | 56 |
|
45 | 57 | 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; |
49 | 64 | }
|
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); |
52 | 67 | });
|
53 | 68 | }
|
0 commit comments