英文:
Create MappedRwLockWriteGuard from a reference in if/else in Rust
问题
let property = if i_already_have_the_reference {
&simple_mut_reference_to_property.property
} else {
my_lock.write().map(|v| &v.property).unwrap()
};
英文:
I am trying to use parking_lot
to write a block of code where I am using the property of a RwLock
whenever I don't already have a reference to it:
let property = if i_already_have_the_reference {
simple_mut_reference_to_property
} else {
my_lock.write().map(|v| v.property)
};
However, this doesn't work since the first branch is a simple &mut MyReference
type, and the second branch is a MappedRwLockWriteGuard
.
If I dereference MappedRwLockWriteGuard
with &mut
, the compiler will drop the temporary at the end of the else branch.
So, the only solution I can think of is to create a dummy MappedRwLockWriteGuard
from simple_mut_reference_to_property
, which doesn't actually unlock a lock on the drop.
What is your suggestion to fix this problem?
If impossible, is there an API that allows manual lock/unlocking of the C-way?
P.S: the reason I use parking_lot
instead of std::sync
is that I thought I can fix the issue with .map
答案1
得分: 3
以下是翻译好的部分:
"在这种情况下的一个有用模式是拥有一个有时被初始化有时不被初始化的变量。您可以将锁保存在该变量中:"
let mut write_guard;
let property = if i_already_have_the_reference {
simple_mut_reference_to_property
} else {
write_guard = my_lock.write();
&mut write_guard.property
};
英文:
A useful pattern in cases like that is to have a variable that is sometimes initialized and sometimes not. You can hold the lock in that variable:
let mut write_guard;
let property = if i_already_have_the_reference {
simple_mut_reference_to_property
} else {
write_guard = my_lock.write();
&mut write_guard.property
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论