Hello.
I would like save Closure in my struct GenericVar as WriteCallbackClosure.
Please help.
Code:
pub trait ThreadSafe : Send + Sync { } impl < __ : ? Sized, > ThreadSafe for __ where Self : Send + Sync
{ }
pub trait MyClosure<T> : Fn(String, T) -> Result<(), String> + std::panic::UnwindSafe + std::panic::RefUnwindSafe + ThreadSafe + 'static
{ }
impl < __ : ? Sized, T> MyClosure<T> for __ where Self : Fn(String, T) -> Result<(), String> + std::panic::UnwindSafe + std::panic::RefUnwindSafe, __: ThreadSafe + 'static
{ }
pub enum WriteCallbackClosure
{
BOOL(Box<dyn MyClosure<bool>>),
U8(Box<dyn MyClosure<u8>>),
U16(Box<dyn MyClosure<u16>>),
U24(Box<dyn MyClosure<u32>>),
U32(Box<dyn MyClosure<u32>>),
I8(Box<dyn MyClosure<i8>>),
I16(Box<dyn MyClosure<i16>>),
I32(Box<dyn MyClosure<i32>>),
F32(Box<dyn MyClosure<f32>>),
F64(Box<dyn MyClosure<f64>>),
}
pub struct GenericVar
{
pub write_cb_closure: Option<WriteCallbackClosure>,
pub name: String,
}
pub fn create_control_variable<Closure, T>(var_name: &String, var_type: &String, func: Closure ) -> GenericVar
where Closure : 'static + ThreadSafe + MyClosure<T> + MyClosure<T> + Clone, T: Any + Sized + std::fmt::Display + Default + Send + std::panic::UnwindSafe
{
// if var_type == BOOL
let mut var = GenericVar{name: var_name.clone(), write_cb_closure: None };
//var.write_cb_closure = Some(WriteCallbackClosure::BOOL(Box::new(func)));
//var.write_cb_closure = Some(WriteCallbackClosure::BOOL(&Box::new(func)));
//var.write_cb_closure = Some(WriteCallbackClosure::BOOL(Box::new(&Box::new(func))));
return var;
}
fn main()
{
//create_control_variable(&"test".to_string(), &"BOOL".to_string(), |name, state| {
// println!("name: {}, state: {}", name, state);
// return Ok(());
// });
}
Playground:CLICK ME!!!