我的结构在 a 中有一个向量parking_lot::RwLock
,并且一个成员函数必须从该向量返回一个受保护的元素:
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>>`