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
//! 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());
/// ```
/// Signal connection loss before releasing ownership of an incomplete scope.
///
/// An adapter passes an already-acquired exclusive ownership guard and holds this
/// helper across its entire setup or multi-exchange workflow. Unlike
/// [`RetireOnDrop`], this helper does not borrow or clear the stream slot: it also
/// covers interruption *between* exchanges when no stream guard is held.
/// The adapter must check the connection indicator before subsequent I/O and
/// arrange teardown/recovery (for example through `Lifecycle::watch_disconnect`).
///
/// Incomplete drop publishes `false` synchronously before dropping `ownership`,
/// including cancellation, timeout and panic unwinding. Call [`Self::complete`]
/// only when releasing ownership is safe, including a validated healthy rejection.
/// Completion never changes a disconnected indicator back to connected.
///
/// This helper neither acquires a lock nor makes an arbitrary value exclusive.
/// It adds no retries, remote cancellation, outcome inference or async cleanup.
/// It is usable without the pool feature and does not change supervisor defaults.
///
/// ```
/// use etherbird::DisconnectOnDrop;
/// let ownership = std::sync::Mutex::new(());
/// let connected = tokio::sync::watch::channel(true).0;
/// {
/// let mut scope = DisconnectOnDrop::new(ownership.lock().unwrap(), &connected);
/// // Perform and validate the complete workflow while ownership is held.
/// scope.complete();
/// }
/// assert!(*connected.borrow());
/// {
/// let _interrupted = DisconnectOnDrop::new(ownership.lock().unwrap(), &connected);
/// }
/// assert!(!*connected.borrow());
/// ```