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
//! Module for working with channels. Rexport of [`crossbeam_channel`]
//!
//!
//! # Examples
//!
//! > Several of these examples are copies of the [`chan`] and [`crossbeam_channel`] crates.
//!
//! [`chan`]: https://github.com/BurntSushi/chan
//! [`crossbeam_channel`]: ../../crossbeam_channel/index.html
//!
//! ## Example: unbounded (async) channel
//!
//! ```rust
//! #[macro_use] extern crate ergo_sync;
//! use ergo_sync::*;
//!
//! # fn main() {
//! let (tx, rx) = ch::unbounded();
//!
//! // Can send an arbitrarily large number of messages.
//! for i in 0..1000 {
//!     ch!(tx <- i);
//! }
//! # }
//! ```
//!
//! ## Example: bounded (sync) channel
//!
//! ```rust
//! #[macro_use] extern crate ergo_sync;
//! use ergo_sync::*;
//!
//! # fn main() {
//! // Create a channel that can hold at most 5 messages at a time.
//! let (tx, rx) = ch::bounded(5);
//!
//! // Can send only 5 messages.
//! for i in 0..5 {
//!     ch!(tx <- i);
//! }
//!
//! // An attempt to send one more message will fail.
//! assert!(tx.try_send(5).is_err());
//! # }
//! ```
//!
//! ## Example: rendevous channel
//!
//! ```rust
//! #[macro_use] extern crate ergo_sync;
//! use ergo_sync::*;
//!
//! # fn main() {
//! let (send, recv) = ch::bounded(0);
//! spawn(move || ch!(send <- 5));
//! assert_eq!(ch!(<- recv), 5); // blocks until the previous send occurs
//! # }
//! ```
//!
//! ## Example: the sentinel channel idiom
//!
//! When writing concurrent programs with `ergo`, you will often find that you need
//! to somehow "wait" until some operation is done. For example, let's say you want
//! to run a function in a separate thread, but wait until it completes. Here's
//! one way to do it:
//!
//! ```rust
//! #[macro_use] extern crate ergo_sync;
//! use ergo_sync::*;
//!
//! fn do_work(done: ch::Sender<()>) {
//!     // do something
//!
//!     // signal that we're done.
//!     ch!(done <- ());
//! }
//!
//! fn main() {
//!     let (sdone, rdone) = ch::bounded(0);
//!     spawn(move || do_work(sdone));
//!     // block until work is done, and then quit the program.
//!     ch!(<- rdone);
//! }
//! ```
//!
//! In effect, we've created a new channel that sends unit values. When we're
//! done doing work, we send a unit value and `main` waits for it to be delivered.
//!
//! Another way of achieving the same thing is to simply close the channel. Once
//! the channel is closed, any previously blocked receive operations become
//! immediately unblocked. What's even cooler is that channels are closed
//! automatically when all senders are dropped. So the new program looks something
//! like this:
//!
//! ```rust
//! #[macro_use] extern crate ergo_sync;
//! use ergo_sync::*;
//!
//! fn do_work(_done: ch::Sender<()>) {
//!     // do something
//! }
//!
//! fn main() {
//!     let (sdone, rdone) = ch::bounded(0);
//!     spawn(move || do_work(sdone));
//!     // Block until the channel is closed.
//!     //
//!     // Note: this _expects_ the error that
//!     // all senders have been dropped and will
//!     // panic if a value is sent instead.
//!     ch!(! <- rdone);
//! }
//! ```
//!
//! We no longer need to explicitly do anything with the `_done` channel. We give
//! `do_work` ownership of the channel, but as soon as the function stops
//! executing, `_done` is dropped, the channel is closed and `rdone.recv()`
//! unblocks returning an error, which we expect with `ch!(! <- rdone)`.
//!
//! ## Example: non-blocking sends/receives
//!
//! ```
//! #[macro_use] extern crate ergo_sync;
//! use ergo_sync::*;
//!
//! # fn main() {
//! let (send, recv) = ch::bounded(1);
//! let data = "send data".to_string();
//! match ch!(send <-? data) {
//!     Some(data) => {
//!         println!("didn't send data, but got it back: {}", data);
//!         unreachable!(); // in this case we don't expect it
//!     }
//!     None => println!("message sent successfully"),
//! }
//!
//! // attempting to send additional data fails
//! let data = "more data".to_string();
//! assert_eq!(Some(data.clone()), ch!(send <-? data));
//!
//! match ch!(<-? recv) {
//!     Some(data) => println!("received data: {}", data),
//!     None => {
//!         println!("didn't receive any data yet");
//!         unreachable!(); // in this case we don't expect it
//!     }
//! }
//! # }
//! ```
//!
//! ## Example: using `select_loop`
//!
//! ```rust
//! #[macro_use] extern crate ergo_sync;
//! use ergo_sync::*;
//!
//! # fn main() {
//! let (tx1, rx1) = ch::unbounded();
//! let (tx2, rx2) = ch::unbounded();
//!
//! spawn(move || ch!(tx1 <- "foo"));
//! spawn(move || ch!(tx2 <- "bar"));
//!
//! select_loop! {
//!     recv(rx1, msg) => {
//!         println!("Received a message from the first channel: {}", msg);
//!     }
//!     recv(rx2, msg) => {
//!         println!("Received a message from the second channel: {}", msg);
//!     }
//! }
//! # }
//! ```
//!

pub use crossbeam_channel::{bounded, unbounded, IntoIter, Iter, Receiver, RecvError,
                            RecvTimeoutError, Select, SelectRecvError, SelectSendError, SendError,
                            SendTimeoutError, Sender, TryIter, TryRecvError, TrySendError};

/// Use with channels with ergonomic syntax and panic with helpful error messages when
/// sending/receiving on a channel is invalid.
///
///   - `ch!(send <- 42)` for sending a value.
///   - `let v = ch!(<- recv)` for receiving a value.
///   - `ch!(! <- recv)` to wait for channels to close.
///   - `<-?` for async operation support.
///
/// **Blocking syntax:**
///
/// - `ch!(send <- value)`: blocks until a value is sent, panics if all receivers are dropped.
/// - `ch!(<- recv)`: blocks until a value is received, panics if all senders are dropped.
/// - `ch!(! <- recv)`: blocks until all senders are dropped, panics if a value is received. Used
///   for signaling.
///
/// > This syntax works with both `crossbeam-channel` channels (which are exported by this crate) as
/// > well as `std::mspc` channels.
///
/// > Note that these operations can deadlock if a channel is leaked.
///
/// **Non-Blocking syntax:**
///
/// - `ch!(send <-? value)`: returns `None` if the value was sent, `Some(value)` if the value
///   was not sent. Panics if all receivers are dropped.
/// - `ch!(<-? recv)`: returns `None` if no value is received, `Some(value)` if a value is
///   received. Panics if all senders are dropped.
/// - `ch!(! <-? recv)`: returns `true` if there are still senders and `false` if the seners have
///   been dropped. Panics if a value is received. Use with `while ch!(! <-? recv) { /* ... */ }`
///
/// > Non-Blocking syntax does _not_ work with `std::mspc` channels.
///
/// # Examples
///
/// ## Example: Using `ergo::chan` channels
///
/// ```rust
/// #[macro_use] extern crate ergo_sync;
/// use ergo_sync::*;
/// # fn main() {
/// let (send, recv) = ch::bounded(3);
/// ch!(send <- 4);
/// ch!(send <- 7);
/// ch!(send <- 42);
/// assert_eq!(4, ch!(<- recv));
/// assert_eq!(7, ch!(<- recv));
/// let v = ch!(<- recv);
/// assert_eq!(42, v);
///
/// drop(send);
/// // ch!(<- recv); // panics
/// ch!(! <- recv);  // succeeds
/// # }
/// ```
///
/// ## Example: Using `std::mspc` channels
///
/// ```rust
/// #[macro_use] extern crate ergo_sync;
/// use std::sync::mpsc::sync_channel;
///
/// # fn main() {
/// let (send, recv) = sync_channel(3);
/// ch!(send <- 4);
/// ch!(send <- 7);
/// ch!(send <- 42);
/// assert_eq!(4, ch!(<- recv));
/// assert_eq!(7, ch!(<- recv));
/// let v = ch!(<- recv);
/// assert_eq!(42, v);
///
/// drop(send);
/// // ch!(<- recv); // panics
/// ch!(! <- recv);  // succeeds
/// # }
/// ```
///
/// ## Example: using non-blocking syntax
///
/// ```rust
/// #[macro_use] extern crate ergo_sync;
/// use ergo_sync::*;
/// # fn main() {
/// let (send, recv) = ch::bounded(3);
/// assert_eq!(None, ch!(<-? recv)); // no values sent yet
///
/// assert!(ch!(send <-? 4).is_none());
/// assert_eq!(Some(4), ch!(<-? recv));
/// assert_eq!(None, ch!(<-? recv));
///
/// assert!(ch!(send <-? 7).is_none());
/// assert!(ch!(send <-? 42).is_none());
/// assert!(ch!(send <-? 1).is_none());
/// // further attempts return the value
/// assert_eq!(Some(100), ch!(send <-? 100));
///
/// assert_eq!(Some(7), ch!(<-? recv));
///
/// assert_eq!(Some(42), ch!(<-? recv));
/// assert_eq!(Some(1), ch!(<-? recv));
/// assert_eq!(None, ch!(<-? recv));
/// assert!(ch!(! <-? recv)); // senders still exist
///
/// drop(send);
/// // ch!(<-? recv); // panics
/// ch!(! <-? recv);  // succeeds
/// # }
/// ```
#[macro_export]
macro_rules! ch {
    [$send:ident <-? $value:expr] => {
        match $send.try_send($value) {
            Ok(()) => None,
            Err(ch::TrySendError::Full(v)) => Some(v),
            Err(ch::TrySendError::Disconnected(_)) => {
                panic!("Attempted to send a value but receivers are disconnected");
            }
        }
    };

    [$send:ident <- $value:expr] => {
        match $send.send($value) {
            Ok(_) => {},
            Err(err) => panic!("{} for `send`.", err),
        }
    };

    [<-? $recv:ident] => {
        match $recv.try_recv() {
            Ok(v) => Some(v),
            Err(ch::TryRecvError::Empty) => None,
            Err(ch::TryRecvError::Disconnected) => {
                panic!("Attempted to recv a value but senders are disconnected");
            }
        }
    };
    [<- $recv:ident] => {
        match $recv.recv() {
            Ok(v) => v,
            Err(err) => panic!("{} for `recv`.", err),
        }
    };


    [! <-? $recv:ident] => {
        match $recv.try_recv() {
            Ok(v) => panic!("Got {:?} when expecting senders to be closed.", v),
            Err(ch::TryRecvError::Empty) => true,  // senders still exist
            Err(ch::TryRecvError::Disconnected) => false, // no more senders
        }
    };
    [! <- $recv:ident] => {
        match $recv.recv() {
            Ok(v) => panic!("Got {:?} when expecting senders to be closed.", v),
            Err(err) => (),
        }
    };
}

/// Handle an expression that could be `Err` and send it over a channel if it is.
///
/// This is the same as the builtin `try!` macro, except if the expression fails than the `Err` is
/// sent on the `$send` channel and the requested action is performed.
///
/// Suggested possible actions:
/// - `continue`
/// - `return`
/// - `break`
/// - some expression that evaluates to a "default value" for that context.
///
/// # Examples
///
/// ```rust
/// #[macro_use] extern crate ergo_sync;
/// use ergo_sync::*;
/// # fn main() {
/// let (send_err, recv_err) = ch::unbounded();
/// let items = &[Ok("this is alright"), Err("not ok"), Err("still not okay")];
/// # let mut okay = 0;
/// for item in items.iter() {
///     let v = ch_try!(send_err, *item, continue);
///     println!("got: {}", v);
///     # okay += 1;
/// }
///
/// drop(send_err);
/// let errs: Vec<_> = recv_err.iter().collect();
/// assert_eq!(vec!["not ok", "still not okay"], errs);
/// # assert_eq!(1, okay);
/// # }
/// ```
#[macro_export]
macro_rules! ch_try {
    [$send:ident, $expr:expr, $action:expr] => {
        match $expr {
            Ok(v) => v,
            Err(e) => {
                ch!($send <- e);
                $action
            }
        }
    };
}