I am calling a function from a thread with a signature of
type ElementDataReceiver = Option<Arc<Mutex<Receiver<ElementStreamFormat>>>>;
fn run(&self, input: ElementDataReceiver) -> Result<()>
There will be a lot of these functions so getting a mutex guard from input will be a pain so I wanted to make a macro to do this.
I have something like this
macro_rules! unlock_or_none {
($var:expr) => {
{
let arc = $var.and_then(|arc| Arc::try_unwrap(arc).ok());
let mutex_guard : Option<MutexGuard<Receiver<ElementStreamFormat>>> = arc.and_then(|mutex| mutex.lock().ok());
mutex_guard
}
};
}
In run I want to unlock the passed in parameter with
let input1_lock = unlock_or_none!(input);
If any part on the unlock fails I want input1_lock to be None
My run function will be something like
fn run(&self,input: ElementDataReceiver) -> Result<()> {
let input1_lock = unlock_or_none!(input1);
loop {
match input1_lock {
Some(lock) => {
let val1 = lock.recv().unwrap();
},
None => {},
}
}
}
This results in rc.and_then(|mutex| mutex.lock().ok());
| ----- ^ mutex
dropped here while still borrowed
I understand why but was wondering if there was a better way to construct this macro so the mutex remains in scope to the caller ?
Thanks