From 1baf6a488e1858f9a7d13d9819ac66c0076cb6ec Mon Sep 17 00:00:00 2001 From: hodasemi Date: Thu, 4 Apr 2024 08:23:46 +0200 Subject: [PATCH] Add clonable FutureStateChange --- src/states.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/states.rs b/src/states.rs index 503d4d2..f28e978 100644 --- a/src/states.rs +++ b/src/states.rs @@ -7,6 +7,27 @@ use std::collections::HashMap; use std::sync::{Arc, Mutex, RwLock}; +pub trait FutureStateChange: Fn() -> Result<()> + Send + Sync { + fn clone_box<'a>(&self) -> Box + where + Self: 'a; +} + +impl Result<()> + Clone + Send + Sync> FutureStateChange for F { + fn clone_box<'a>(&self) -> Box + where + Self: 'a, + { + Box::new(self.clone()) + } +} + +impl<'a> Clone for Box { + fn clone(&self) -> Self { + (**self).clone_box() + } +} + struct State { name: String, @@ -295,7 +316,7 @@ impl States { pub fn future_state_change<'b>( &self, id: impl Into>, - ) -> Result Result<()> + Clone + Send + Sync> { + ) -> Result> { let state = id.into().map(|id| self.get_state(id)).transpose()?; let current_state = self.current_state.clone(); let gui_handler = if self.control_top_gui { @@ -305,14 +326,14 @@ impl States { }; let logging = self.log_state_change; - Ok(move || { + Ok(Box::new(move || { Self::_set_state( state.clone(), &mut *current_state.lock().unwrap(), gui_handler.clone(), logging, ) - }) + })) } }