-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve ergonomics around lock guard #8
Comments
Actually I found a workaround but it's still rather cumbersome to deal with locks because lock guards borrow from them? It would be great to have an owned implementation that could be passed around without dealing with borrow checker or lifetimes. |
Internally I use |
Ok, I just found |
@pronebird I opened PR #9. Since internally |
@oblique looks good. Just pulled it into my project and it worked great. I rewrote my code a bit into two stage rocket to benefit from early return but otherwise now I can hide all the guts of named_lock initialization into separate function. fn main() -> ExitCode {
if run().is_ok() {
ExitCode::SUCCESS
} else {
ExitCode::FAILURE
}
}
fn run() -> Result<()> {
let _named_lock_guard = ensure_single_instance()?;
// [redacted]
Ok(())
}
fn ensure_single_instance() -> named_lock::Result<named_lock::NamedLockGuard> {
named_lock::NamedLock::create("my-unique-lock")
.and_then(|lock| lock.try_lock())
.inspect_err(|err| match err {
named_lock::Error::WouldBlock => {
eprintln!("Another instance is already running");
}
e => {
eprintln!("Couldn't acquire single instance lock: {}", e);
}
})
} |
Thanks! I just merged the changes. I will migrate to |
@oblique let me see if I can help you with |
Hi,
First of all great crate, looks solid.
However I found that it becomes a bit difficult to integrate the crate because
NamedLockGuard
borrows fromNamedLock
, consider the following helper:which doesn't compile and errors with:
Honestly I don't know how to untangle this and make it compile. Putting all that code into
main()
is not really an option for me since I don't returnResult<>
frommain()
since I don't want errors printed by default.The text was updated successfully, but these errors were encountered: