Passing different methods as parameters to another method

I want to implement a ThreadPool struct which can take any function (with different type and no. of arguments) and execute it in a new thread. I have written a sample code below where I need to pass s1_method() and s2_method() to execute() function of ThreadPool. Is there any way to achieve this in rust? Any help on this will be appreciated.

struct s1 {
 
}

struct s2{

}

impl s1 {
    fn s1_method(&self, var1 : u32, var2 : bool){
    }
}

impl s2 {
    fn s2_method(&self, var3 : String){
    }
}

struct ThreadPool{
    fn execute(){
    }
}

You can have execute take any function that implements the FnOnce() trait, and then whoever calls it can pass a closure which internally calls s1_method with the wanted arguments. Check out how the stdlib does this with std::thread::spawn for example

2 Likes

I will check it. Thank you for quick reply.

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.