Crates.io | parking_method |
lib.rs | parking_method |
version | 0.4.0 |
source | src |
created_at | 2022-07-22 16:36:38.257953 |
updated_at | 2022-07-22 22:54:46.027726 |
description | Obtain parking_lot locks by policy objects |
homepage | |
repository | https://github.com/cehteh/parking_method.git |
max_upload_size | |
id | 630907 |
size | 13,805 |
'parking_lot' has a lot variants for locking, there is the normal blocking lock, a try_lock, two forms of timed try locks and all of these are available as recursive variant as well.
This leads to a some explosion on the api, dispatching to these variants in generic code becomes pretty bloated. This crate abstracts all the different locking variants into a single trait with a uniform API that takes policy objects for the dispatch on underlying locking methods
// reexports parts of parking_lot as well
use parking_method::*;
let rwlock = RwLock::new(String::from("test"));
// Note the 2 following syntax forms are equivalent
assert_eq!(*Blocking.read(&rwlock).unwrap(), "test");
assert_eq!(*ReadLockMethod::read(&Blocking, &rwlock).unwrap(), "test");
assert_eq!(*TryLock.read(&rwlock).unwrap(), "test");
let timeout = Duration::from_millis(100);
assert_eq!(
*ReadLockMethod::read(&timeout, &rwlock).unwrap(),
"test"
);
assert_eq!(
*ReadLockMethod::read(
&Recursive(Instant::now() + Duration::from_millis(100)),
&rwlock).unwrap(),
"test"
);