-
Notifications
You must be signed in to change notification settings - Fork 638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zip_latest Stream adapter #891
Comments
I don't have any particular opposition to this, but I haven't seen it commonly requested. The presence of it in other frameworks makes it seem like others find it useful-- what is it typically used for? |
|
When I use data streams that arrive at different times, it always need this zip_latest to combine those. |
@cnzx219 have you considered a declarative macro that expands to something like this: async_stream::stream! {
let mut it_a = <expression_one>;
let mut it_b = <expression_two>;
let mut a = None;
let mut b = None;
loop {
tokio::select! {
Some(next) = it_a.next() => { a = Some(next); }
Some(next) = it_b.next() => { b = Some(next); }
else => break,
}
if let (Some(a), Some(b)) = (a, b) {
yield (a.clone(), b.clone())
}
}
} |
Thanks for your advise. But the |
@cnzx219 no no, this is what The produced
which is the behavior of The problem is with lifetimes. use std::rc::Rc;
let zipped = zip_latest!(clone_stream, unclone_stream.map(Rc)); |
Thanks for your explanation. And I also know that there are multiple ways to implement this behavior. But I prefer the first one, it can work too. As in my first comments, I just supports that it's commonly requested. |
Has anyone implemented this yet? I'd also be keen for a similar combinator which works on |
@extremeandy |
@stephaneyfx if you are taking on this undertaking (:v), please consider including |
|
I would like your opinion on adding a
zip_latest
adapter toStreamExt
(a.k.a.combineLatest
in some reactive frameworks, but I think that "combine" does not give any indication of how items are combined).This adapter proves useful when zipping two streams that don't necessarily produce items at the same time and re-using the last produced item is desirable. For example, let's assume a system composed of two components A and B changing over time, but not necessarily simultaneously. The states of A and B can be represented by two streams SA and SB whose items are the new states of each component. The usual
zip
adapter produces a new item only when both SA and SB do, so it is not suitable to represent the state of the system as a whole. When SA produces a new item, if SB doesn't, the item freshly produced by SA should be paired with the latest item produced by SB (and vice versa). Such a stream can be used to represent the state of the system. With a diagram, this gives (self and other are Stream):Please let me know if this is something worth integrating to
futures-util
, in which case I can submit a PR.Thank you for such a great crate!
The text was updated successfully, but these errors were encountered: