Browse Source

Improve some service start/stop error handling

pull/86/head
AzureMarker 1 year ago
parent
commit
87f33f144e
  1. 6
      ctru-rs/src/error.rs
  2. 15
      ctru-rs/src/services/ir_user.rs

6
ctru-rs/src/error.rs

@ -94,6 +94,8 @@ pub enum Error {
/// Size of the requested data (in bytes). /// Size of the requested data (in bytes).
wanted: usize, wanted: usize,
}, },
/// An error that doesn't fit into the other categories.
Other(String),
} }
impl Error { impl Error {
@ -153,6 +155,7 @@ impl fmt::Debug for Error {
.field("provided", provided) .field("provided", provided)
.field("wanted", wanted) .field("wanted", wanted)
.finish(), .finish(),
Self::Other(err) => f.debug_tuple("Other").field(err).finish(),
} }
} }
} }
@ -175,7 +178,8 @@ impl fmt::Display for Error {
Self::OutputAlreadyRedirected => { Self::OutputAlreadyRedirected => {
write!(f, "output streams are already redirected to 3dslink") write!(f, "output streams are already redirected to 3dslink")
} }
Self::BufferTooShort{provided, wanted} => write!(f, "the provided buffer's length is too short (length = {provided}) to hold the wanted data (size = {wanted})") Self::BufferTooShort{provided, wanted} => write!(f, "the provided buffer's length is too short (length = {provided}) to hold the wanted data (size = {wanted})"),
Self::Other(err) => write!(f, "{err}"),
} }
} }
} }

15
ctru-rs/src/services/ir_user.rs

@ -1,5 +1,6 @@
use crate::error::ResultCode; use crate::error::ResultCode;
use crate::services::ServiceReference; use crate::services::ServiceReference;
use crate::Error;
use ctru_sys::{Handle, MEMPERM_READ, MEMPERM_READWRITE}; use ctru_sys::{Handle, MEMPERM_READ, MEMPERM_READWRITE};
use std::alloc::Layout; use std::alloc::Layout;
use std::cmp::max; use std::cmp::max;
@ -107,14 +108,22 @@ impl IrUser {
recv_buffer_size, recv_buffer_size,
recv_packet_count, recv_packet_count,
}; };
*IR_USER_STATE.lock().unwrap() = Some(user_state); let mut ir_user_state = IR_USER_STATE
.lock()
.map_err(|e| Error::Other(format!("Failed to write to IR_USER_STATE: {e}")))?;
*ir_user_state = Some(user_state);
Ok(()) Ok(())
}, },
|| { || {
// Remove our service state from the global location // Remove our service state from the global location
let mut shared_mem_guard = IR_USER_STATE.lock().unwrap(); let mut shared_mem_guard = IR_USER_STATE
let shared_mem = shared_mem_guard.take().unwrap(); .lock()
.expect("Failed to write to IR_USER_STATE");
let Some(shared_mem) = shared_mem_guard.take() else {
// If we don't have any state, then we don't need to clean up.
return;
};
(move || unsafe { (move || unsafe {
// Close service and memory handles // Close service and memory handles

Loading…
Cancel
Save