From 98e9b144a1f10ff2ef9f04b39db3d0a1c6d72a43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ant=C3=B4nio=20Cardoso?= Date: Fri, 9 Jun 2023 17:43:15 -0300 Subject: [PATCH] src: mavlink: Avoid Clone to avoid unwanted Drops --- src/mavlink/mavlink_camera.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/mavlink/mavlink_camera.rs b/src/mavlink/mavlink_camera.rs index 8679bc82..d9c6c40e 100644 --- a/src/mavlink/mavlink_camera.rs +++ b/src/mavlink/mavlink_camera.rs @@ -19,7 +19,7 @@ lazy_static! { static ref ID_CONTROL: Arc>> = Arc::new(Mutex::new(vec![])); } -#[derive(Clone, Debug)] +#[derive(Debug)] // Do not Clone this, otherwise it's very likely to have unwanted Drop calls #[allow(dead_code)] pub struct MavlinkCameraComponent { // MAVLink specific information @@ -39,7 +39,6 @@ pub struct MavlinkCameraComponent { thermal: bool, } -#[derive(Clone)] pub struct MavlinkCameraInformation { component: MavlinkCameraComponent, mavlink_connection_string: String, @@ -234,15 +233,17 @@ impl MavlinkCameraHandle { let heartbeat_state = thread_state.clone(); let receive_message_state = thread_state.clone(); - let component = match heartbeat_mavlink_information.lock() { - Ok(guard) => guard, - Err(error) => return Err(anyhow!("Failed locking a Mutex. Reason: {error}")), - } - .component - .to_owned(); + let (system_id, component_id) = { + let mavlink_information = match heartbeat_mavlink_information.lock() { + Ok(guard) => guard, + Err(error) => return Err(anyhow!("Failed locking a Mutex. Reason: {error}")), + }; - let system_id = component.system_id; - let component_id = component.component_id; + ( + mavlink_information.component.system_id, + mavlink_information.component.component_id, + ) + }; Ok(Self { mavlink_camera_information, @@ -287,7 +288,7 @@ fn heartbeat_loop( ThreadState::Running => (), ThreadState::Restart => { *vehicle.write().as_deref_mut().unwrap() = - reconnect(&mavlink_camera_information.lock().unwrap().clone()); + reconnect(&mavlink_camera_information.lock().unwrap()); *state = ThreadState::Running; } ThreadState::Zombie => continue, @@ -336,7 +337,7 @@ fn receive_message_loop( ThreadState::Running => (), ThreadState::Restart => { let information = lock_or_return_error!(mavlink_camera_information); - *vehicle.write().as_deref_mut().unwrap() = reconnect(&information.clone()); + *vehicle.write().as_deref_mut().unwrap() = reconnect(&information); *state = ThreadState::Running; } ThreadState::Zombie => {