networkframework 0.13.1

Safe Rust bindings for Apple's Network.framework — modern, post-CFNetwork TCP / UDP / TLS / Bonjour networking on macOS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
//! Async stream wrappers for Network.framework callback-based APIs.
//!
//! Each stream type subscribes to one or more handler-based Apple APIs and
//! delivers events through a [`BoundedAsyncStream`] that any async runtime
//! can `.await` on.
//!
//! # Feature gate
//!
//! All types in this module require the **`async`** cargo feature.
//!
//! # Back-pressure policy
//!
//! Streams are **lossy by default**: when the internal buffer is full the
//! oldest event is dropped to make room for the newest. Choose a capacity
//! large enough that your consumer can drain it between bursts.
//!
//! # Drop semantics
//!
//! Dropping any of the stream types automatically unsubscribes from the
//! underlying Network.framework handler and frees the associated sender.

#![cfg(feature = "async")]

use core::ffi::{c_int, c_void};
use core::fmt;
use core::marker::PhantomData;
use core::ptr;
use doom_fish_utils::panic_safe::catch_user_panic;
use doom_fish_utils::stream::{AsyncStreamSender, BoundedAsyncStream, NextItem};

use crate::browser::{BrowseResult, BrowseResultChange, BrowserState};
use crate::error::FrameworkError;
use crate::ffi;

struct SubscriptionHandle {
    cleanup: Option<Box<dyn FnOnce() + Send>>,
}

impl Drop for SubscriptionHandle {
    fn drop(&mut self) {
        if let Some(cleanup) = self.cleanup.take() {
            cleanup();
        }
    }
}

// SAFETY: `SubscriptionHandle` only stores a `Send` cleanup closure and moves
// it to the dropping thread; no raw pointers are shared here.
unsafe impl Send for SubscriptionHandle {}
// SAFETY: shared references never execute the cleanup closure. It is only taken
// and run during `Drop`, which requires unique access.
unsafe impl Sync for SubscriptionHandle {}

impl fmt::Debug for SubscriptionHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SubscriptionHandle")
            .field("has_cleanup", &self.cleanup.is_some())
            .finish_non_exhaustive()
    }
}

/// State of an `nw_connection_t`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConnectionState {
    Invalid,
    Waiting,
    Preparing,
    Ready,
    Failed,
    Cancelled,
    Unknown(i32),
}

impl ConnectionState {
    const fn from_raw(raw: i32) -> Self {
        match raw {
            0 => Self::Invalid,
            1 => Self::Waiting,
            2 => Self::Preparing,
            3 => Self::Ready,
            4 => Self::Failed,
            5 => Self::Cancelled,
            other => Self::Unknown(other),
        }
    }
}

/// Event fired when `nw_connection_set_state_changed_handler` fires.
#[derive(Clone)]
pub struct ConnectionStateEvent {
    pub state: ConnectionState,
    pub error: Option<FrameworkError>,
}

impl fmt::Debug for ConnectionStateEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ConnectionStateEvent")
            .field("state", &self.state)
            .field(
                "error",
                &self
                    .error
                    .as_ref()
                    .map(|error| (error.domain(), error.code())),
            )
            .finish()
    }
}

/// Async stream of [`ConnectionStateEvent`] for a [`crate::client::TcpClient`].
#[derive(Debug)]
pub struct ConnectionStateStream<'a> {
    inner: BoundedAsyncStream<ConnectionStateEvent>,
    _handle: SubscriptionHandle,
    _owner: PhantomData<&'a crate::client::TcpClient>,
}

unsafe extern "C" fn connection_state_cb(state: c_int, error: *mut c_void, ctx: *mut c_void) {
    if ctx.is_null() {
        return;
    }

    catch_user_panic("connection_state_cb", || {
        // SAFETY: `ctx` was created by `Box::into_raw` in `subscribe` and
        // remains valid until cleanup clears the handler, drains the queue,
        // and reclaims the sender box.
        let sender = unsafe { &*ctx.cast::<AsyncStreamSender<ConnectionStateEvent>>() };
        let error = if error.is_null() {
            None
        } else {
            // SAFETY: the shim hands this callback a retained `nw_error_t`
            // ownership token, which `FrameworkError::from_raw` takes over.
            Some(unsafe { FrameworkError::from_raw(error) })
        };
        sender.push(ConnectionStateEvent {
            state: ConnectionState::from_raw(state),
            error,
        });
    });
}

impl<'a> ConnectionStateStream<'a> {
    /// Subscribe to connection state changes.
    #[must_use]
    pub fn subscribe(client: &'a crate::client::TcpClient, capacity: usize) -> Self {
        let (stream, sender) = BoundedAsyncStream::new(capacity);
        let sender_ptr = Box::into_raw(Box::new(sender));
        let obj_ptr = client.as_ptr();
        let sender_addr = sender_ptr as usize;
        let obj_addr = obj_ptr as usize;
        // SAFETY: `obj_ptr` is a live borrowed connection handle and
        // `sender_ptr` stays valid until the cleanup closure clears the handler,
        // drains the queue, and frees it.
        unsafe {
            ffi::nw_shim_connection_set_state_changed_handler(
                obj_ptr,
                Some(connection_state_cb),
                sender_ptr.cast(),
            );
        }
        let cleanup: Box<dyn FnOnce() + Send> = Box::new(move || {
            let obj_ptr = obj_addr as *mut c_void;
            let sender_ptr = sender_addr as *mut AsyncStreamSender<ConnectionStateEvent>;
            // SAFETY: `sender_ptr` was produced by `Box::into_raw` above. The
            // handler is cleared and the queue is drained before we reconstruct
            // the box, so no callback can ever observe `sender_ptr` again.
            unsafe {
                ffi::nw_shim_connection_set_state_changed_handler(obj_ptr, None, ptr::null_mut());
                ffi::nw_shim_connection_drain_queue(obj_ptr);
                drop(Box::from_raw(sender_ptr));
            }
        });
        Self {
            inner: stream,
            _handle: SubscriptionHandle {
                cleanup: Some(cleanup),
            },
            _owner: PhantomData,
        }
    }

    /// Asynchronously wait for the next event.
    #[must_use]
    pub const fn next(&self) -> NextItem<'_, ConnectionStateEvent> {
        self.inner.next()
    }

    /// Try to get an event without blocking.
    #[must_use]
    pub fn try_next(&self) -> Option<ConnectionStateEvent> {
        self.inner.try_next()
    }

    /// Number of events currently buffered.
    #[must_use]
    pub fn buffered_count(&self) -> usize {
        self.inner.buffered_count()
    }
}

/// Async stream of viability changes (`true` = viable) for a [`crate::client::TcpClient`].
#[derive(Debug)]
pub struct ConnectionViabilityStream<'a> {
    inner: BoundedAsyncStream<bool>,
    _handle: SubscriptionHandle,
    _owner: PhantomData<&'a crate::client::TcpClient>,
}

unsafe extern "C" fn connection_viability_cb(value: c_int, ctx: *mut c_void) {
    if ctx.is_null() {
        return;
    }

    catch_user_panic("connection_viability_cb", || {
        // SAFETY: `ctx` was created by `Box::into_raw` in `subscribe` and
        // remains valid until cleanup clears the handler, drains the queue,
        // and reclaims the sender box.
        let sender = unsafe { &*ctx.cast::<AsyncStreamSender<bool>>() };
        sender.push(value != 0);
    });
}

impl<'a> ConnectionViabilityStream<'a> {
    /// Subscribe to viability changes.
    #[must_use]
    pub fn subscribe(client: &'a crate::client::TcpClient, capacity: usize) -> Self {
        let (stream, sender) = BoundedAsyncStream::new(capacity);
        let sender_ptr = Box::into_raw(Box::new(sender));
        let obj_ptr = client.as_ptr();
        let sender_addr = sender_ptr as usize;
        let obj_addr = obj_ptr as usize;
        // SAFETY: `obj_ptr` is a live borrowed connection handle and
        // `sender_ptr` stays valid until the cleanup closure clears the handler,
        // drains the queue, and frees it.
        unsafe {
            ffi::nw_shim_connection_set_viability_changed_handler(
                obj_ptr,
                Some(connection_viability_cb),
                sender_ptr.cast(),
            );
        }
        let cleanup: Box<dyn FnOnce() + Send> = Box::new(move || {
            let obj_ptr = obj_addr as *mut c_void;
            let sender_ptr = sender_addr as *mut AsyncStreamSender<bool>;
            // SAFETY: `sender_ptr` was produced by `Box::into_raw` above. The
            // handler is cleared and the queue is drained before we reconstruct
            // the box, so no callback can ever observe `sender_ptr` again.
            unsafe {
                ffi::nw_shim_connection_set_viability_changed_handler(
                    obj_ptr,
                    None,
                    ptr::null_mut(),
                );
                ffi::nw_shim_connection_drain_queue(obj_ptr);
                drop(Box::from_raw(sender_ptr));
            }
        });
        Self {
            inner: stream,
            _handle: SubscriptionHandle {
                cleanup: Some(cleanup),
            },
            _owner: PhantomData,
        }
    }

    /// Asynchronously wait for the next event.
    #[must_use]
    pub const fn next(&self) -> NextItem<'_, bool> {
        self.inner.next()
    }

    /// Try to get an event without blocking.
    #[must_use]
    pub fn try_next(&self) -> Option<bool> {
        self.inner.try_next()
    }

    /// Number of events currently buffered.
    #[must_use]
    pub fn buffered_count(&self) -> usize {
        self.inner.buffered_count()
    }
}

/// Async stream of better-path-available events for a [`crate::client::TcpClient`].
#[derive(Debug)]
pub struct ConnectionBetterPathStream<'a> {
    inner: BoundedAsyncStream<bool>,
    _handle: SubscriptionHandle,
    _owner: PhantomData<&'a crate::client::TcpClient>,
}

unsafe extern "C" fn connection_better_path_cb(value: c_int, ctx: *mut c_void) {
    if ctx.is_null() {
        return;
    }

    catch_user_panic("connection_better_path_cb", || {
        // SAFETY: `ctx` was created by `Box::into_raw` in `subscribe` and
        // remains valid until cleanup clears the handler, drains the queue,
        // and reclaims the sender box.
        let sender = unsafe { &*ctx.cast::<AsyncStreamSender<bool>>() };
        sender.push(value != 0);
    });
}

impl<'a> ConnectionBetterPathStream<'a> {
    /// Subscribe to better-path notifications.
    #[must_use]
    pub fn subscribe(client: &'a crate::client::TcpClient, capacity: usize) -> Self {
        let (stream, sender) = BoundedAsyncStream::new(capacity);
        let sender_ptr = Box::into_raw(Box::new(sender));
        let obj_ptr = client.as_ptr();
        let sender_addr = sender_ptr as usize;
        let obj_addr = obj_ptr as usize;
        // SAFETY: `obj_ptr` is a live borrowed connection handle and
        // `sender_ptr` stays valid until the cleanup closure clears the handler,
        // drains the queue, and frees it.
        unsafe {
            ffi::nw_shim_connection_set_better_path_available_handler(
                obj_ptr,
                Some(connection_better_path_cb),
                sender_ptr.cast(),
            );
        }
        let cleanup: Box<dyn FnOnce() + Send> = Box::new(move || {
            let obj_ptr = obj_addr as *mut c_void;
            let sender_ptr = sender_addr as *mut AsyncStreamSender<bool>;
            // SAFETY: `sender_ptr` was produced by `Box::into_raw` above. The
            // handler is cleared and the queue is drained before we reconstruct
            // the box, so no callback can ever observe `sender_ptr` again.
            unsafe {
                ffi::nw_shim_connection_set_better_path_available_handler(
                    obj_ptr,
                    None,
                    ptr::null_mut(),
                );
                ffi::nw_shim_connection_drain_queue(obj_ptr);
                drop(Box::from_raw(sender_ptr));
            }
        });
        Self {
            inner: stream,
            _handle: SubscriptionHandle {
                cleanup: Some(cleanup),
            },
            _owner: PhantomData,
        }
    }

    /// Asynchronously wait for the next event.
    #[must_use]
    pub const fn next(&self) -> NextItem<'_, bool> {
        self.inner.next()
    }

    /// Try to get an event without blocking.
    #[must_use]
    pub fn try_next(&self) -> Option<bool> {
        self.inner.try_next()
    }

    /// Number of events currently buffered.
    #[must_use]
    pub fn buffered_count(&self) -> usize {
        self.inner.buffered_count()
    }
}

/// Async stream of path-changed events for a [`crate::client::TcpClient`].
#[derive(Debug)]
pub struct ConnectionPathChangedStream<'a> {
    inner: BoundedAsyncStream<crate::path::Path>,
    _handle: SubscriptionHandle,
    _owner: PhantomData<&'a crate::client::TcpClient>,
}

unsafe extern "C" fn connection_path_changed_cb(path: *mut c_void, ctx: *mut c_void) {
    if path.is_null() || ctx.is_null() {
        return;
    }

    catch_user_panic("connection_path_changed_cb", || {
        // SAFETY: `ctx` was created by `Box::into_raw` in `subscribe` and
        // remains valid until cleanup clears the handler, drains the queue,
        // and reclaims the sender box.
        let sender = unsafe { &*ctx.cast::<AsyncStreamSender<crate::path::Path>>() };
        // SAFETY: the shim passes this callback a retained `nw_path_t`
        // ownership token, which `Path::from_raw` takes over.
        let path = unsafe { crate::path::Path::from_raw(path) };
        sender.push(path);
    });
}

impl<'a> ConnectionPathChangedStream<'a> {
    /// Subscribe to path changes.
    #[must_use]
    pub fn subscribe(client: &'a crate::client::TcpClient, capacity: usize) -> Self {
        let (stream, sender) = BoundedAsyncStream::new(capacity);
        let sender_ptr = Box::into_raw(Box::new(sender));
        let obj_ptr = client.as_ptr();
        let sender_addr = sender_ptr as usize;
        let obj_addr = obj_ptr as usize;
        // SAFETY: `obj_ptr` is a live borrowed connection handle and
        // `sender_ptr` stays valid until the cleanup closure clears the handler,
        // drains the queue, and frees it.
        unsafe {
            ffi::nw_shim_connection_set_path_changed_handler(
                obj_ptr,
                Some(connection_path_changed_cb),
                sender_ptr.cast(),
            );
        }
        let cleanup: Box<dyn FnOnce() + Send> = Box::new(move || {
            let obj_ptr = obj_addr as *mut c_void;
            let sender_ptr = sender_addr as *mut AsyncStreamSender<crate::path::Path>;
            // SAFETY: `sender_ptr` was produced by `Box::into_raw` above. The
            // handler is cleared and the queue is drained before we reconstruct
            // the box, so no callback can ever observe `sender_ptr` again.
            unsafe {
                ffi::nw_shim_connection_set_path_changed_handler(obj_ptr, None, ptr::null_mut());
                ffi::nw_shim_connection_drain_queue(obj_ptr);
                drop(Box::from_raw(sender_ptr));
            }
        });
        Self {
            inner: stream,
            _handle: SubscriptionHandle {
                cleanup: Some(cleanup),
            },
            _owner: PhantomData,
        }
    }

    /// Asynchronously wait for the next event.
    #[must_use]
    pub const fn next(&self) -> NextItem<'_, crate::path::Path> {
        self.inner.next()
    }

    /// Try to get an event without blocking.
    #[must_use]
    pub fn try_next(&self) -> Option<crate::path::Path> {
        self.inner.try_next()
    }

    /// Number of events currently buffered.
    #[must_use]
    pub fn buffered_count(&self) -> usize {
        self.inner.buffered_count()
    }
}

/// State of an `nw_listener_t`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ListenerState {
    Invalid,
    Waiting,
    Ready,
    Failed,
    Cancelled,
    Unknown(i32),
}

impl ListenerState {
    const fn from_raw(raw: i32) -> Self {
        match raw {
            0 => Self::Invalid,
            1 => Self::Waiting,
            2 => Self::Ready,
            3 => Self::Failed,
            4 => Self::Cancelled,
            other => Self::Unknown(other),
        }
    }
}

/// Event from a [`ListenerEventStream`].
pub enum ListenerEvent {
    /// Listener state changed.
    State {
        state: ListenerState,
        error: Option<FrameworkError>,
    },
    /// A new inbound connection was accepted.
    NewConnection(crate::client::TcpClient),
}

impl fmt::Debug for ListenerEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::State { state, error } => f
                .debug_struct("State")
                .field("state", state)
                .field(
                    "error",
                    &error.as_ref().map(|error| (error.domain(), error.code())),
                )
                .finish(),
            Self::NewConnection(_) => f.write_str("NewConnection(TcpClient { .. })"),
        }
    }
}

struct ListenerNewConnectionContext {
    sender: AsyncStreamSender<ListenerEvent>,
    keepalives: crate::parameters::KeepAlives,
}

/// Async stream of [`ListenerEvent`] for a [`crate::listener::TcpListener`].
#[derive(Debug)]
pub struct ListenerEventStream<'a> {
    inner: BoundedAsyncStream<ListenerEvent>,
    _handle: SubscriptionHandle,
    _owner: PhantomData<&'a crate::listener::TcpListener>,
}

unsafe extern "C" fn listener_state_cb(state: c_int, error: *mut c_void, ctx: *mut c_void) {
    if ctx.is_null() {
        return;
    }

    catch_user_panic("listener_state_cb", || {
        // SAFETY: `ctx` was created by `Box::into_raw` in `subscribe` and
        // remains valid until cleanup clears the handler, drains the queue,
        // and reclaims the sender box.
        let sender = unsafe { &*ctx.cast::<AsyncStreamSender<ListenerEvent>>() };
        let error = if error.is_null() {
            None
        } else {
            // SAFETY: the shim hands this callback a retained `nw_error_t`
            // ownership token, which `FrameworkError::from_raw` takes over.
            Some(unsafe { FrameworkError::from_raw(error) })
        };
        sender.push(ListenerEvent::State {
            state: ListenerState::from_raw(state),
            error,
        });
    });
}

unsafe extern "C" fn listener_new_connection_cb(connection_handle: *mut c_void, ctx: *mut c_void) {
    if connection_handle.is_null() || ctx.is_null() {
        return;
    }

    catch_user_panic("listener_new_connection_cb", || {
        // SAFETY: `ctx` was created by `Box::into_raw` in `subscribe` and
        // remains valid until cleanup clears the handler, drains the queue,
        // and reclaims the context box.
        let ctx = unsafe { &*ctx.cast::<ListenerNewConnectionContext>() };
        // SAFETY: `connection_handle` is a live accepted-connection handle
        // produced by the listener shim, and ownership transfers to the new
        // `TcpClient` wrapper.
        let client = unsafe {
            crate::client::TcpClient::from_raw_with_keepalives(
                connection_handle,
                ctx.keepalives.clone(),
            )
        };
        ctx.sender.push(ListenerEvent::NewConnection(client));
    });
}

impl<'a> ListenerEventStream<'a> {
    /// Subscribe to listener state changes and inbound connections.
    #[must_use]
    pub fn subscribe(listener: &'a crate::listener::TcpListener, capacity: usize) -> Self {
        let (stream, sender) = BoundedAsyncStream::new(capacity);
        let state_ptr = Box::into_raw(Box::new(sender.clone()));
        let conn_ptr = Box::into_raw(Box::new(ListenerNewConnectionContext {
            sender,
            keepalives: listener.keepalives(),
        }));
        let obj_ptr = listener.as_ptr();
        let state_addr = state_ptr as usize;
        let conn_addr = conn_ptr as usize;
        let obj_addr = obj_ptr as usize;
        // SAFETY: `obj_ptr` is a live borrowed listener handle, and both box
        // pointers stay valid until the cleanup closure clears the handlers,
        // drains the queue, and frees them.
        unsafe {
            ffi::nw_shim_listener_set_state_changed_handler(
                obj_ptr,
                Some(listener_state_cb),
                state_ptr.cast(),
            );
            ffi::nw_shim_listener_set_new_connection_handler(
                obj_ptr,
                Some(listener_new_connection_cb),
                conn_ptr.cast(),
            );
        }
        let cleanup: Box<dyn FnOnce() + Send> = Box::new(move || {
            let obj_ptr = obj_addr as *mut c_void;
            let state_ptr = state_addr as *mut AsyncStreamSender<ListenerEvent>;
            let conn_ptr = conn_addr as *mut ListenerNewConnectionContext;
            // SAFETY: `state_ptr` and `conn_ptr` were produced by `Box::into_raw`
            // above. Both handlers are cleared and the queue is drained before
            // we reconstruct the boxes, so no callback can ever observe these
            // pointers again.
            unsafe {
                ffi::nw_shim_listener_set_state_changed_handler(obj_ptr, None, ptr::null_mut());
                ffi::nw_shim_listener_set_new_connection_handler(obj_ptr, None, ptr::null_mut());
                ffi::nw_shim_listener_drain_queue(obj_ptr);
                drop(Box::from_raw(state_ptr));
                drop(Box::from_raw(conn_ptr));
            }
        });
        Self {
            inner: stream,
            _handle: SubscriptionHandle {
                cleanup: Some(cleanup),
            },
            _owner: PhantomData,
        }
    }

    /// Asynchronously wait for the next event.
    #[must_use]
    pub const fn next(&self) -> NextItem<'_, ListenerEvent> {
        self.inner.next()
    }

    /// Try to get an event without blocking.
    #[must_use]
    pub fn try_next(&self) -> Option<ListenerEvent> {
        self.inner.try_next()
    }

    /// Number of events currently buffered.
    #[must_use]
    pub fn buffered_count(&self) -> usize {
        self.inner.buffered_count()
    }
}

/// Async stream of path updates from a [`crate::path_monitor::PathMonitor`].
#[derive(Debug)]
pub struct PathUpdateStream<'a> {
    inner: BoundedAsyncStream<crate::path::Path>,
    _handle: SubscriptionHandle,
    _owner: PhantomData<&'a crate::path_monitor::PathMonitor>,
}

unsafe extern "C" fn path_update_cb(path: *mut c_void, ctx: *mut c_void) {
    if path.is_null() || ctx.is_null() {
        return;
    }

    catch_user_panic("path_update_cb", || {
        // SAFETY: `ctx` was created by `Box::into_raw` in `subscribe` and
        // remains valid until cleanup clears the handler, drains the queue,
        // and reclaims the sender box.
        let sender = unsafe { &*ctx.cast::<AsyncStreamSender<crate::path::Path>>() };
        // SAFETY: the shim passes this callback a retained `nw_path_t`
        // ownership token, which `Path::from_raw` takes over.
        let path = unsafe { crate::path::Path::from_raw(path) };
        sender.push(path);
    });
}

impl<'a> PathUpdateStream<'a> {
    /// Subscribe to path monitor updates.
    #[must_use]
    pub fn subscribe(monitor: &'a crate::path_monitor::PathMonitor, capacity: usize) -> Self {
        let (stream, sender) = BoundedAsyncStream::new(capacity);
        let sender_ptr = Box::into_raw(Box::new(sender));
        let obj_ptr = monitor.as_ptr();
        let sender_addr = sender_ptr as usize;
        let obj_addr = obj_ptr as usize;
        // SAFETY: `obj_ptr` is a live borrowed path-monitor handle and
        // `sender_ptr` stays valid until the cleanup closure clears the handler,
        // drains the queue, and frees it.
        unsafe {
            ffi::nw_shim_path_monitor_set_update_handler(
                obj_ptr,
                Some(path_update_cb),
                sender_ptr.cast(),
            );
        }
        let cleanup: Box<dyn FnOnce() + Send> = Box::new(move || {
            let obj_ptr = obj_addr as *mut c_void;
            let sender_ptr = sender_addr as *mut AsyncStreamSender<crate::path::Path>;
            // SAFETY: `sender_ptr` was produced by `Box::into_raw` above. The
            // handler is cleared and the queue is drained before we reconstruct
            // the box, so no callback can ever observe `sender_ptr` again.
            unsafe {
                ffi::nw_shim_path_monitor_set_update_handler(obj_ptr, None, ptr::null_mut());
                ffi::nw_shim_path_monitor_drain_queue(obj_ptr);
                drop(Box::from_raw(sender_ptr));
            }
        });
        Self {
            inner: stream,
            _handle: SubscriptionHandle {
                cleanup: Some(cleanup),
            },
            _owner: PhantomData,
        }
    }

    /// Asynchronously wait for the next event.
    #[must_use]
    pub const fn next(&self) -> NextItem<'_, crate::path::Path> {
        self.inner.next()
    }

    /// Try to get an event without blocking.
    #[must_use]
    pub fn try_next(&self) -> Option<crate::path::Path> {
        self.inner.try_next()
    }

    /// Number of events currently buffered.
    #[must_use]
    pub fn buffered_count(&self) -> usize {
        self.inner.buffered_count()
    }
}

/// Event from a [`BrowserEventStream`].
#[derive(Clone)]
pub enum BrowserAsyncEvent {
    /// Browser state changed.
    State {
        state: BrowserState,
        error: Option<FrameworkError>,
    },
    /// Browse results changed.
    Results {
        old_result: Option<BrowseResult>,
        new_result: Option<BrowseResult>,
        changes: BrowseResultChange,
        batch_complete: bool,
    },
}

impl fmt::Debug for BrowserAsyncEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::State { state, error } => f
                .debug_struct("State")
                .field("state", state)
                .field(
                    "error",
                    &error.as_ref().map(|error| (error.domain(), error.code())),
                )
                .finish(),
            Self::Results {
                old_result,
                new_result,
                changes,
                batch_complete,
            } => f
                .debug_struct("Results")
                .field("old_result_present", &old_result.is_some())
                .field("new_result_present", &new_result.is_some())
                .field("changes", changes)
                .field("batch_complete", batch_complete)
                .finish(),
        }
    }
}

/// Async stream of [`BrowserAsyncEvent`] from a [`crate::browser::Browser`].
#[derive(Debug)]
pub struct BrowserEventStream<'a> {
    inner: BoundedAsyncStream<BrowserAsyncEvent>,
    _handle: SubscriptionHandle,
    _owner: PhantomData<&'a crate::browser::Browser>,
}

unsafe extern "C" fn browser_state_cb(state: c_int, error: *mut c_void, ctx: *mut c_void) {
    if ctx.is_null() {
        return;
    }

    catch_user_panic("browser_state_cb", || {
        // SAFETY: `ctx` was created by `Box::into_raw` in `subscribe` and
        // remains valid until cleanup clears the handler, drains the queue,
        // and reclaims the sender box.
        let sender = unsafe { &*ctx.cast::<AsyncStreamSender<BrowserAsyncEvent>>() };
        let error = if error.is_null() {
            None
        } else {
            // SAFETY: the shim hands this callback a retained `nw_error_t`
            // ownership token, which `FrameworkError::from_raw` takes over.
            Some(unsafe { FrameworkError::from_raw(error) })
        };
        sender.push(BrowserAsyncEvent::State {
            state: BrowserState::from_raw(state),
            error,
        });
    });
}

unsafe extern "C" fn browser_results_cb(
    old_result: *mut c_void,
    new_result: *mut c_void,
    changes: u64,
    batch_complete: c_int,
    ctx: *mut c_void,
) {
    if ctx.is_null() {
        return;
    }

    catch_user_panic("browser_results_cb", || {
        // SAFETY: `ctx` was created by `Box::into_raw` in `subscribe` and
        // remains valid until cleanup clears the handler, drains the queue,
        // and reclaims the sender box.
        let sender = unsafe { &*ctx.cast::<AsyncStreamSender<BrowserAsyncEvent>>() };
        let old_result = if old_result.is_null() {
            None
        } else {
            // SAFETY: the shim passes retained browse-result handles to the
            // callback, and ownership transfers to the Rust wrappers.
            Some(unsafe { BrowseResult::from_raw(old_result) })
        };
        let new_result = if new_result.is_null() {
            None
        } else {
            // SAFETY: the shim passes retained browse-result handles to the
            // callback, and ownership transfers to the Rust wrappers.
            Some(unsafe { BrowseResult::from_raw(new_result) })
        };
        sender.push(BrowserAsyncEvent::Results {
            old_result,
            new_result,
            changes: BrowseResultChange::from_raw(changes),
            batch_complete: batch_complete != 0,
        });
    });
}

impl<'a> BrowserEventStream<'a> {
    /// Subscribe to browser state and browse-result updates.
    #[must_use]
    pub fn subscribe(browser: &'a crate::browser::Browser, capacity: usize) -> Self {
        let (stream, sender) = BoundedAsyncStream::new(capacity);
        let state_ptr = Box::into_raw(Box::new(sender.clone()));
        let results_ptr = Box::into_raw(Box::new(sender));
        let obj_ptr = browser.as_ptr();
        let state_addr = state_ptr as usize;
        let results_addr = results_ptr as usize;
        let obj_addr = obj_ptr as usize;
        // SAFETY: `obj_ptr` is a live borrowed browser handle, and both box
        // pointers stay valid until the cleanup closure clears the handlers,
        // drains the queue, and frees them.
        unsafe {
            ffi::nw_shim_browser_set_state_changed_handler(
                obj_ptr,
                Some(browser_state_cb),
                state_ptr.cast(),
            );
            ffi::nw_shim_browser_set_browse_results_changed_handler(
                obj_ptr,
                Some(browser_results_cb),
                results_ptr.cast(),
            );
        }
        let cleanup: Box<dyn FnOnce() + Send> = Box::new(move || {
            let obj_ptr = obj_addr as *mut c_void;
            let state_ptr = state_addr as *mut AsyncStreamSender<BrowserAsyncEvent>;
            let results_ptr = results_addr as *mut AsyncStreamSender<BrowserAsyncEvent>;
            // SAFETY: `state_ptr` and `results_ptr` were produced by
            // `Box::into_raw` above. Both handlers are cleared and the queue is
            // drained before we reconstruct the boxes, so no callback can ever
            // observe these pointers again.
            unsafe {
                ffi::nw_shim_browser_set_state_changed_handler(obj_ptr, None, ptr::null_mut());
                ffi::nw_shim_browser_set_browse_results_changed_handler(
                    obj_ptr,
                    None,
                    ptr::null_mut(),
                );
                ffi::nw_shim_browser_drain_queue(obj_ptr);
                drop(Box::from_raw(state_ptr));
                drop(Box::from_raw(results_ptr));
            }
        });
        Self {
            inner: stream,
            _handle: SubscriptionHandle {
                cleanup: Some(cleanup),
            },
            _owner: PhantomData,
        }
    }

    /// Asynchronously wait for the next event.
    #[must_use]
    pub const fn next(&self) -> NextItem<'_, BrowserAsyncEvent> {
        self.inner.next()
    }

    /// Try to get an event without blocking.
    #[must_use]
    pub fn try_next(&self) -> Option<BrowserAsyncEvent> {
        self.inner.try_next()
    }

    /// Number of events currently buffered.
    #[must_use]
    pub fn buffered_count(&self) -> usize {
        self.inner.buffered_count()
    }
}