Hi,
I'm trying to use the etcd crate to watch keys for changes, deserialize the new value for the key and return that through a channel:
use etcd_client::{Client, Error, WatchOptions};
use serde::Deserialize;
use serde_json;
use tokio::sync::mpsc;
pub async fn watch<'a, T>(mut client: Client, prefix: String) -> Result<mpsc::Receiver<T>, Error>
where
T: Deserialize<'a>,
{
let (_, mut stream) = client.watch(prefix, Some(WatchOptions::new())).await?;
let (tx, rx) = mpsc::channel::<T>(32);
while let Some(ref r) = stream.message().await? {
for e in r.events() {
match e.kv() {
Some(kv) => {
let kv = kv.value();
let value: T = serde_json::from_slice(kv).unwrap();
tx.send(value).await;
}
None => {}
}
}
}
return Ok(rx);
But that does not compile:
error[E0716]: temporary value dropped while borrowed
--> src/lib.rs:14:29
|
6 | pub async fn watch<'a, T>(mut client: Client, prefix: String) -> Result<mpsc::Receiver<T>, Error>
| -- lifetime `'a` defined here
...
14 | while let Some(ref r) = stream.message().await? {
| ^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
...
18 | let value: T = serde_json::from_slice(kv.value().clone()).unwrap();
| ------------------------------------------ argument requires that borrow lasts for `'a`
...
24 | }
| - temporary value is freed at the end of this statement
Does anyone have an idea on how to make this work?