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
//! A variant of `std::sync::mpsc` that supports `map`-style operations.
//!
//!
//! This crate is near a drop-in replacement for `std::sync::mpsc`. The only difference is that
//! the type `ExtSender<T>`, which replaces `Sender<T>`, supports operations such as `map`,
//! `filter` and `filter_map` -- see the documentation of `TransformableSender` for all
//! details and examples.
//!
//! The type `Receiver<T>` is reexported from std::sync::mpsc::Receiver.
use std::sync::mpsc::{ channel as std_channel, Sender };
pub use std::sync::mpsc::Receiver;

use std::sync::Arc;
use std::marker::PhantomData;
use std::ops::Deref;

/// Creates a new asynchronous channel, returning the sender/receiver halves.
/// All data sent on the sender will become available on the receiver, and no send will block
/// the calling thread (this channel has an "infinite buffer").
///
/// Receivers are identical to those of `std::sync::mpsc::Receiver`.
/// Senders have the same behavior as those of `std::sync::mpsc::Sender`, with the exception
/// of error values.
///
/// # Example
///
/// ```
/// use std::thread;
/// use transformable_channels::mpsc::*;
///
/// let (tx, rx) = channel();
///
/// for i in 1 .. 5 {
///   tx.send(i).unwrap();
/// }
///
/// thread::spawn(move || {
///   for (msg, i) in rx.iter().zip(1..5) {
///     assert_eq!(msg, i);
///   }
/// });
/// ```
pub fn channel<T>() -> (RawSender<T>, Receiver<T>) where T: Send + 'static {
    let (tx, rx) = std_channel();
    (RawSender { std_sender: tx }, rx)
}

/// The sending-half of this crate's asynchronous channel type. This half can only be owned by one
/// thread, but all instances can be cloned to let copies be shared with other threads.
///
/// All implementations of `ExtSender` provided in this crate implement `TransformableSender`,
/// which provides
pub trait ExtSender<T>: Send + 'static where T: Send + 'static {
    /// Attempts to send a value on this channel.
    ///
    /// A successful send occurs when it is determined that the other end of the channel has not
    /// hung up already. An unsuccessful send would be one where the corresponding receiver has
    /// already been deallocated. Note that a return value of `Err` means that the data will never
    /// be received, but a return value of `Ok` does not mean that the data will be received. It
    /// is possible for the corresponding receiver to hang up immediately after this function
    /// returns `Ok`.
    ///
    /// This method will never block the current thread.
    fn send(&self, t: T) -> Result<(), ()>;

    /// A low-level method used to define Clone(). Probably not useful outside of this crate. May
    /// disappear in future versions.
    fn internal_clone(&self) -> Box<ExtSender<T>>;
}

pub trait TransformableSender<V>: Send + 'static where V: Send + 'static, Self: ExtSender<V> + Clone {
    /// From an `ExtSender`, derive a new `ExtSender` with the same `Receiver`, but which transforms
    /// values prior to transmitting them.
    ///
    /// This allows, for instance, converting from one data type to another one, or labelling values
    /// with their origin.
    ///
    /// Note that the closure will often be shared between threads and must therefore implement `Sync`.
    ///
    /// # Safety and performance
    ///
    /// Argument `f` is executed during the call to `send`. Therefore, any panic in `f` will cause the
    /// sender thread to panic. If `f` is slow, the sender thread will be slowed down.
    ///
    /// # Example
    ///
    /// ```
    /// use transformable_channels::mpsc::*;
    ///
    /// let (tx, rx) = channel();
    ///
    /// #[derive(Debug)]
    /// struct LabelledValue<T> {
    ///   origin: usize,
    ///   value: T,
    /// }
    ///
    /// for i in 1 .. 5 {
    ///   // Let us label messages as they are sent.
    ///   let tx = tx.map(move |msg| {
    ///      LabelledValue {
    ///        origin: i,
    ///        value: msg,
    ///      }
    ///   });
    ///
    ///   tx.send(i).unwrap();
    /// }
    ///
    /// for _ in 1 .. 5 {
    ///   let msg = rx.recv().unwrap();
    ///   assert_eq!(msg.origin, msg.value);
    /// }
    /// ```
    fn map<F, T>(&self, f: F) -> MappedSender<F, T, V, Self> where
        Self: Sized,
        F: Fn(T) -> V + Sync + Send + 'static,
        T: Send + 'static,
        V: Send + 'static
    {
        MappedSender {
            wrapped: self.clone(),
            transformer: Arc::new(f),
            phantom: PhantomData
        }
    }

    /// From an `ExtSender`, derive a new `ExtSender` with the same `Receiver`, but which may decide
    /// to discard some values instead of transmitting them.
    ///
    /// This allows, for instance, discarding events that are not of interest.
    ///
    /// Note that the closure will often be shared between threads and must therefore implement `Sync`.
    ///
    /// # Safety and performance
    ///
    /// Argument `f` is executed during the call to `send`. Therefore, any panic in `f` will cause the
    /// sender thread to panic. If `f` is slow, the sender thread will be slowed down.
    ///
    /// # Example
    ///
    /// ```
    /// use transformable_channels::mpsc::*;
    ///
    /// let (tx, rx) = channel();
    ///
    /// // We are only interested in even messages
    /// let tx = tx.filter(move |msg| {
    ///    msg % 2 == 0
    /// });
    ///
    /// for i in 1 .. 10 {
    ///   tx.send(i).unwrap();
    /// }
    ///
    /// for _ in 1 .. 5 {
    ///   assert!(rx.recv().unwrap() % 2 == 0);
    /// }
    /// ```
    fn filter<F>(&self, f: F) -> FilteredSender<F, V, Self> where
        Self: Sized,
        F: Fn(&V) -> bool + Sync + Send + 'static,
    {
        FilteredSender {
            wrapped: self.clone(),
            filter: Arc::new(f),
            phantom: PhantomData
        }
    }

    /// From an `ExtSender`, derive a new `ExtSender` with the same `Receiver`, but which may both
    /// transform values priori to transmitting them, or to discard them entirely.
    ///
    /// This combines the features of `map` and `filter`.
    ///
    /// Note that the closure will often be shared between threads and must therefore implement `Sync`.
    ///
    /// # Safety and performance
    ///
    /// Argument `f` is executed during the call to `send`. Therefore, any panic in `f` will cause the
    /// sender thread to panic. If `f` is slow, the sender thread will be slowed down.
    ///
    /// # Example
    ///
    /// ```
    /// use transformable_channels::mpsc::*;
    ///
    /// let (tx, rx) = channel();
    ///
    /// // We are only interested in even messages
    /// let tx = tx.filter_map(move |msg| {
    ///    if msg % 2 == 0 {
    ///      Some( msg + 1 )
    ///    } else {
    ///      None
    ///    }
    /// });
    ///
    /// for i in 1 .. 10 {
    ///   tx.send(i).unwrap();
    /// }
    ///
    /// for i in 1 .. 5 {
    ///   assert_eq!(rx.recv().unwrap(), i * 2 + 1);
    /// }
    /// ```
    fn filter_map<F, T>(&self, f: F) -> FilterMappedSender<F, T, V, Self> where
        Self: Sized,
        F: Fn(T) -> Option<V> + Sync + Send + 'static,
        T: Send + 'static,
    {
        FilterMappedSender {
            wrapped: self.clone(),
            transformer: Arc::new(f),
            phantom: PhantomData
        }
    }

    /// From two `ExtSender`s, derive a new `ExtSender` which sends to both `ExtSender`s.
    ///
    /// # Example
    ///
    /// ```
    /// use transformable_channels::mpsc::*;
    ///
    /// let (tx_1, rx_1) = channel();
    /// let (tx_2, rx_2) = channel();
    ///
    /// let tx = tx_1.tie(&tx_2);
    ///
    /// tx.send(("one".to_string(), 1)).unwrap();
    /// assert_eq!(rx_1.recv().unwrap(), "one".to_string());
    /// assert_eq!(rx_2.recv().unwrap(), 1);
    /// ```
    fn tie<S, W>(&self, other: &S) -> TiedSender<Self, S, V, W> where
        Self: Sized,
        S: ExtSender<W> + TransformableSender<W>,
        W: Send + 'static,
    {
        TiedSender {
            left: self.clone(),
            right: other.clone(),
            phantom: PhantomData
        }
    }

}

/// A trait object for an ExtSender.
impl<T> ExtSender<T> for Box<ExtSender<T>> where T: Send + 'static {
    fn send(&self, t: T) -> Result<(), ()> {
        self.deref().send(t)
    }
    fn internal_clone(&self) -> Box<ExtSender<T>> {
        self.deref().internal_clone()
    }
}
impl<T> Clone for Box<ExtSender<T>> where T: Send + 'static {
  fn clone(&self) -> Self {
      self.internal_clone()
  }
}
impl<T> TransformableSender<T> for Box<ExtSender<T>> where T: Send + 'static { }

/// An implementation of `ExtSender` directly on top of `std::sync::mpsc::Sender` and with
/// the same performance.
pub struct RawSender<T> where T: Send + 'static {
    std_sender: Sender<T>
}
impl<T> ExtSender<T> for RawSender<T> where T: Send + 'static {
    /// Let the wrapped `Sender` send the value. Discard error values.
    fn send(&self, t: T) -> Result<(), ()> {
        match (self.std_sender).send(t) {
            Ok(_) => Ok(()),
            Err(_) => Err(())
        }
    }

    /// Clone the wrapped `Sender` and wrap the result.
    fn internal_clone(&self) -> Box<ExtSender<T>> {
        Box::new(RawSender {
            std_sender: self.std_sender.clone()
        })
    }
}
impl<T> Clone for RawSender<T> where T: Send + 'static {
    fn clone(&self) -> Self {
        RawSender {
            std_sender: self.std_sender.clone()
        }
    }
}
impl<T> TransformableSender<T> for RawSender<T> where T: Send + 'static { }

/// An `ExtSender` obtained from a call to method `filter_map`.
pub struct FilterMappedSender<F, T, V, W> where
    W: ExtSender<V> + TransformableSender<V> + Sized,
    F: Fn(T) -> Option<V> + Sync + Send + 'static,
    T: Send + 'static,
    V: Send + 'static,
{
    wrapped: W,
    transformer: Arc<F>,
    phantom: PhantomData<(T, V)>
}
impl<F, T, V, W> ExtSender<T> for FilterMappedSender<F, T, V, W> where
    W: ExtSender<V> + TransformableSender<V> + Sized,
    F: Fn(T) -> Option<V> + Sync + Send + 'static,
    T: Send + 'static,
    V: Send + 'static
{
    fn send(&self, t: T) -> Result<(), ()> {
        match (self.transformer)(t) {
            None => Ok(()),
            Some(t2) => (self.wrapped).send(t2)
        }
    }
    fn internal_clone(&self) -> Box<ExtSender<T>> {
        Box::new(FilterMappedSender {
            wrapped: self.wrapped.clone(),
            transformer: self.transformer.clone(),
            phantom: PhantomData::<(T, V)>
        })
    }
}
impl<F, T, V, W> Clone for FilterMappedSender<F, T, V, W>  where
    W: ExtSender<V> + TransformableSender<V> + Sized,
    F: Fn(T) -> Option<V> + Sync + Send + 'static,
    T: Send + 'static,
    V: Send + 'static
{
    fn clone(&self) -> Self {
        FilterMappedSender {
            wrapped: self.wrapped.clone(),
            transformer: self.transformer.clone(),
            phantom: PhantomData
        }
    }
}
impl<F, T, V, W> TransformableSender<T> for FilterMappedSender<F, T, V, W>  where
    W: ExtSender<V> + TransformableSender<V> + Sized,
    F: Fn(T) -> Option<V> + Sync + Send + 'static,
    T: Send + 'static,
    V: Send + 'static
{}

/// An `ExtSender` obtained from a call to method `filter`.
pub struct FilteredSender<F, T, W> where
    F: Fn(&T) -> bool + Sync + Send + 'static,
    T: Send + 'static,
    W: ExtSender<T> + TransformableSender<T> + Sized,
{
    wrapped: W,
    filter: Arc<F>,
    phantom: PhantomData<T>
}
impl<F, T, W> ExtSender<T> for FilteredSender<F, T, W> where
    F: Fn(&T) -> bool + Sync + Send + 'static,
    T: Send + 'static,
    W: ExtSender<T> + TransformableSender<T> + Sized,
{
    fn send(&self, t: T) -> Result<(), ()> {
        if (self.filter)(&t) {
            (self.wrapped).send(t)
        } else {
            Ok(())
        }
    }
    fn internal_clone(&self) -> Box<ExtSender<T>> {
        Box::new(FilteredSender {
            wrapped: self.wrapped.clone(),
            filter: self.filter.clone(),
            phantom: PhantomData
        })
    }
}
impl<F, T, W> Clone for FilteredSender<F, T, W> where
    F: Fn(&T) -> bool + Sync + Send + 'static,
    T: Send + 'static,
    W: ExtSender<T> + TransformableSender<T> + Sized,
{
    fn clone(&self) -> Self {
        FilteredSender {
            wrapped: self.wrapped.clone(),
            filter: self.filter.clone(),
            phantom: PhantomData
        }
    }
}
impl<F, T, W> TransformableSender<T> for FilteredSender<F, T, W> where
    F: Fn(&T) -> bool + Sync + Send + 'static,
    T: Send + 'static,
    W: ExtSender<T> + TransformableSender<T> + Sized,
{}

/// An `ExtSender` obtained from a call to method `map`.
pub struct MappedSender<F, T, V, W> where
    W: ExtSender<V> + TransformableSender<V> + Sized,
    F: Fn(T) -> V + Sync + Send + 'static,
    T: Send + 'static,
    V: Send + 'static,
{
    wrapped: W,
    transformer: Arc<F>,
    phantom: PhantomData<(T, V)>
}
impl<F, T, V, W> ExtSender<T> for MappedSender<F, T, V, W> where
    W: ExtSender<V> + TransformableSender<V> + Sized,
    F: Fn(T) -> V + Sync + Send + 'static,
    T: Send + 'static,
    V: Send + 'static
{
    fn send(&self, t: T) -> Result<(), ()> {
        (self.wrapped).send((self.transformer)(t))
    }
    fn internal_clone(&self) -> Box<ExtSender<T>> {
        Box::new(MappedSender {
            wrapped: self.wrapped.clone(),
            transformer: self.transformer.clone(),
            phantom: PhantomData::<(T, V)>
        })
    }
}
impl<F, T, V, W> Clone for MappedSender<F, T, V, W> where
    W: ExtSender<V> + TransformableSender<V> + Sized,
    F: Fn(T) -> V + Sync + Send + 'static,
    T: Send + 'static,
    V: Send + 'static
{
    fn clone(&self) -> Self {
        MappedSender {
            wrapped: self.wrapped.clone(),
            transformer: self.transformer.clone(),
            phantom: PhantomData
        }
    }
}
impl<F, T, V, W> TransformableSender<T> for MappedSender<F, T, V, W> where
    W: ExtSender<V> + TransformableSender<V> + Sized,
    F: Fn(T) -> V + Sync + Send + 'static,
    T: Send + 'static,
    V: Send + 'static
{}

/// An `ExtSender` obtained from a call to method `tie`.
pub struct TiedSender<S1, S2, V1, V2> where
    S1: ExtSender<V1> + TransformableSender<V1> + Sized,
    S2: ExtSender<V2> + TransformableSender<V2> + Sized,
    V1: Send + 'static,
    V2: Send + 'static,
{
    left: S1,
    right: S2,
    phantom: PhantomData<(V1, V2)>
}
impl<S1, S2, V1, V2> ExtSender<(V1, V2)> for TiedSender<S1, S2, V1, V2>
where
    S1: ExtSender<V1> + TransformableSender<V1> + Sized,
    S2: ExtSender<V2> + TransformableSender<V2> + Sized,
    V1: Send + 'static,
    V2: Send + 'static,
{
    fn send(&self, (left, right): (V1, V2)) -> Result<(), ()> {
        match (self.left.send(left), self.right.send(right)) {
            (Err(()), Err(())) => Err(()), // By spec, return `Err()` only if we are sure that
                // the message didn't reach is destination.
            _ => Ok(()) // Otherwise, return `Ok()`
        }
    }
    fn internal_clone(&self) -> Box<ExtSender<(V1, V2)>> {
        Box::new(TiedSender {
            left: self.left.clone(),
            right: self.right.clone(),
            phantom: PhantomData
        })
    }
}
impl<S1, S2, V1, V2> Clone for TiedSender<S1, S2, V1, V2>
where
    S1: ExtSender<V1> + TransformableSender<V1> + Sized,
    S2: ExtSender<V2> + TransformableSender<V2> + Sized,
    V1: Send + 'static,
    V2: Send + 'static,
{
    fn clone(&self) -> Self {
        TiedSender {
            left: self.left.clone(),
            right: self.right.clone(),
            phantom: PhantomData
        }
    }
}

#[test]
fn test_chain_map() {
    println!("* Making sure that chain composition of .map() takes place in the right order");
    let (tx, rx) = channel();
    let tx = tx.map(|x| x + 1).map(|x| x * 2);
    tx.send(0).unwrap();
    assert_eq!(rx.recv().unwrap(), 1);
}


#[test]
fn test_filter_map() {
    let (tx, rx) = channel();
    let tx2 = (tx.map(|x| { x + 1 })).filter(|x| x % 2 == 0);
    let tx3 = tx.filter_map(|x| if x%2 == 0 { Some (x + 1) } else { None });

    for i in 1 .. 10 {
        tx2.send(i).unwrap();
        tx3.send(i).unwrap();
        if i % 2 == 0 {
            let got_2 = rx.recv().unwrap();
            let got_3 = rx.recv().unwrap();
            assert_eq!(got_2, got_3);
        }
    }
}