-
Notifications
You must be signed in to change notification settings - Fork 12.1k
/
comm.rs
224 lines (185 loc) · 5.26 KB
/
comm.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
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/*!
Message passing
*/
#[allow(missing_doc)];
use clone::Clone;
use kinds::Send;
use option::Option;
pub use rt::comm::SendDeferred;
use rtcomm = rt::comm;
/// A trait for things that can send multiple messages.
pub trait GenericChan<T> {
/// Sends a message.
fn send(&self, x: T);
}
/// Things that can send multiple messages and can detect when the receiver
/// is closed
pub trait GenericSmartChan<T> {
/// Sends a message, or report if the receiver has closed the connection.
fn try_send(&self, x: T) -> bool;
}
/// A trait for things that can receive multiple messages.
pub trait GenericPort<T> {
/// Receives a message, or fails if the connection closes.
fn recv(&self) -> T;
/** Receives a message, or returns `none` if
the connection is closed or closes.
*/
fn try_recv(&self) -> Option<T>;
}
/// Ports that can `peek`
pub trait Peekable<T> {
/// Returns true if a message is available
fn peek(&self) -> bool;
}
pub struct PortOne<T> { priv x: rtcomm::PortOne<T> }
pub struct ChanOne<T> { priv x: rtcomm::ChanOne<T> }
pub fn oneshot<T: Send>() -> (PortOne<T>, ChanOne<T>) {
let (p, c) = rtcomm::oneshot();
(PortOne { x: p }, ChanOne { x: c })
}
pub struct Port<T> { priv x: rtcomm::Port<T> }
pub struct Chan<T> { priv x: rtcomm::Chan<T> }
pub fn stream<T: Send>() -> (Port<T>, Chan<T>) {
let (p, c) = rtcomm::stream();
(Port { x: p }, Chan { x: c })
}
impl<T: Send> ChanOne<T> {
pub fn send(self, val: T) {
let ChanOne { x: c } = self;
c.send(val)
}
pub fn try_send(self, val: T) -> bool {
let ChanOne { x: c } = self;
c.try_send(val)
}
pub fn send_deferred(self, val: T) {
let ChanOne { x: c } = self;
c.send_deferred(val)
}
pub fn try_send_deferred(self, val: T) -> bool {
let ChanOne{ x: c } = self;
c.try_send_deferred(val)
}
}
impl<T: Send> PortOne<T> {
pub fn recv(self) -> T {
let PortOne { x: p } = self;
p.recv()
}
pub fn try_recv(self) -> Option<T> {
let PortOne { x: p } = self;
p.try_recv()
}
}
impl<T: Send> Peekable<T> for PortOne<T> {
fn peek(&self) -> bool {
let &PortOne { x: ref p } = self;
p.peek()
}
}
impl<T: Send> GenericChan<T> for Chan<T> {
fn send(&self, val: T) {
let &Chan { x: ref c } = self;
c.send(val)
}
}
impl<T: Send> GenericSmartChan<T> for Chan<T> {
fn try_send(&self, val: T) -> bool {
let &Chan { x: ref c } = self;
c.try_send(val)
}
}
impl<T: Send> SendDeferred<T> for Chan<T> {
fn send_deferred(&self, val: T) {
let &Chan { x: ref c } = self;
c.send_deferred(val)
}
fn try_send_deferred(&self, val: T) -> bool {
let &Chan { x: ref c } = self;
c.try_send_deferred(val)
}
}
impl<T: Send> GenericPort<T> for Port<T> {
fn recv(&self) -> T {
let &Port { x: ref p } = self;
p.recv()
}
fn try_recv(&self) -> Option<T> {
let &Port { x: ref p } = self;
p.try_recv()
}
}
impl<T: Send> Peekable<T> for Port<T> {
fn peek(&self) -> bool {
let &Port { x: ref p } = self;
p.peek()
}
}
pub struct SharedChan<T> { priv x: rtcomm::SharedChan<T> }
impl<T: Send> SharedChan<T> {
pub fn new(c: Chan<T>) -> SharedChan<T> {
let Chan { x: c } = c;
SharedChan { x: rtcomm::SharedChan::new(c) }
}
}
impl<T: Send> GenericChan<T> for SharedChan<T> {
fn send(&self, val: T) {
let &SharedChan { x: ref c } = self;
c.send(val)
}
}
impl<T: Send> GenericSmartChan<T> for SharedChan<T> {
fn try_send(&self, val: T) -> bool {
let &SharedChan { x: ref c } = self;
c.try_send(val)
}
}
impl<T: Send> SendDeferred<T> for SharedChan<T> {
fn send_deferred(&self, val: T) {
let &SharedChan { x: ref c } = self;
c.send_deferred(val)
}
fn try_send_deferred(&self, val: T) -> bool {
let &SharedChan { x: ref c } = self;
c.try_send_deferred(val)
}
}
impl<T> Clone for SharedChan<T> {
fn clone(&self) -> SharedChan<T> {
let &SharedChan { x: ref c } = self;
SharedChan { x: c.clone() }
}
}
pub struct SharedPort<T> { priv x: rtcomm::SharedPort<T> }
impl<T: Send> SharedPort<T> {
pub fn new(p: Port<T>) -> SharedPort<T> {
let Port { x: p } = p;
SharedPort { x: rtcomm::SharedPort::new(p) }
}
}
impl<T: Send> GenericPort<T> for SharedPort<T> {
fn recv(&self) -> T {
let &SharedPort { x: ref p } = self;
p.recv()
}
fn try_recv(&self) -> Option<T> {
let &SharedPort { x: ref p } = self;
p.try_recv()
}
}
impl<T> Clone for SharedPort<T> {
fn clone(&self) -> SharedPort<T> {
let &SharedPort { x: ref p } = self;
SharedPort { x: p.clone() }
}
}