问题
I have this method from ExchangeInfo
struct that returns a RwLockReadGuardRef
from owning_ref
crate:
pub fn get_pair<'a, 'me: 'a>(
&'me self,
name: &str,
) -> RwLockReadGuardRef<'a, TradePairHashMap, Arc<RwLock<TradePair>>> {
RwLockReadGuardRef::new(self.pairs.read().unwrap()).map(|pairs| pairs.get(name).unwrap())
}
I'm trying to call it here:
pub async fn get_pair<'a>(
name: &str,
exchange_info: &'a ExchangeInfo,
retrieval: &dyn ExchangeInfoRetrieval,
refresh: bool,
) -> Result<OwningRef<&'a ExchangeInfo, RwLockReadGuard<'a, TradePair>>> {
if refresh {
if let Err(err) = exchange_info.refresh(retrieval).await {
return Err(err);
}
}
Ok(OwningRef::new(exchange_info).map(|e| &e.get_pair(name).read().unwrap()))
}
Unfortunately, I get the following error:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> shared/src/exchange_info.rs:25:49
|
25 | Ok(OwningRef::new(exchange_info).map(|e| &e.get_pair(name).read().unwrap()))
| ^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 25:42...
--> shared/src/exchange_info.rs:25:42
|
25 | Ok(OwningRef::new(exchange_info).map(|e| &e.get_pair(name).read().unwrap()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that reference does not outlive borrowed content
--> shared/src/exchange_info.rs:25:47
|
25 | Ok(OwningRef::new(exchange_info).map(|e| &e.get_pair(name).read().unwrap()))
| ^
note: but, the lifetime must be valid for the lifetime `'a` as defined on the function body at 13:23...
--> shared/src/exchange_info.rs:13:23
|
13 | pub async fn get_pair<'a>(
| ^^
note: ...so that the expression is assignable
--> shared/src/exchange_info.rs:25:5
|
25 | Ok(OwningRef::new(exchange_info).map(|e| &e.get_pair(name).read().unwrap()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: expected `std::result::Result<owning_ref::OwningRef<&'a exchange_info::ExchangeInfo, std::sync::RwLockReadGuard<'a, _>>, _>`
found `std::result::Result<owning_ref::OwningRef<&exchange_info::ExchangeInfo, std::sync::RwLockReadGuard<'_, _>>, _>`
How can I fix this ?
There is the complete playground
Thanks for your answers :)
来源:https://stackoverflow.com/questions/63523566/how-to-handle-cannot-infer-an-appropriate-lifetime-for-autoref-due-to-conflicti