Crates.io | with_lock_auto |
lib.rs | with_lock_auto |
version | 1.0.0 |
source | src |
created_at | 2023-06-30 14:15:06.833299 |
updated_at | 2023-06-30 14:15:06.833299 |
description | A simple tool to easily and safely access arc-mutexes without |
homepage | https://crates.io/crates/with_lock_auto |
repository | https://github.com/vaqxai/rust_with_lock |
max_upload_size | |
id | 904545 |
size | 5,473 |
A simple locking library that allows you to easily and safely access the interior of Arc
Adds the following methods to each Arc<Mutex
mutex.with_lock(|data| {
// do stuff
});
// this is the same but uses try_lock instead of lock().unwrap()
mutex.with_lock_try(|data| {
// do stuff
});
The closure can also return any type
You can also clone the inner value simply by doing:
let inner_value_clone = mutex.get_clone(); // if you're sure you can lock the mutex
let inner_value_clone_optional = mutex.try_get_clone(); // if you want an option that's none when the mutex can't be locked (uses try_lock internally)
But the inner value of course needs to implement Clone
does not introduce new types and works on all existing Arc<Mutex