使用泛型生命周期时,借用值的寿命不够长。</p>
fn get<'a, T>(&self, key: &[u8]) -> Result<Option<T>>
where
T: Deserialize<'a>,
{
match self.db.get(key)? {
Some(dbv) => {
let v = serde_json::from_slice(&*dbv)?;
Ok(Some(v))
}
None => Ok(None),
}
}
函数 db.get 声明:
pub fn get(&self, key: &[u8]) -> Result<Option<DBVector>, String>
函数 serde_json::from_slice 声明:
pub fn from_slice<'a, T>(v: &'a [u8]) -> Result<T>
where
T: de::Deserialize<'a>
错误信息:
error[E0597]: `dbv` does not live long enough
--> libs/block-db/src/index.rs:106:50
|
100 | fn get<'a, T>(&self, key: &[u8]) -> Result<Option<T>>
| -- lifetime `'a` defined here
...
106 | let v = serde_json::from_slice(&*dbv)?;
| -------------------------^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `dbv` is borrowed for `'a`
...
109 | None => Ok(None),
| - `dbv` dropped here while still borrowed
error: aborting due to previous erro