Reputation: 985
My struct has a vector inside a parking_lot::RwLock
, and one member function has to return a guarded element from that vector:
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))
}
}
(this is the simplified core of the problem, not the real code)
The code passes type checks, but I get lifetime errors for f
.
Is there a way to modify the code so that it is sound and passes the borrow checker?
The error is:
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>>`
Upvotes: 1
Views: 808
Reputation: 985
The solution is to use RwLockReadGuard::try_map()
and to change the return type from MappedRwLockReadGuard<'_, Option<&_>>
to Option<MappedRwLockReadGuard<'a, _>>
:
impl S {
fn f(&self, i: usize) -> Option<MappedRwLockReadGuard<'_, String>> {
RwLockReadGuard::try_map(self.v.read(), |unlocked| unlocked.get(i)).ok()
}
}
Upvotes: 0