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
45 #[fuchsia::test]
46 async fn check_notifier() {
47 let notifier = DropNotifier::default();
48 let waiter = notifier.waiter();
49 let mut on_closed = waiter.on_closed();
50 assert!(futures::poll!(&mut on_closed).is_pending());
51 assert!(!waiter.is_closed());
52 std::mem::drop(notifier);
53 let on_closed2 = waiter.on_closed();
54 assert!(waiter.is_closed());
55 assert_eq!(on_closed.await.expect("await"), zx::Signals::EVENTPAIR_PEER_CLOSED);
56 assert_eq!(on_closed2.await.expect("await"), zx::Signals::EVENTPAIR_PEER_CLOSED);
57 }
58}