我的结构在parking_lot::RwLock
中有一个向量,并且一个成员函数必须从该向量返回一个 protected 元素:
use parking_lot::*;
struct S {
v: RwLock<Vec<String>>,
}
impl S {
fn f(&self, i: usize) -> MappedRwLockReadGuard<'_, Option<&String>> {
RwLockReadGuard::map(self.v.read(), |unlocked| &unlocked.get(i))
}
}
(这是问题的简化核心,而不是实际代码)
该代码通过了类型检查,但是
f
出现了生命周期错误。有没有一种方法可以修改代码,使其声音正确并通过借阅检查器?
错误是:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
--> src/lib.rs:9:66
|
9 | RwLockReadGuard::map(self.v.read(), |unlocked| &unlocked.get(i))
| ^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 9:45...
--> src/lib.rs:9:45
|
9 | RwLockReadGuard::map(self.v.read(), |unlocked| &unlocked.get(i))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that reference does not outlive borrowed content
--> src/lib.rs:9:57
|
9 | RwLockReadGuard::map(self.v.read(), |unlocked| &unlocked.get(i))
| ^^^^^^^^
note: but, the lifetime must be valid for the anonymous lifetime #1 defined on the method body at 8:5...
--> src/lib.rs:8:5
|
8 | / fn f(&self, i: usize) -> MappedRwLockReadGuard<'_, Option<&String>> {
9 | | RwLockReadGuard::map(self.v.read(), |unlocked| &unlocked.get(i))
10 | | }
| |_____^
note: ...so that the expression is assignable
--> src/lib.rs:9:9
|
9 | RwLockReadGuard::map(self.v.read(), |unlocked| &unlocked.get(i))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: expected `lock_api::rwlock::MappedRwLockReadGuard<'_, _, std::option::Option<&std::string::String>>`
found `lock_api::rwlock::MappedRwLockReadGuard<'_, _, std::option::Option<&std::string::String>>`
最佳答案
解决方案是使用RwLockReadGuard::try_map()
并将返回类型从MappedRwLockReadGuard<'_, Option<&_>>
更改为Option<MappedRwLockReadGuard<'a, _>>
:
impl S {
fn f(&self, i: usize) -> Option<MappedRwLockReadGuard<'_, String>> {
RwLockReadGuard::try_map(self.v.read(), |unlocked| unlocked.get(i)).ok()
}
}
关于rust - 从parking_lot::RwLock返回映射的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60452298/