Generally, this means using Rc
or Arc
, depending on threading requirements. In the case of tokio, which is a single-threaded event loop, Rc
is enough.
In your case, I don't think you need a Mutex
(unless the real code does, which isn't visible in the playground). You can wrap the HashMap
in a RefCell
, which is like a single-threaded mutex.
So something like this for your example:
pub struct Coordinator {
// Rc<RefCell<>> wrapper
sessions : Rc<RefCell<HashMap<String, String>>,
}
...
// Make a clone - this gives you an "owned" handle to the data, even though all clones reference the same
// piece of data - this is shared ownership
let user_session_clone = Rc::clone(&self.sessions);
// Add `move` so the closure takes ownership of `user_session_clone`. Now your closure doesn't capture
// any references from its environment, and is therefore 'static
let us_f = user_sessions.for_each(move |user_session| {
let m = &mut *user_session_clone.borrow_mut();
m.insert( "".to_owned(), " ".to_owned() );
future::ok( () )
});
core.handle().spawn( us_f );