Skip to content

Utility for polling a future from an async function #2257

Closed
@Plecra

Description

@Plecra
struct Check<'a, F: Future + ?Sized>(Pin<&'a mut F>);
impl<F: Future + ?Sized> Future for Check<'_, F> {
    type Output = Poll<F::Output>;
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        Poll::Ready(self.0.as_mut().poll(cx))
    }
}
async fn checkup(tasks: Vec<Task>) {
    for mut task in tasks {
        if let Poll::Ready(val) = Check(task.fut.as_mut()).await {
            handle(val);
            cleanup(task);
        }
    }
}

I've seen this pattern come up in a few different projects now, and it seems generic enough for the futures crate. It could well be too niche though.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions