Storing generic asynchronous functions that require &mut self inside a struct

I need to write an interface for an API where I can access various async methods that take &mut self.
Since I'm writing an interface I have another struct to store the API and then implement the methods to call the API.

struct Foo {
    // Bar API interface
    bar: Bar,
    runner: Runner<Self>,
}

With help from this post (How can I define struct that holds futures of function which functions will return a future - #34 by kpreid) I was able to store basic async functions that take no arguments but now I'm having trouble modifying it to take &mut self since apparently it's not Send I'm not sure why or how to fix it.

Simplified code:

Many thanks!

&'static mut is almost never a useful type. You've probably got the 'static from a poor compiler suggestion. When the compiler says you must have a 'static lifetime, what it it's really trying to say is that temporary references are forbidden and won't work, and you should replace &mut T with Arc<Mutex<T>> instead, or redesign the whole architecture to avoid needing this at all.

In practice async runners don't support use of references, or any explicit scope-based mutability at all. They support T: Clone, and that's it. From there users of the type can choose if they make the T something trivially copyable, or Arc<immutable> or Arc<Mutex<something to actually mutate>>.

Rust is incredibly strict about exclusivity and temporary nature of scope-bound loans (&mut), so most of the time you won't be able to do anything non-trivial with them. They are not a way to refer to objects in a genral-purpose way, and they are not required for mutability. They're primarily to enforce temporary exclusive access.

From what I understand you are trying to execute the async methods Foo::_mul32 and Foo::_div64 at the same time using join_all. This can't happen since both of those methods take &mut self which is an exclusive reference and you have only one instance of Foo. You can't have 2 &mut Foo references to the Foo instance at the same time.

Okay, thanks for your response. This makes me be in a weird situation because the Bar API I had in my simplified version runs commands over the network and takes &mut self. The problem is that if I can't do a join_all then the functions are blocking and I need to wait for every connection to finish which is a problem since some functions are based on user input so it could take forever to run.

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.