Open baadc0de opened 1 year ago
This looks like it could work for me - the resulting Streams own their own kv::Store and can be put in a StreamMap.
pub fn watch_bucket_as<T: DeserializeOwned>(store: kv::Store, key: String) -> impl Stream<Item = anyhow::Result<Option<T>>> + Unpin {
Box::pin(stream! {
let Ok(stream) = store.watch(key).await else { return; };
pin_mut!(stream);
while let Some(entry) = stream.next().await {
match entry {
| Ok(entry) => {
match entry.operation {
| kv::Operation::Put => {
yield serde_json::from_slice(&entry.value).map_err(serde_err).map(Some);
}
| kv::Operation::Delete | kv::Operation::Purge => {
yield Ok(None);
}
}
}
| Err(err) => {
yield Err(nats_err(err));
}
}
}
})
}
Use Case:
The sync NATS client's
kv::Watch
struct does not have a lifetime parameter, while the async_nats version does. This complicates ownership when trying to own akv::Store
and atokio_stream StreamMap
ofkv::Watch
instances in the same struct.Proposed Change:
Make
async_nats kv::Watch
not have a lifetime parameter to match sync natskv::Watch
.Who Benefits From The Change(s)?
Users of multiple
kv::Watch
in a single tokioselect!
loop.Alternative Approaches
Somehow manhandle rust borrow checker into allowing the borrow. Maybe rebuild
StreamMap
on each loop iteration?