Create MappedRwLockWriteGuard from a reference in if/else in Rust

huangapple go评论68阅读模式
英文:

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
};

huangapple
  • 本文由 发表于 2023年5月21日 18:20:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定