Skip to main content

externally_driven_async_rt/notify/
notified.rs

1use core::{future::Future, pin::Pin, task::Poll};
2
3use crate::notify::Notification;
4
5/// A future that resolves once [`EventLoop::notify`](crate::EventLoop::notify) has been called.
6///
7/// Notifications don't queue up: any number of `notify()` calls made before the future is polled
8/// resolve it only once.
9///
10/// Only one task can wait for a notification at a time.
11#[must_use = "futures do nothing unless you `.await` or poll them"]
12pub struct Notified<'ctx> {
13    notification: &'ctx Notification,
14    registered: bool,
15}
16
17impl<'ctx> Notified<'ctx> {
18    pub(crate) const fn new(notification: &'ctx Notification) -> Self {
19        Self {
20            notification,
21            registered: false,
22        }
23    }
24}
25
26impl Future for Notified<'_> {
27    type Output = ();
28
29    fn poll(mut self: Pin<&mut Self>, cx: &mut core::task::Context<'_>) -> Poll<()> {
30        let poll = self.notification.poll(cx.waker());
31        self.registered = poll.is_pending();
32        poll
33    }
34}
35
36impl Drop for Notified<'_> {
37    fn drop(&mut self) {
38        if self.registered {
39            self.notification.forget_waker();
40        }
41    }
42}
43
44impl core::fmt::Debug for Notified<'_> {
45    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
46        f.write_str("Notified")
47    }
48}