This repository has been archived by the owner on Nov 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
channel.rs
911 lines (836 loc) · 27.3 KB
/
channel.rs
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
//! The channel interface.
use std::fmt;
use std::hash::{Hash, Hasher};
use std::isize;
use std::iter::FusedIterator;
use std::mem;
use std::panic::{RefUnwindSafe, UnwindSafe};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant};
use libc;
use flavors;
use internal::select::{Operation, SelectHandle, Token};
/// A channel in the form of one of the different flavors.
pub struct Channel<T> {
/// The number of senders associated with this channel.
senders: AtomicUsize,
/// This channel's flavor.
flavor: ChannelFlavor<T>,
}
/// Channel flavors.
enum ChannelFlavor<T> {
/// Bounded channel based on a preallocated array.
Array(flavors::array::Channel<T>),
/// Unbounded channel implemented as a linked list.
List(flavors::list::Channel<T>),
/// Zero-capacity channel.
Zero(flavors::zero::Channel<T>),
}
/// Creates a channel of unbounded capacity.
///
/// This type of channel can hold any number of messages (i.e. it has infinite capacity).
///
/// # Examples
///
/// ```
/// use std::thread;
/// use crossbeam_channel as channel;
///
/// let (s, r) = channel::unbounded();
///
/// // An expensive computation.
/// fn fib(n: i32) -> i32 {
/// if n <= 1 {
/// n
/// } else {
/// fib(n - 1) + fib(n - 2)
/// }
/// }
///
/// // Spawn a thread doing an expensive computation.
/// thread::spawn(move || {
/// s.send(fib(20));
/// });
///
/// // Let's see what's the result of the computation.
/// println!("{}", r.recv().unwrap());
/// ```
pub fn unbounded<T>() -> (Sender<T>, Receiver<T>) {
let chan = Arc::new(Channel {
senders: AtomicUsize::new(0),
flavor: ChannelFlavor::List(flavors::list::Channel::new()),
});
let s = Sender::new(chan.clone());
let r = Receiver(ReceiverFlavor::Channel(chan));
(s, r)
}
/// Creates a channel of bounded capacity.
///
/// This type of channel has an internal buffer of length `cap` in which messages get queued.
///
/// A rather special case is zero-capacity channel, also known as *rendezvous* channel. Such a
/// channel cannot hold any messages since its buffer is of length zero. Instead, send and receive
/// operations must be executing at the same time in order to pair up and pass the message over.
///
/// # Examples
///
/// ```
/// use std::thread;
/// use std::time::Duration;
/// use crossbeam_channel as channel;
///
/// let (s, r) = channel::bounded(1);
///
/// // This call returns immediately since there is enough space in the channel.
/// s.send(1);
///
/// thread::spawn(move || {
/// // This call blocks the current thread because the channel is full.
/// // It will be able to complete only after the first message is received.
/// s.send(2);
/// });
///
/// thread::sleep(Duration::from_secs(1));
/// assert_eq!(r.recv(), Some(1));
/// assert_eq!(r.recv(), Some(2));
/// ```
///
/// ```
/// use std::thread;
/// use std::time::Duration;
/// use crossbeam_channel as channel;
///
/// let (s, r) = channel::bounded(0);
///
/// thread::spawn(move || {
/// // This call blocks the current thread until a receive operation appears
/// // on the other side of the channel.
/// s.send(1);
/// });
///
/// thread::sleep(Duration::from_secs(1));
/// assert_eq!(r.recv(), Some(1));
/// ```
pub fn bounded<T>(cap: usize) -> (Sender<T>, Receiver<T>) {
let chan = Arc::new(Channel {
senders: AtomicUsize::new(0),
flavor: {
if cap == 0 {
ChannelFlavor::Zero(flavors::zero::Channel::new())
} else {
ChannelFlavor::Array(flavors::array::Channel::with_capacity(cap))
}
},
});
let s = Sender::new(chan.clone());
let r = Receiver(ReceiverFlavor::Channel(chan));
(s, r)
}
/// Creates a receiver that delivers a message after a certain duration of time.
///
/// The channel is bounded with capacity of 1 and is never closed. Exactly one message will be
/// automatically sent into the channel after `duration` elapses. The message is the instant at
/// which it is sent into the channel.
///
/// # Examples
///
/// ```
/// # #[macro_use]
/// # extern crate crossbeam_channel;
/// # fn main() {
/// use std::time::Duration;
/// use crossbeam_channel as channel;
///
/// let (s, r) = channel::unbounded::<i32>();
///
/// let timeout = Duration::from_millis(100);
/// select! {
/// recv(r, msg) => println!("got {:?}", msg),
/// recv(channel::after(timeout)) => println!("timed out"),
/// }
/// # }
/// ```
///
/// ```
/// use std::thread;
/// use std::time::{Duration, Instant};
/// use crossbeam_channel as channel;
///
/// // Converts a number into a `Duration` in milliseconds.
/// let ms = |ms| Duration::from_millis(ms);
///
/// // Returns `true` if `a` and `b` are very close `Instant`s.
/// let eq = |a, b| a + ms(50) > b && b + ms(50) > a;
///
/// let start = Instant::now();
/// let r = channel::after(ms(100));
///
/// thread::sleep(ms(500));
///
/// // This message was sent 100 ms from the start and received 500 ms from the start.
/// assert!(eq(r.recv().unwrap(), start + ms(100)));
/// assert!(eq(Instant::now(), start + ms(500)));
/// ```
pub fn after(duration: Duration) -> Receiver<Instant> {
Receiver(ReceiverFlavor::After(flavors::after::Channel::new(duration)))
}
/// Creates a receiver that delivers messages periodically.
///
/// The channel is bounded with capacity of 1 and is never closed. Messages will be automatically
/// sent into the channel in intervals of `duration`, but the time intervals are only measured
/// while the channel is empty. The channel always contains at most one message. Each message is
/// the instant at which it is sent into the channel.
///
/// # Examples
///
/// ```
/// use std::thread;
/// use std::time::{Duration, Instant};
/// use crossbeam_channel as channel;
///
/// // Converts a number into a `Duration` in milliseconds.
/// let ms = |ms| Duration::from_millis(ms);
///
/// // Returns `true` if `a` and `b` are very close `Instant`s.
/// let eq = |a, b| a + ms(50) > b && b + ms(50) > a;
///
/// let start = Instant::now();
/// let r = channel::tick(ms(100));
///
/// // This message was sent 100 ms from the start and received 100 ms from the start.
/// assert!(eq(r.recv().unwrap(), start + ms(100)));
/// assert!(eq(Instant::now(), start + ms(100)));
///
/// thread::sleep(ms(500));
///
/// // This message was sent 200 ms from the start and received 600 ms from the start.
/// assert!(eq(r.recv().unwrap(), start + ms(200)));
/// assert!(eq(Instant::now(), start + ms(600)));
///
/// // This message was sent 700 ms from the start and received 700 ms from the start.
/// assert!(eq(r.recv().unwrap(), start + ms(700)));
/// assert!(eq(Instant::now(), start + ms(700)));
/// ```
pub fn tick(duration: Duration) -> Receiver<Instant> {
Receiver(ReceiverFlavor::Tick(flavors::tick::Channel::new(duration)))
}
/// The sending side of a channel.
///
/// Senders can be cloned and shared among multiple threads.
///
/// # Examples
///
/// ```
/// use std::thread;
/// use crossbeam_channel as channel;
///
/// let (s1, r) = channel::unbounded();
/// let s2 = s1.clone();
///
/// thread::spawn(move || s1.send(1));
/// thread::spawn(move || s2.send(2));
///
/// let msg1 = r.recv().unwrap();
/// let msg2 = r.recv().unwrap();
///
/// assert_eq!(msg1 + msg2, 3);
/// ```
pub struct Sender<T>(Arc<Channel<T>>);
unsafe impl<T: Send> Send for Sender<T> {}
unsafe impl<T: Send> Sync for Sender<T> {}
impl<T> Sender<T> {
/// Creates a sender handle for the channel and increments the sender count.
fn new(chan: Arc<Channel<T>>) -> Self {
let old_count = chan.senders.fetch_add(1, Ordering::SeqCst);
// Cloning senders and calling `mem::forget` on the clones could potentially overflow the
// counter. It's very difficult to recover sensibly from such degenerate scenarios so we
// just abort when the count becomes very large.
if old_count > isize::MAX as usize {
unsafe { libc::abort() }
}
Sender(chan)
}
/// Returns a unique identifier for the channel.
fn channel_id(&self) -> usize {
&*self.0 as *const Channel<T> as usize
}
/// Sends a message into the channel, blocking the current thread if the channel is full.
///
/// If called on a zero-capacity channel, this method blocks the current thread until a receive
/// operation appears on the other side of the channel.
///
/// Note: `s.send(msg)` is equivalent to `select! { send(s, msg) => {} }`.
///
/// # Examples
///
/// ```
/// use std::thread;
/// use std::time::Duration;
/// use crossbeam_channel as channel;
///
/// let (s, r) = channel::bounded(0);
///
/// thread::spawn(move || s.send(1));
///
/// assert_eq!(r.recv(), Some(1));
/// ```
pub fn send(&self, msg: T) {
match &self.0.flavor {
ChannelFlavor::Array(chan) => chan.send(msg),
ChannelFlavor::List(chan) => chan.send(msg),
ChannelFlavor::Zero(chan) => chan.send(msg),
}
}
/// Returns `true` if the channel is empty.
///
/// Note: zero-capacity channels are always empty.
///
/// # Examples
///
/// ```
/// use crossbeam_channel as channel;
///
/// let (s, r) = channel::unbounded();
/// assert!(s.is_empty());
///
/// s.send(0);
/// assert!(!s.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
match &self.0.flavor {
ChannelFlavor::Array(chan) => chan.is_empty(),
ChannelFlavor::List(chan) => chan.is_empty(),
ChannelFlavor::Zero(chan) => chan.is_empty(),
}
}
/// Returns `true` if the channel is full.
///
/// Note: zero-capacity channels are always full.
///
/// # Examples
///
/// ```
/// use crossbeam_channel as channel;
///
/// let (s, r) = channel::bounded(1);
///
/// assert!(!s.is_full());
/// s.send(0);
/// assert!(s.is_full());
/// ```
pub fn is_full(&self) -> bool {
match &self.0.flavor {
ChannelFlavor::Array(chan) => chan.is_full(),
ChannelFlavor::List(chan) => chan.is_full(),
ChannelFlavor::Zero(chan) => chan.is_full(),
}
}
/// Returns the number of messages in the channel.
///
/// # Examples
///
/// ```
/// use crossbeam_channel as channel;
///
/// let (s, r) = channel::unbounded();
/// assert_eq!(s.len(), 0);
///
/// s.send(1);
/// s.send(2);
/// assert_eq!(s.len(), 2);
/// ```
pub fn len(&self) -> usize {
match &self.0.flavor {
ChannelFlavor::Array(chan) => chan.len(),
ChannelFlavor::List(chan) => chan.len(),
ChannelFlavor::Zero(chan) => chan.len(),
}
}
/// If the channel is bounded, returns its capacity.
///
/// # Examples
///
/// ```
/// use crossbeam_channel as channel;
///
/// let (s, _) = channel::unbounded::<i32>();
/// assert_eq!(s.capacity(), None);
///
/// let (s, _) = channel::bounded::<i32>(5);
/// assert_eq!(s.capacity(), Some(5));
///
/// let (s, _) = channel::bounded::<i32>(0);
/// assert_eq!(s.capacity(), Some(0));
/// ```
pub fn capacity(&self) -> Option<usize> {
match &self.0.flavor {
ChannelFlavor::Array(chan) => chan.capacity(),
ChannelFlavor::List(chan) => chan.capacity(),
ChannelFlavor::Zero(chan) => chan.capacity(),
}
}
}
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
if self.0.senders.fetch_sub(1, Ordering::SeqCst) == 1 {
match &self.0.flavor {
ChannelFlavor::Array(chan) => chan.close(),
ChannelFlavor::List(chan) => chan.close(),
ChannelFlavor::Zero(chan) => chan.close(),
};
}
}
}
impl<T> Clone for Sender<T> {
fn clone(&self) -> Self {
Sender::new(self.0.clone())
}
}
impl<T> fmt::Debug for Sender<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Sender").finish()
}
}
impl<T> Hash for Sender<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.channel_id().hash(state)
}
}
impl<T> PartialEq for Sender<T> {
fn eq(&self, other: &Sender<T>) -> bool {
self.channel_id() == other.channel_id()
}
}
impl<T> Eq for Sender<T> {}
impl<T> PartialEq<Receiver<T>> for Sender<T> {
fn eq(&self, other: &Receiver<T>) -> bool {
self.channel_id() == other.channel_id()
}
}
impl<T> UnwindSafe for Sender<T> {}
impl<T> RefUnwindSafe for Sender<T> {}
/// The receiving side of a channel.
///
/// Receivers can be cloned and shared among multiple threads.
///
/// # Examples
///
/// ```
/// use std::thread;
/// use std::time::Duration;
/// use crossbeam_channel as channel;
///
/// let (s, r) = channel::unbounded();
///
/// thread::spawn(move || {
/// s.send("Hello world!");
/// thread::sleep(Duration::from_secs(2));
/// s.send("Delayed for 2 seconds");
/// });
///
/// println!("{}", r.recv().unwrap()); // Received immediately.
/// println!("Waiting...");
/// println!("{}", r.recv().unwrap()); // Received after 2 seconds.
/// ```
pub struct Receiver<T>(ReceiverFlavor<T>);
/// Receiver flavors.
pub enum ReceiverFlavor<T> {
/// A regular channel (array, list, or zero flavor).
Channel(Arc<Channel<T>>),
/// The after flavor.
After(flavors::after::Channel),
/// The tick flavor.
Tick(flavors::tick::Channel),
}
unsafe impl<T: Send> Send for Receiver<T> {}
unsafe impl<T: Send> Sync for Receiver<T> {}
impl<T> Receiver<T> {
/// Returns a unique identifier for the channel.
fn channel_id(&self) -> usize {
match &self.0 {
ReceiverFlavor::Channel(chan) => &**chan as *const Channel<T> as usize,
ReceiverFlavor::After(chan) => chan.channel_id(),
ReceiverFlavor::Tick(chan) => chan.channel_id(),
}
}
/// Blocks the current thread until a message is received or the channel is closed.
///
/// Returns the message if it was received, or `None` if the channel is closed and empty.
///
/// If called on a zero-capacity channel, this method blocks the current thread until a send
/// operation appears on the other side of the channel or it becomes closed.
///
/// Note: `r.recv()` is equivalent to `select! { recv(r, msg) => msg }`.
///
/// # Examples
///
/// ```
/// use std::thread;
/// use std::time::Duration;
/// use crossbeam_channel as channel;
///
/// let (s, r) = channel::unbounded();
///
/// thread::spawn(move || {
/// thread::sleep(Duration::from_secs(1));
/// s.send(5);
/// // `s` gets dropped, thus closing the channel.
/// });
///
/// assert_eq!(r.recv(), Some(5));
/// assert_eq!(r.recv(), None);
/// ```
pub fn recv(&self) -> Option<T> {
match &self.0 {
ReceiverFlavor::Channel(arc) => match &arc.flavor {
ChannelFlavor::Array(chan) => chan.recv(),
ChannelFlavor::List(chan) => chan.recv(),
ChannelFlavor::Zero(chan) => chan.recv(),
},
ReceiverFlavor::After(chan) => unsafe {
mem::transmute_copy::<Option<Instant>, Option<T>>(&chan.recv())
},
ReceiverFlavor::Tick(chan) => unsafe {
mem::transmute_copy::<Option<Instant>, Option<T>>(&chan.recv())
},
}
}
/// Attempts to receive a message from the channel without blocking.
///
/// If there is no message ready to be received, returns `None`.
///
/// Note: `r.try_recv()` is equivalent to `select! { recv(r, msg) => msg, default => None }`.
///
/// # Examples
///
/// ```
/// use crossbeam_channel as channel;
///
/// let (s, r) = channel::unbounded();
/// assert_eq!(r.try_recv(), None);
///
/// s.send(5);
/// drop(s);
///
/// assert_eq!(r.try_recv(), Some(5));
/// assert_eq!(r.try_recv(), None);
/// ```
pub fn try_recv(&self) -> Option<T> {
match recv_nonblocking(self) {
RecvNonblocking::Message(msg) => Some(msg),
RecvNonblocking::Empty => None,
RecvNonblocking::Closed => None,
}
}
/// Returns `true` if the channel is empty.
///
/// Note: zero-capacity channels are always empty.
///
/// # Examples
///
/// ```
/// use crossbeam_channel as channel;
///
/// let (s, r) = channel::unbounded();
///
/// assert!(r.is_empty());
/// s.send(0);
/// assert!(!r.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
match &self.0 {
ReceiverFlavor::Channel(arc) => match &arc.flavor {
ChannelFlavor::Array(chan) => chan.is_empty(),
ChannelFlavor::List(chan) => chan.is_empty(),
ChannelFlavor::Zero(chan) => chan.is_empty(),
},
ReceiverFlavor::After(chan) => chan.is_empty(),
ReceiverFlavor::Tick(chan) => chan.is_empty(),
}
}
/// Returns `true` if the channel is full.
///
/// Note: zero-capacity channels are always full.
///
/// # Examples
///
/// ```
/// use crossbeam_channel as channel;
///
/// let (s, r) = channel::bounded(1);
///
/// assert!(!r.is_full());
/// s.send(0);
/// assert!(r.is_full());
/// ```
pub fn is_full(&self) -> bool {
match &self.0 {
ReceiverFlavor::Channel(arc) => match &arc.flavor {
ChannelFlavor::Array(chan) => chan.is_full(),
ChannelFlavor::List(chan) => chan.is_full(),
ChannelFlavor::Zero(chan) => chan.is_full(),
},
ReceiverFlavor::After(chan) => !chan.is_empty(),
ReceiverFlavor::Tick(chan) => !chan.is_empty(),
}
}
/// Returns the number of messages in the channel.
///
/// # Examples
///
/// ```
/// use crossbeam_channel as channel;
///
/// let (s, r) = channel::unbounded();
/// assert_eq!(r.len(), 0);
///
/// s.send(1);
/// s.send(2);
/// assert_eq!(r.len(), 2);
/// ```
pub fn len(&self) -> usize {
match &self.0 {
ReceiverFlavor::Channel(arc) => match &arc.flavor {
ChannelFlavor::Array(chan) => chan.len(),
ChannelFlavor::List(chan) => chan.len(),
ChannelFlavor::Zero(chan) => chan.len(),
},
ReceiverFlavor::After(chan) => chan.len(),
ReceiverFlavor::Tick(chan) => chan.len(),
}
}
/// If the channel is bounded, returns its capacity.
///
/// # Examples
///
/// ```
/// use crossbeam_channel as channel;
///
/// let (_, r) = channel::unbounded::<i32>();
/// assert_eq!(r.capacity(), None);
///
/// let (_, r) = channel::bounded::<i32>(5);
/// assert_eq!(r.capacity(), Some(5));
///
/// let (_, r) = channel::bounded::<i32>(0);
/// assert_eq!(r.capacity(), Some(0));
/// ```
pub fn capacity(&self) -> Option<usize> {
match &self.0 {
ReceiverFlavor::Channel(arc) => match &arc.flavor {
ChannelFlavor::Array(chan) => chan.capacity(),
ChannelFlavor::List(chan) => chan.capacity(),
ChannelFlavor::Zero(chan) => chan.capacity(),
},
ReceiverFlavor::After(chan) => chan.capacity(),
ReceiverFlavor::Tick(chan) => chan.capacity(),
}
}
}
impl<T> Clone for Receiver<T> {
fn clone(&self) -> Self {
let inner = match &self.0 {
ReceiverFlavor::Channel(arc) => ReceiverFlavor::Channel(arc.clone()),
ReceiverFlavor::After(chan) => ReceiverFlavor::After(chan.clone()),
ReceiverFlavor::Tick(chan) => ReceiverFlavor::Tick(chan.clone()),
};
Receiver(inner)
}
}
impl<T> fmt::Debug for Receiver<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Receiver").finish()
}
}
impl<T> Hash for Receiver<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.channel_id().hash(state)
}
}
impl<T> PartialEq for Receiver<T> {
fn eq(&self, other: &Receiver<T>) -> bool {
self.channel_id() == other.channel_id()
}
}
impl<T> Eq for Receiver<T> {}
impl<T> PartialEq<Sender<T>> for Receiver<T> {
fn eq(&self, other: &Sender<T>) -> bool {
self.channel_id() == other.channel_id()
}
}
impl<T> Iterator for Receiver<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
self.recv()
}
}
impl<T> FusedIterator for Receiver<T> {}
impl<'a, T> IntoIterator for &'a Receiver<T> {
type Item = T;
type IntoIter = Receiver<T>;
fn into_iter(self) -> Self::IntoIter {
self.clone()
}
}
impl<T> UnwindSafe for Receiver<T> {}
impl<T> RefUnwindSafe for Receiver<T> {}
impl<T> SelectHandle for Sender<T> {
fn try(&self, token: &mut Token) -> bool {
match &self.0.flavor {
ChannelFlavor::Array(chan) => chan.sender().try(token),
ChannelFlavor::List(chan) => chan.sender().try(token),
ChannelFlavor::Zero(chan) => chan.sender().try(token),
}
}
fn retry(&self, token: &mut Token) -> bool {
match &self.0.flavor {
ChannelFlavor::Array(chan) => chan.sender().retry(token),
ChannelFlavor::List(chan) => chan.sender().retry(token),
ChannelFlavor::Zero(chan) => chan.sender().retry(token),
}
}
fn deadline(&self) -> Option<Instant> {
None
}
fn register(&self, token: &mut Token, oper: Operation) -> bool {
match &self.0.flavor {
ChannelFlavor::Array(chan) => chan.sender().register(token, oper),
ChannelFlavor::List(chan) => chan.sender().register(token, oper),
ChannelFlavor::Zero(chan) => chan.sender().register(token, oper),
}
}
fn unregister(&self, oper: Operation) {
match &self.0.flavor {
ChannelFlavor::Array(chan) => chan.sender().unregister(oper),
ChannelFlavor::List(chan) => chan.sender().unregister(oper),
ChannelFlavor::Zero(chan) => chan.sender().unregister(oper),
}
}
fn accept(&self, token: &mut Token) -> bool {
match &self.0.flavor {
ChannelFlavor::Array(chan) => chan.sender().accept(token),
ChannelFlavor::List(chan) => chan.sender().accept(token),
ChannelFlavor::Zero(chan) => chan.sender().accept(token),
}
}
}
impl<T> SelectHandle for Receiver<T> {
fn try(&self, token: &mut Token) -> bool {
match &self.0 {
ReceiverFlavor::Channel(arc) => match &arc.flavor {
ChannelFlavor::Array(chan) => chan.receiver().try(token),
ChannelFlavor::List(chan) => chan.receiver().try(token),
ChannelFlavor::Zero(chan) => chan.receiver().try(token),
},
ReceiverFlavor::After(chan) => chan.try(token),
ReceiverFlavor::Tick(chan) => chan.try(token),
}
}
fn retry(&self, token: &mut Token) -> bool {
match &self.0 {
ReceiverFlavor::Channel(arc) => match &arc.flavor {
ChannelFlavor::Array(chan) => chan.receiver().retry(token),
ChannelFlavor::List(chan) => chan.receiver().retry(token),
ChannelFlavor::Zero(chan) => chan.receiver().retry(token),
},
ReceiverFlavor::After(chan) => chan.retry(token),
ReceiverFlavor::Tick(chan) => chan.retry(token),
}
}
fn deadline(&self) -> Option<Instant> {
match &self.0 {
ReceiverFlavor::Channel(_) => None,
ReceiverFlavor::After(chan) => chan.deadline(),
ReceiverFlavor::Tick(chan) => chan.deadline(),
}
}
fn register(&self, token: &mut Token, oper: Operation) -> bool {
match &self.0 {
ReceiverFlavor::Channel(arc) => match &arc.flavor {
ChannelFlavor::Array(chan) => chan.receiver().register(token, oper),
ChannelFlavor::List(chan) => chan.receiver().register(token, oper),
ChannelFlavor::Zero(chan) => chan.receiver().register(token, oper),
},
ReceiverFlavor::After(chan) => chan.register(token, oper),
ReceiverFlavor::Tick(chan) => chan.register(token, oper),
}
}
fn unregister(&self, oper: Operation) {
match &self.0 {
ReceiverFlavor::Channel(arc) => match &arc.flavor {
ChannelFlavor::Array(chan) => chan.receiver().unregister(oper),
ChannelFlavor::List(chan) => chan.receiver().unregister(oper),
ChannelFlavor::Zero(chan) => chan.receiver().unregister(oper),
},
ReceiverFlavor::After(chan) => chan.unregister(oper),
ReceiverFlavor::Tick(chan) => chan.unregister(oper),
}
}
fn accept(&self, token: &mut Token) -> bool {
match &self.0 {
ReceiverFlavor::Channel(arc) => match &arc.flavor {
ChannelFlavor::Array(chan) => chan.receiver().accept(token),
ChannelFlavor::List(chan) => chan.receiver().accept(token),
ChannelFlavor::Zero(chan) => chan.receiver().accept(token),
},
ReceiverFlavor::After(chan) => chan.accept(token),
ReceiverFlavor::Tick(chan) => chan.accept(token),
}
}
}
/// Writes a message into the channel.
pub unsafe fn write<T>(s: &Sender<T>, token: &mut Token, msg: T) {
match &s.0.flavor {
ChannelFlavor::Array(chan) => chan.write(token, msg),
ChannelFlavor::List(chan) => chan.write(token, msg),
ChannelFlavor::Zero(chan) => chan.write(token, msg),
}
}
/// Receives a message from the channel.
pub unsafe fn read<T>(r: &Receiver<T>, token: &mut Token) -> Option<T> {
match &r.0 {
ReceiverFlavor::Channel(arc) => match &arc.flavor {
ChannelFlavor::Array(chan) => chan.read(token),
ChannelFlavor::List(chan) => chan.read(token),
ChannelFlavor::Zero(chan) => chan.read(token),
},
ReceiverFlavor::After(chan) => {
mem::transmute_copy::<Option<Instant>, Option<T>>(&chan.read(token))
},
ReceiverFlavor::Tick(chan) => {
mem::transmute_copy::<Option<Instant>, Option<T>>(&chan.read(token))
},
}
}
/// The result of a non-blocking receive operation.
pub enum RecvNonblocking<T> {
/// A message was received.
Message(T),
/// The channel is empty.
Empty,
/// The channel is empty and closed.
Closed,
}
/// Attempts to receive a message without blocking.
pub fn recv_nonblocking<T>(r: &Receiver<T>) -> RecvNonblocking<T> {
match &r.0 {
ReceiverFlavor::Channel(arc) => match &arc.flavor {
ChannelFlavor::Array(chan) => chan.recv_nonblocking(),
ChannelFlavor::List(chan) => chan.recv_nonblocking(),
ChannelFlavor::Zero(chan) => chan.recv_nonblocking(),
},
ReceiverFlavor::After(chan) => {
let res = chan.recv_nonblocking();
unsafe {
mem::transmute_copy::<RecvNonblocking<Instant>, RecvNonblocking<T>>(&res)
}
},
ReceiverFlavor::Tick(chan) => {
let res = chan.recv_nonblocking();
unsafe {
mem::transmute_copy::<RecvNonblocking<Instant>, RecvNonblocking<T>>(&res)
}
},
}
}