Prevent type from being held across await point

⚓ Rust    📅 2026-03-18    👤 surdeus    👁️ 2      

surdeus

I have a bunch of different types that have a method that can asynchronously wait for an event:

impl A {
    async fn event(&mut self) -> Event { /*...*/ }
}

All the objects stem from a single Connection which receives all the events for all the objects and dispatches them accordingly:

impl Connection {
    /// Runs `fut` to completion. Will dispatch events to objects
    async fn run(&mut self, fut: impl Future) { /*...*/ }
}

The order in which events are handled is important, so when the Connection receives an event, it waits for its respective object to be listening before dispatching it and moving on to the next event. The issue here is that if an object exists but the user did not call object.event().await on it, the connection will wait forever for an event listener that will never come. An alternative solution would be to drop events that have no listeners, but this would lead to subtle issues and would be particularly problematic with multithreading.

I realized that what this can be boiled down to is that object cannot exist by itself across an await point, unless object.event().await has been called. Is there any kind of trick I can use here to get the compiler to warn or error if this was done? The only thing close to this I know of is the lint emitted when a RefCell is held across an await point. Alternatively, is there an entirely different approach I could use using async (not callbacks)?

1 post - 1 participant

Read full topic

🏷️ Rust_feed