我正在try 使用parking_lot
来编写一个代码块,其中每当我没有对RwLock
的引用时,我就使用它的属性:
let property = if i_already_have_the_reference {
simple_mut_reference_to_property
} else {
my_lock.write().map(|v| v.property)
};
但是,这不起作用,因为第一个分支是简单的&mut MyReference
类型,而第二个分支是MappedRwLockWriteGuard
类型.
如果我用&mut
取消对MappedRwLockWriteGuard
的引用,编译器将在Else分支的末尾删除临时变量.
所以,我能想到的唯一解决方案是从simple_mut_reference_to_property
创建一个虚拟的MappedRwLockWriteGuard
,这实际上不会解锁Drop上的锁.
你对解决这个问题有什么建议?
如果不可能,是否有允许手动锁定/解锁C-Way的API?
附言:我之所以用parking_lot
而不是std::sync
是因为我认为我可以用.map
解决这个问题