From eb8628d9bccfea168df9f1d8636c272a0265fbab Mon Sep 17 00:00:00 2001 From: Mark Drobnak Date: Sun, 29 Jan 2023 13:54:31 -0800 Subject: [PATCH] Make wait_for_event a method on Handle --- ctru-rs/examples/ir-user.rs | 23 ++++++++++++++--------- ctru-rs/src/services/srv.rs | 25 ++++++++++++++++--------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/ctru-rs/examples/ir-user.rs b/ctru-rs/examples/ir-user.rs index 934d801..9a834a0 100644 --- a/ctru-rs/examples/ir-user.rs +++ b/ctru-rs/examples/ir-user.rs @@ -2,7 +2,7 @@ use ctru::prelude::*; use ctru::services::ir_user::{CirclePadProInputResponse, IrDeviceId, IrUser}; -use ctru::services::srv; +use ctru::services::srv::HandleExt; use ctru_sys::Handle; use std::io::Write; use std::time::Duration; @@ -40,8 +40,10 @@ fn main() { } // Check if we've received a packet from the circle pad pro - let packet_received = - srv::wait_for_event(demo.receive_packet_event, Duration::ZERO).is_ok(); + let packet_received = demo + .receive_packet_event + .wait_for_event(Duration::ZERO) + .is_ok(); if packet_received { demo.handle_packets(); } @@ -128,8 +130,9 @@ impl<'screen> CirclePadProDemo<'screen> { .expect("Couldn't initialize circle pad pro connection"); // Wait for the connection to establish - if let Err(e) = - srv::wait_for_event(self.connection_status_event, Duration::from_millis(100)) + if let Err(e) = self + .connection_status_event + .wait_for_event(Duration::from_millis(100)) { if !e.is_timeout() { panic!("Couldn't initialize circle pad pro connection: {e}"); @@ -148,8 +151,9 @@ impl<'screen> CirclePadProDemo<'screen> { .expect("Failed to disconnect circle pad pro connection"); // Wait for the disconnect to go through - if let Err(e) = - srv::wait_for_event(self.connection_status_event, Duration::from_millis(100)) + if let Err(e) = self + .connection_status_event + .wait_for_event(Duration::from_millis(100)) { if !e.is_timeout() { panic!("Couldn't initialize circle pad pro connection: {e}"); @@ -174,8 +178,9 @@ impl<'screen> CirclePadProDemo<'screen> { self.print_status_info(); // Wait for the response - let recv_event_result = - srv::wait_for_event(self.receive_packet_event, Duration::from_millis(100)); + let recv_event_result = self + .receive_packet_event + .wait_for_event(Duration::from_millis(100)); self.print_status_info(); if recv_event_result.is_ok() { diff --git a/ctru-rs/src/services/srv.rs b/ctru-rs/src/services/srv.rs index 00ca358..eeaa723 100644 --- a/ctru-rs/src/services/srv.rs +++ b/ctru-rs/src/services/srv.rs @@ -7,14 +7,21 @@ use crate::error::ResultCode; use ctru_sys::Handle; use std::time::Duration; -/// Wait for an event to fire. If the timeout is reached, an error is returned. You can use -/// [`Error::is_timeout`] to check if the error is due to a timeout. -pub fn wait_for_event(event: Handle, timeout: Duration) -> crate::Result<()> { - unsafe { - ResultCode(ctru_sys::svcWaitSynchronization( - event, - timeout.as_nanos() as i64, - ))?; +/// Extension trait for [Handle] +pub trait HandleExt { + /// Wait for an event to fire. If the timeout is reached, an error is returned. You can use + /// [`Error::is_timeout`] to check if the error is due to a timeout. + fn wait_for_event(self, timeout: Duration) -> crate::Result<()>; +} + +impl HandleExt for Handle { + fn wait_for_event(self, timeout: Duration) -> crate::Result<()> { + unsafe { + ResultCode(ctru_sys::svcWaitSynchronization( + self, + timeout.as_nanos() as i64, + ))?; + } + Ok(()) } - Ok(()) }