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
// Copyright 2025 Nathan Sizemore <nathanrsizemore@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
/// Receive from an `mpsc` receiver with a timeout, mapping each outcome to a value.
///
/// This macro wraps `Receiver::recv_timeout`, returning:
/// - the received value on `Ok(v)`;
/// - the result of `$on_timeout` when the wait times out;
/// - the result of `$on_disconnected` when the channel is disconnected.
///
/// ### Type constraints
/// The expressions/blocks passed as `$on_timeout` and `$on_disconnected` **must
/// evaluate to the same type** as the success value (the type carried by the channel),
/// so the whole macro expands to a single, consistent value.
///
/// ### Parameters
/// - `$rx`: an expression evaluating to `std::sync::mpsc::Receiver<T>`.
/// - `$dur`: an expression evaluating to `std::time::Duration`.
/// - `$on_timeout`: a **block** evaluated when `RecvTimeoutError::Timeout`.
/// - `$on_disconnected`: a **block** evaluated when `RecvTimeoutError::Disconnected`.
///
/// Blocks are used so you can place statements, logging, or even `return`/`break`
/// inside the handlers if desired.
///
/// ### Example
/// ```rust
/// # use std::sync::mpsc;
/// # use std::time::Duration;
/// # use your_crate::recv_timeout;
/// # let (_tx, rx) = mpsc::channel::<u32>();
/// let v: u32 = recv_timeout!(
/// rx,
/// Duration::from_millis(10),
/// { 0 }, // on timeout, default to 0
/// { 42 } // on disconnect, sentinel value
/// );
/// assert!(v == 0 || v == 42); // depending on race
/// ```
/// Convenience wrapper over [`recv_timeout!`] that takes the timeout in milliseconds.
///
/// This converts `$ms` to a `Duration` via `Duration::from_millis` and then
/// defers to `recv_timeout!`.
///
/// ### Parameters
/// - `$rx`: an expression evaluating to `std::sync::mpsc::Receiver<T>`.
/// - `$ms`: an integer-like expression convertible to `u64`.
/// - `$on_timeout`: a **block** evaluated when timeout occurs.
/// - `$on_disconnected`: a **block** evaluated when the channel is disconnected.
///
/// ### Example
/// ```rust
/// # use std::sync::mpsc;
/// # use your_crate::recv_timeout_ms;
/// # let (_tx, rx) = mpsc::channel::<&'static str>();
/// let s: &str = recv_timeout_ms!(
/// rx,
/// 5, // milliseconds
/// { "timeout" },
/// { "disconnected" }
/// );
/// ```
// Copyright 2025 Nathan Sizemore <nathanrsizemore@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
/// Receive from an `mpsc` receiver with a timeout; on timeout, `continue` the loop.
///
/// This macro wraps `Receiver::recv_timeout`, returning:
/// - the received value on `Ok(v)`;
/// - `continue` (i.e., skip to next loop iteration) when the wait times out;
/// - the result of `$on_disconnected` when the channel is disconnected.
///
/// ⚠️ **Must be used inside a loop** because it expands `continue` on timeout.
///
/// ### Type constraints
/// `$on_disconnected` **must evaluate to the same type** as the success value
/// (the type carried by the channel), so the whole macro expands to a consistent value
/// in the non-timeout branches.
///
/// ### Parameters
/// - `$rx`: an expression evaluating to `std::sync::mpsc::Receiver<T>`.
/// - `$dur`: an expression evaluating to `std::time::Duration`.
/// - `$on_disconnected`: a **block** evaluated when `RecvTimeoutError::Disconnected`.
///
/// ### Example
/// ```rust
/// # use std::sync::mpsc;
/// # use std::time::Duration;
/// # let (_tx, rx) = mpsc::channel::<u32>();
/// loop {
/// let v: u32 = recv_timeout_or_continue!(
/// rx,
/// Duration::from_millis(10),
/// { // on disconnected
/// 42
/// }
/// );
/// // use v ...
/// }
/// ```
/// Convenience wrapper over [`recv_timeout_or_continue!`] that takes milliseconds.
///
/// Converts `$ms` to a `Duration` via `Duration::from_millis` and then defers to
/// [`recv_timeout_or_continue!`]. On timeout, this **continues** the surrounding loop.
///
/// ⚠️ **Must be used inside a loop** because it expands `continue` on timeout.
///
/// ### Type constraints
/// `$on_disconnected` **must evaluate to the same type** as the channel’s success value.
///
/// ### Parameters
/// - `$rx`: an expression evaluating to `std::sync::mpsc::Receiver<T>`.
/// - `$ms`: an integer-like expression convertible to `u64`.
/// - `$on_disconnected`: a **block** evaluated when the channel is disconnected.
///
/// ### Example
/// ```rust
/// # use std::sync::mpsc;
/// # let (_tx, rx) = mpsc::channel::<&'static str>();
/// loop {
/// let s: &str = recv_timeout_ms_or_continue!(
/// rx,
/// 250,
/// { // on disconnected
/// "disconnected"
/// }
/// );
/// // use s ...
/// }
/// ```