How to save MyClosure<T> as enum?

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!!!

you can implement

impl From<MyClosure<bool> for WriteCallbackClosure {
    fn from(c: MyClosure<bool>) -> Self { … }
}

(and for all others)
and then add a trait bound to create_control_variable
Closure: Into<WriteCallbackClosure>

1 Like

Solution: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=9bc39d3f735c6a4962b13de1ba4ab8ef

Solution2: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=1df422d163fdab8316962515415f0bb8

Solution3:https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=ddbda197417c6e9b3d55ddd929bee7d0
is still trying to get rid of <T: Any> from WriteCallbackClosure

error[E0277]: expected a `std::ops::Fn<(std::string::String, bool)>` closure, found `Closure`
  --> src/main.rs:67:66
   |
67 |         self.write_cb_closure =  Some(WriteCallbackClosure::BOOL(Box::new(callback)));
   |                                                                  ^^^^^^^^^^^^^^^^^^
   |                                                                  |
   |                                                                  expected an implementor of trait `MyClosure<bool>`
   |                                                                  help: consider borrowing here: `&Box::new(callback)`
   |
   = note: the trait bound `Closure: MyClosure<bool>` is not satisfied
   = note: required because of the requirements on the impl of `MyClosure<bool>` for `Closure`
   = note: required for the cast to the object type `dyn MyClosure<bool, Output = std::result::Result<(), std::string::String>>

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.