Return 来自 parking_lot::RwLock 的映射数据

Return mapped data from parking_lot::RwLock

我的结构在 parking_lot::RwLock 中有一个向量,一个成员函数必须 return 来自该向量的受保护元素:

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() 并将 return 类型从 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()
    }
}