Skip to content

Commit 44d297a

Browse files
committed
A rare case in which static mut is probably better
1 parent 1cd9437 commit 44d297a

File tree

1 file changed

+6
-26
lines changed

1 file changed

+6
-26
lines changed

ctru-rs/src/applets/error.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
55
use crate::services::{apt::Apt, gfx::Gfx};
66

7-
use ctru_sys::{errorConf, errorDisp};
8-
9-
use std::cell::UnsafeCell;
7+
use ctru_sys::{errorConf, errorDisp, errorInit};
108

119
/// Configuration struct to set up the Error applet.
1210
#[doc(alias = "errorConf")]
@@ -48,7 +46,7 @@ impl PopUp {
4846
pub fn new(word_wrap: WordWrap) -> Self {
4947
let mut state = Box::<errorConf>::default();
5048

51-
unsafe { ctru_sys::errorInit(state.as_mut(), word_wrap as _, 0) };
49+
unsafe { errorInit(state.as_mut(), word_wrap as _, 0) };
5250

5351
Self { state }
5452
}
@@ -96,33 +94,15 @@ impl PopUp {
9694
}
9795
}
9896

99-
struct PanicHookConfig {
100-
error_app: UnsafeCell<PopUp>,
101-
}
102-
103-
impl PanicHookConfig {
104-
fn new() -> Self {
105-
Self {
106-
error_app: UnsafeCell::new(PopUp::new(WordWrap::Enabled)),
107-
}
108-
}
109-
110-
// There can only be one invocation of an applet active at any given time, so our `UnsafeCell`
111-
// crimes *should* be okay here.
112-
unsafe fn get(&self) -> *mut errorConf {
113-
unsafe { (*self.error_app.get()).state.as_mut() }
114-
}
115-
}
116-
117-
unsafe impl Sync for PanicHookConfig {}
118-
11997
pub(crate) fn set_panic_hook(call_old_hook: bool) {
12098
use crate::services::gfx::GFX_ACTIVE;
12199
use std::sync::TryLockError;
122100

123101
let old_hook = std::panic::take_hook();
124102

125-
let config = PanicHookConfig::new();
103+
static mut ERROR_CONF: errorConf = unsafe { std::mem::zeroed() };
104+
105+
unsafe { errorInit(&raw mut ERROR_CONF, WordWrap::Enabled as _, 0) };
126106

127107
std::panic::set_hook(Box::new(move |panic_info| {
128108
// If we get a `WouldBlock` error, we know that the `Gfx` service has been initialized.
@@ -132,7 +112,7 @@ pub(crate) fn set_panic_hook(call_old_hook: bool) {
132112
old_hook(panic_info);
133113
}
134114

135-
let error_conf = unsafe { &mut *config.get() };
115+
let error_conf = unsafe { (&raw mut ERROR_CONF).as_mut().unwrap() };
136116

137117
let mut buf1 = itoa::Buffer::new();
138118

0 commit comments

Comments
 (0)