Add clonable FutureStateChange

This commit is contained in:
hodasemi 2024-04-04 08:23:46 +02:00
parent 4bf0607a11
commit 1baf6a488e

View file

@ -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<dyn 'a + FutureStateChange>
where
Self: 'a;
}
impl<F: Fn() -> Result<()> + Clone + Send + Sync> FutureStateChange for F {
fn clone_box<'a>(&self) -> Box<dyn 'a + FutureStateChange>
where
Self: 'a,
{
Box::new(self.clone())
}
}
impl<'a> Clone for Box<dyn 'a + FutureStateChange> {
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<Option<&'b str>>,
) -> Result<impl Fn() -> Result<()> + Clone + Send + Sync> {
) -> Result<Box<dyn FutureStateChange>> {
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,
)
})
}))
}
}