starnix_lifecycle/
drop_notifier.rs1use fidl::HandleBased;
6use fuchsia_async as fasync;
7
8#[derive(Debug)]
14pub struct DropNotifier {
15 _local_event: zx::EventPair,
16 notified_event: zx::EventPair,
17}
18
19pub type DropWaiter = fasync::RWHandle<zx::EventPair>;
21
22impl DropNotifier {
23 pub fn waiter(&self) -> DropWaiter {
25 fasync::RWHandle::new(self.event())
26 }
27
28 pub fn event(&self) -> zx::EventPair {
30 self.notified_event.duplicate_handle(zx::Rights::SAME_RIGHTS).expect("duplicate event")
31 }
32}
33
34impl Default for DropNotifier {
35 fn default() -> Self {
36 let (_local_event, notified_event) = zx::EventPair::create();
37 Self { _local_event, notified_event }
38 }
39}
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44 use std::pin::pin;
45
46 #[fuchsia::test]
47 async fn check_notifier() {
48 let notifier = DropNotifier::default();
49 let waiter = notifier.waiter();
50 let mut on_closed = pin!(waiter.on_closed());
51 assert!(futures::poll!(&mut on_closed).is_pending());
52 assert!(!waiter.is_closed());
53 std::mem::drop(notifier);
54 let on_closed2 = waiter.on_closed();
55 assert!(waiter.is_closed());
56 assert_eq!(on_closed.await.expect("await"), zx::Signals::EVENTPAIR_PEER_CLOSED);
57 assert_eq!(on_closed2.await.expect("await"), zx::Signals::EVENTPAIR_PEER_CLOSED);
58 }
59}