so ... is there a way to lock an Arc+Mutex in one call?
i was looking for "inline macros" or "scopeless macros", but no luck
let state_mutex = Arc::clone(&STATE_ARC);
let mut state = state_mutex.lock().unwrap();
state.key = "val";
i have tried ...
macro_rules! lock_mutex_arc {
( $mutex_arc:expr ) => {
{
let state_mutex = Arc::clone(&$mutex_arc);
let mut state = state_mutex.lock().unwrap();
state
// error[E0597]: `state_mutex` does not live long enough
$mutex_arc.as_ref().and_then(|mutex| mutex.lock().ok());
// error[E0599]: no method named `and_then` found for reference `&Mutex<PreloadState>` in the current scope
let mut state = Arc::clone(&$mutex_arc).lock().unwrap();
state
// error[E0716]: temporary value dropped while borrowed
let mut state = Arc::clone(&$mutex_arc).into_inner().unwrap();
state
// error[E0507]: cannot move out of an `Arc`
}
};
}
These two versions of the code will deadlock in the exact same situations.
Also, based on the name of STATE_ARC, it sounds like it is a global. There's generally no point in putting globals in an Arc, since the entire point of an Arc is to share the value, which the global already takes care of.