use fidl::HandleBased;
use {fuchsia_async as fasync, fuchsia_zircon as zx};
#[derive(Debug)]
pub struct DropNotifier {
_local_event: zx::EventPair,
notified_event: zx::EventPair,
}
pub type DropWaiter = fasync::RWHandle<zx::EventPair>;
impl DropNotifier {
pub fn waiter(&self) -> DropWaiter {
fasync::RWHandle::new(self.event())
}
pub fn event(&self) -> zx::EventPair {
self.notified_event.duplicate_handle(zx::Rights::SAME_RIGHTS).expect("duplicate event")
}
}
impl Default for DropNotifier {
fn default() -> Self {
let (_local_event, notified_event) = zx::EventPair::create();
Self { _local_event, notified_event }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[fuchsia::test]
async fn check_notifier() {
let notifier = DropNotifier::default();
let waiter = notifier.waiter();
let mut on_closed = waiter.on_closed();
assert!(futures::poll!(&mut on_closed).is_pending());
assert!(!waiter.is_closed());
std::mem::drop(notifier);
let on_closed2 = waiter.on_closed();
assert!(waiter.is_closed());
assert_eq!(on_closed.await.expect("await"), zx::Signals::EVENTPAIR_PEER_CLOSED);
assert_eq!(on_closed2.await.expect("await"), zx::Signals::EVENTPAIR_PEER_CLOSED);
}
}