From 0f781ce7d76e0c88014df8a945bbfff54a578b17 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Thu, 17 Mar 2022 14:08:18 +0100 Subject: [PATCH] Fixed nits and oversight in Soc --- ctru-rs/src/gfx.rs | 1 + ctru-rs/src/lib.rs | 22 ++++++++++++---------- ctru-rs/src/romfs.rs | 2 +- ctru-rs/src/services/reference.rs | 8 ++++---- ctru-rs/src/services/soc.rs | 12 +++--------- 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/ctru-rs/src/gfx.rs b/ctru-rs/src/gfx.rs index a516fe5..6400d97 100644 --- a/ctru-rs/src/gfx.rs +++ b/ctru-rs/src/gfx.rs @@ -220,6 +220,7 @@ impl From for ctru_sys::gfx3dSide_t { #[cfg(test)] mod tests { use super::*; + use crate::Error; #[test] fn gfx_duplicate() { diff --git a/ctru-rs/src/lib.rs b/ctru-rs/src/lib.rs index 22d0b44..810c5df 100644 --- a/ctru-rs/src/lib.rs +++ b/ctru-rs/src/lib.rs @@ -27,10 +27,11 @@ pub fn init() { } #[cfg(not(test))] - _panic_hook_setup(); + panic_hook_setup(); } -fn _panic_hook_setup() { +#[cfg(not(test))] +fn panic_hook_setup() { use crate::services::hid::{Hid, KeyPad}; use std::panic::PanicInfo; @@ -45,14 +46,15 @@ fn _panic_hook_setup() { if main_thread == std::thread::current().id() && console::Console::exists() { println!("\nPress SELECT to exit the software"); - let hid = Hid::init().unwrap(); - - loop { - hid.scan_input(); - let keys = hid.keys_down(); - if keys.contains(KeyPad::KEY_SELECT) { - break; - } + match Hid::init() { + Ok(hid) => loop { + hid.scan_input(); + let keys = hid.keys_down(); + if keys.contains(KeyPad::KEY_SELECT) { + break; + } + }, + Err(e) => println!("Error while intializing Hid controller during panic: {e}"), } } }); diff --git a/ctru-rs/src/romfs.rs b/ctru-rs/src/romfs.rs index dff45ca..e054985 100644 --- a/ctru-rs/src/romfs.rs +++ b/ctru-rs/src/romfs.rs @@ -52,7 +52,7 @@ mod tests { use super::*; #[test] - fn romfs_duplicate() { + fn romfs_counter() { let _romfs = RomFS::init().unwrap(); let value = *ROMFS_ACTIVE.lock().unwrap(); diff --git a/ctru-rs/src/services/reference.rs b/ctru-rs/src/services/reference.rs index a2d1682..7fac227 100644 --- a/ctru-rs/src/services/reference.rs +++ b/ctru-rs/src/services/reference.rs @@ -2,7 +2,7 @@ use crate::Error; use std::sync::Mutex; pub(crate) struct ServiceReference { counter: &'static Mutex, - close: Box, + close: Box, } impl ServiceReference { @@ -14,9 +14,9 @@ impl ServiceReference { ) -> crate::Result where S: FnOnce() -> crate::Result<()>, - E: Fn() + 'static, + E: Fn() + Send + Sync + 'static, { - let mut value = counter.lock().unwrap(); // todo: handle poisoning + let mut value = counter.lock().expect("Mutex Counter for ServiceReference is poisoned"); // todo: handle poisoning if *value == 0 { start()?; @@ -35,7 +35,7 @@ impl ServiceReference { impl Drop for ServiceReference { fn drop(&mut self) { - let mut value = self.counter.lock().unwrap(); // should probably handle poisoning - could just map_err to ignore it. + let mut value = self.counter.lock().expect("Mutex Counter for ServiceReference is poisoned"); // todo: handle poisoning *value -= 1; if *value == 0 { (self.close)(); diff --git a/ctru-rs/src/services/soc.rs b/ctru-rs/src/services/soc.rs index 5514adf..5705d06 100644 --- a/ctru-rs/src/services/soc.rs +++ b/ctru-rs/src/services/soc.rs @@ -33,7 +33,7 @@ impl Soc { pub fn init_with_buffer_size(num_bytes: usize) -> crate::Result { let _service_handler = ServiceReference::new( &SOC_ACTIVE, - true, + false, || { let soc_mem = unsafe { memalign(0x1000, num_bytes) } as *mut u32; let r = unsafe { ctru_sys::socInit(soc_mem, num_bytes as u32) }; @@ -65,18 +65,12 @@ impl Soc { #[cfg(test)] mod tests { use super::*; + use crate::Error; #[test] fn soc_duplicate() { let _soc = Soc::init().unwrap(); - let value = *SOC_ACTIVE.lock().unwrap(); - assert_eq!(value, 1); - - drop(_soc); - - let value = *SOC_ACTIVE.lock().unwrap(); - - assert_eq!(value, 0); + assert!(matches!(Soc::init(), Err(Error::ServiceAlreadyActive))) } }