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
use std::sync::mpsc::{channel, Receiver, RecvError, Sender, SendError};
use std::sync::{Arc, Mutex, MutexGuard};
use std::fmt;
use std::any::Any;
use std::error::Error;
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum BroadcastError<T> {
SendError(T),
RecvError,
}
impl<T: fmt::Display> fmt::Display for BroadcastError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
BroadcastError::SendError(ref t) =>
write!(fmt, "could not send data on channel: {}", t),
BroadcastError::RecvError =>
write!(fmt, "could not receive data on channel"),
}
}
}
impl<T: Send + fmt::Display + fmt::Debug + Any> Error for BroadcastError<T> {
fn description(&self) -> &str {
match *self {
BroadcastError::SendError(_) => "could not send data on channel",
BroadcastError::RecvError => "could not receive data on channel",
}
}
fn cause(&self) -> Option<&Error> {
None
}
}
impl<T> From<SendError<T>> for BroadcastError<T> {
fn from(err: SendError<T>) -> BroadcastError<T> {
let SendError(data) = err;
BroadcastError::SendError(data)
}
}
impl<T> From<RecvError> for BroadcastError<T> {
fn from(_err: RecvError) -> BroadcastError<T> {
BroadcastError::RecvError
}
}
pub struct Broadcast<T> {
inner: Arc<Inner<T>>,
}
impl<T> Broadcast<T> {
pub fn new() -> Broadcast<T> {
let inner = Arc::new(Inner { senders: Mutex::new(Vec::new()) });
Broadcast { inner: inner }
}
pub fn consume(&self) -> Consumer<T> {
let (b, c) = channel();
self.inner.add_sender(b);
Consumer { inner: self.inner.clone(), receiver: c }
}
}
impl<T: Clone> Broadcast<T> {
pub fn send(&self, data: T) -> Result<(), BroadcastError<T>> {
let guard = self.inner.read_senders();
for s in guard.iter() {
try!(s.send(data.clone()));
}
Ok(())
}
}
struct Inner<T> {
senders: Mutex<Vec<Sender<T>>>,
}
impl<T> Inner<T> {
fn read_senders<'a>(&'a self) -> MutexGuard<'a, Vec<Sender<T>>> {
self.senders.lock().unwrap()
}
fn add_sender(&self, sender: Sender<T>) {
let mut vec = self.senders.lock().unwrap();
vec.push(sender);
}
}
pub struct Consumer<T> {
inner: Arc<Inner<T>>,
receiver: Receiver<T>,
}
impl<T> Consumer<T> {
pub fn recv(&self) -> Result<T, BroadcastError<T>> {
let data = try!(self.receiver.recv());
Ok(data)
}
}
impl<T> Clone for Consumer<T> {
fn clone(&self) -> Self {
let (s, r) = channel();
self.inner.add_sender(s);
Consumer {
inner: self.inner.clone(),
receiver: r,
}
}
}
pub fn broadcast_channel<T: Clone>() -> (Broadcast<T>, Consumer<T>) {
let broadcast = Broadcast::new();
let consumer = broadcast.consume();
(broadcast, consumer)
}
#[cfg(test)]
mod test {
use broadcast::broadcast_channel;
use super::Inner;
use std::sync::{Arc, Mutex};
use std::sync::mpsc::{channel};
use std::thread::spawn;
#[test]
fn inner_iterator() {
let (s1, r1) = channel();
let (s2, r2) = channel();
let inner = Arc::new(Inner { senders: Mutex::new(vec!(s1, s2)) });
let guard = inner.read_senders();
for s in guard.iter() {
assert!(s.send(10u8).is_ok());
}
assert_eq!(r1.recv().unwrap(), 10u8);
assert_eq!(r2.recv().unwrap(), 10u8);
}
#[test]
fn sends_to_multiple_consumers() {
let (p, c) = broadcast_channel();
let c2 = c.clone();
let res = p.send(9u8);
assert!(res.is_ok());
let res = c.recv();
assert!(res.is_ok());
assert_eq!(res.unwrap(), 9u8);
let res = c2.recv();
assert!(res.is_ok());
assert_eq!(res.unwrap(), 9u8)
}
#[test]
fn test_send_threads() {
let (p, c1) = broadcast_channel();
let c2 = c1.clone();
let (s1, r1) = channel();
let (s2, r2) = channel();
let _thread = spawn(move || {
assert_eq!(c1.recv().unwrap(), 9u8);
s1.send(10u8).unwrap();
});
let _thread = spawn(move || {
assert_eq!(c2.recv().unwrap(), 9u8);
s2.send(10u8).unwrap();
});
assert!(p.send(9u8).is_ok());
assert!(r1.recv().is_ok());
assert!(r2.recv().is_ok());
}
}