Continuing futures in a specific order
⚓ Rust 📅 2026-01-15 👤 surdeus 👁️ 1I'm working on a library for a protocol where each protocol object can receive events. I thought it would be interesting to use async for this. I'm using async fns on objects for events, so to receive an event you just await on a method. An object can have multiple events, and I would like to have the ability for events that are not being listened for to just be dropped.
The issue I'm currently running into is once an event comes through, I want the event futures to be completed in the order that they are received, because event ordering in the protocol is important. For example, if you have these objects:
struct A;
impl A {
async fn event1(&self);
async fn event2(&self);
}
struct B;
impl B {
async fn event1(&self);
}
And then you await them in whatever order:
use futures_lite::FutureExt;
a.event1().or(a.event2()).or(b.event1()).await
And you receive the events like:
A::event2B::event1A::event1
Then the futures should also be completed in this order. The first thing that comes to mind is to have a queue/channel of events, signal a given event when its event is at the top of the queue, have the event signal back when it's pulled the event, and then move on to the next event. But this seems like a lot of switching back and forth, and maybe there's a more straightforward method of achieving this I'm not thinking of. Any ideas?
3 posts - 2 participants
🏷️ Rust_feed