This simple piece of code is driving me nuts:
pub fn load_any<'a, T, K>(&self, key: K) -> Result<Option<T>, kv::Error> where
T: 'a + Persistent + Debug + Serialize + for<'de> Deserialize<'de>,
K: 'a + kv::Key<'a>,
{
let bucket = self.store.bucket::<K,kv::Bincode<T>>(Some(T::bucket_name()))?;
bucket
.get(key)?.unwrap();
Ok(None)
}
the error is:
--> src/persist.rs:47:9
|
42 | pub fn load_any<'a, T, K>(&self, key: K) -> Result<Option<T>, kv::Error> where
| -- lifetime `'a` defined here
...
47 | bucket
| -^^^^^
| |
| _________borrowed value does not live long enough
| |
48 | | .get(key)?.unwrap();
| |_____________________- argument requires that `bucket` is borrowed for `'a`
...
51 | }
| - `bucket` dropped here while still borrowed
I tried almost everything (including cloning key and messing up with lifetimes) but I can't understand how to make it work.
The real code was different (I obviously do something with value returned by get) but error is the same.
Get result is not returned and bucket is used only to call get().
Rust compiler complains that bucket is dropped while still borrowed but nothing is borrowing it.
The same code works perfectly replacing the generic K with a typed var:
pub fn load_str<'a, T>(&self, key: &str) -> Result<Option<T>, kv::Error> where
T: 'a + Persistent + Debug + Serialize + for<'de> Deserialize<'de>,
{
let bucket = self.store.bucket::<&str,kv::Bincode<T>>(Some(T::bucket_name()))?;
bucket
.get(key)?.unwrap();
Ok(None)
// This code works perfectly
}
Code uses the kv library.