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
//! Adapter-owned cancellation safety for resources with an outstanding reply.
use watch;
/// Retire a resource slot when a scope ends before the adapter confirms completion.
///
/// Hold this guard across the entire exchange, including reply validation. On
/// cancellation, timeout, panic unwinding or an early return, it drops the slot's
/// resource and publishes `false` to the adapter's connection indicator.
/// Call [`Self::complete`] only after a valid exchange, including a valid device
/// rejection that leaves the transport usable. The guard does not infer health
/// from a `Result`, retry operations, acquire locks or await lifecycle cleanup.
/// Dropping a resource is not a guarantee of remote cancellation or secret erasure.
///
/// This is optional adapter plumbing, independent of direct versus pooled dispatch.
///
/// ```
/// use etherbird::RetireOnDrop;
/// let (connected, _) = tokio::sync::watch::channel(true);
/// let mut slot = Some(42);
/// {
/// let mut exchange = RetireOnDrop::new(&mut slot, &connected);
/// assert_eq!(exchange.resource(), Some(&mut 42));
/// exchange.complete(); // The adapter has validated the complete reply.
/// }
/// assert_eq!(slot, Some(42));
/// assert!(*connected.borrow());
/// {
/// let _incomplete = RetireOnDrop::new(&mut slot, &connected);
/// }
/// assert!(slot.is_none());
/// assert!(!*connected.borrow());
/// ```