3个版本
0.17.2 | 2023年9月7日 |
---|---|
0.17.1 | 2023年9月7日 |
0.17.0 | 2023年6月27日 |
0.16.4 |
|
0.12.1 |
|
在 并发 中排名 413
每月下载量 79
27KB
494 行
提供统一单线程代码和基于RwLocks代码的接口。
这个特质的副作用是,当借检查器被激活以检查当前作用域中引入的死锁时,表现得很好。这是因为现在只能在对可变引用的调用中使用 Self::write
。
示例
use parking_lot::RwLock;
use read_write_api::{
DowngradableWriteGuard,
RwApi,
RwApiWrapper,
RwApiWrapperOwned,
UpgradableReadGuard,
};
fn do_something(mut x: impl RwApi<Target=u64>) -> u64 {
let guard = x.upgradable_read();
if *guard == 1 {
let mut guard = guard.upgrade_to_downgradable();
*guard = 2;
*guard.downgrade()
} else {
*guard
}
}
assert_eq!(do_something(RwApiWrapperOwned(1)), 2);
assert_eq!(do_something(RwApiWrapperOwned(3)), 3);
assert_eq!(do_something(&mut RwApiWrapperOwned(1)), 2);
assert_eq!(do_something(&mut RwApiWrapperOwned(3)), 3);
assert_eq!(do_something(RwLock::new(1)), 2);
assert_eq!(do_something(RwLock::new(3)), 3);
assert_eq!(do_something(&RwLock::new(1)), 2);
assert_eq!(do_something(&RwLock::new(3)), 3);
assert_eq!(do_something(&mut RwLock::new(1)), 2);
assert_eq!(do_something(&mut RwLock::new(3)), 3);
fn do_something_ref<'a>(mut x: impl RwApi<Target=&'a mut u64>) -> u64 {
if **x.read() == 1 {
**x.write() = 2;
**x.read()
} else {
**x.read()
}
}
assert_eq!(do_something_ref(RwApiWrapper(&mut 1)), 2);
assert_eq!(do_something_ref(RwApiWrapper(&mut 3)), 3);
assert_eq!(do_something_ref(&mut RwApiWrapper(&mut 1)), 2);
assert_eq!(do_something_ref(&mut RwApiWrapper(&mut 3)), 3);
assert_eq!(do_something_ref(RwLock::new(&mut 1)), 2);
assert_eq!(do_something_ref(RwLock::new(&mut 3)), 3);
assert_eq!(do_something_ref(&RwLock::new(&mut 1)), 2);
assert_eq!(do_something_ref(&RwLock::new(&mut 3)), 3);
assert_eq!(do_something_ref(&mut RwLock::new(&mut 1)), 2);
assert_eq!(do_something_ref(&mut RwLock::new(&mut 3)), 3);
依赖关系
~0.4–5.5MB
~11K SLoC