Skip to content

Feature Request: Future to wait for the next idle event #221

@titaniumtraveler

Description

@titaniumtraveler

There are a bunch of instances where it is useful to wait for the next time
that the event loop is idle and it would be nice to have first party support for
that when running in an async context.

Granted, it is pretty easy to implement that code on the user-side - I think it
should be something like this:

use std::{future::Future, pin::Pin};
use calloop::{sources::Idle, LoopHandle};

pub struct IdleFuture {
    calloop: LoopHandle<'static, _>
    idle: Option<Idle<'static>>,
}

impl Future for IdleFuture {
    type Output = ();
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        match &mut self.idle {
            // On first poll set up the waker
            idle @ None => {
                let waker = cx.waker.clone()
                let idle = self.calloop.insert_idle(move |_|{
                      waker.wake()
                });
                
                Poll::Pending
            }
            // On the second poll return to the user
            idle @ Some(_) => {
                idle = None;
                Poll::Ready(())
            }
        }
    }
}

But having direct support for it would be nicer!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions