use core::{future::Future, pin::Pin, task::Poll};
use crate::notify::Notification;
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Notified<'ctx> {
notification: &'ctx Notification,
registered: bool,
}
impl<'ctx> Notified<'ctx> {
pub(crate) const fn new(notification: &'ctx Notification) -> Self {
Self {
notification,
registered: false,
}
}
}
impl Future for Notified<'_> {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut core::task::Context<'_>) -> Poll<()> {
let poll = self.notification.poll(cx.waker());
self.registered = poll.is_pending();
poll
}
}
impl Drop for Notified<'_> {
fn drop(&mut self) {
if self.registered {
self.notification.forget_waker();
}
}
}
impl core::fmt::Debug for Notified<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("Notified")
}
}