(index<- ) ./libstd/comm.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 /*!
12 Message passing
13 */
14
15 #[allow(missing_doc)];
16
17 use clone::Clone;
18 use kinds::Send;
19 use option::Option;
20 pub use rt::comm::SendDeferred;
21 use rtcomm = rt::comm;
22
23 /// A trait for things that can send multiple messages.
24 pub trait GenericChan<T> {
25 /// Sends a message.
26 fn send(&self, x: T);
27 }
28
29 /// Things that can send multiple messages and can detect when the receiver
30 /// is closed
31 pub trait GenericSmartChan<T> {
32 /// Sends a message, or report if the receiver has closed the connection.
33 fn try_send(&self, x: T) -> bool;
34 }
35
36 /// A trait for things that can receive multiple messages.
37 pub trait GenericPort<T> {
38 /// Receives a message, or fails if the connection closes.
39 fn recv(&self) -> T;
40
41 /** Receives a message, or returns `none` if
42 the connection is closed or closes.
43 */
44 fn try_recv(&self) -> Option<T>;
45 }
46
47 /// Ports that can `peek`
48 pub trait Peekable<T> {
49 /// Returns true if a message is available
50 fn peek(&self) -> bool;
51 }
52
53 pub struct PortOne<T> { x: rtcomm::PortOne<T> }
54 pub struct ChanOne<T> { x: rtcomm::ChanOne<T> }
55
56 pub fn oneshot<T: Send>() -> (PortOne<T>, ChanOne<T>) {
57 let (p, c) = rtcomm::oneshot();
58 (PortOne { x: p }, ChanOne { x: c })
59 }
60
61 pub struct Port<T> { x: rtcomm::Port<T> }
62 pub struct Chan<T> { x: rtcomm::Chan<T> }
63
64 pub fn stream<T: Send>() -> (Port<T>, Chan<T>) {
65 let (p, c) = rtcomm::stream();
66 (Port { x: p }, Chan { x: c })
67 }
68
69 impl<T: Send> ChanOne<T> {
70 pub fn send(self, val: T) {
71 let ChanOne { x: c } = self;
72 c.send(val)
73 }
74
75 pub fn try_send(self, val: T) -> bool {
76 let ChanOne { x: c } = self;
77 c.try_send(val)
78 }
79
80 pub fn send_deferred(self, val: T) {
81 let ChanOne { x: c } = self;
82 c.send_deferred(val)
83 }
84
85 pub fn try_send_deferred(self, val: T) -> bool {
86 let ChanOne{ x: c } = self;
87 c.try_send_deferred(val)
88 }
89 }
90
91 impl<T: Send> PortOne<T> {
92 pub fn recv(self) -> T {
93 let PortOne { x: p } = self;
94 p.recv()
95 }
96
97 pub fn try_recv(self) -> Option<T> {
98 let PortOne { x: p } = self;
99 p.try_recv()
100 }
101 }
102
103 impl<T: Send> Peekable<T> for PortOne<T> {
104 fn peek(&self) -> bool {
105 let &PortOne { x: ref p } = self;
106 p.peek()
107 }
108 }
109
110 impl<T: Send> GenericChan<T> for Chan<T> {
111 fn send(&self, val: T) {
112 let &Chan { x: ref c } = self;
113 c.send(val)
114 }
115 }
116
117 impl<T: Send> GenericSmartChan<T> for Chan<T> {
118 fn try_send(&self, val: T) -> bool {
119 let &Chan { x: ref c } = self;
120 c.try_send(val)
121 }
122 }
123
124 impl<T: Send> SendDeferred<T> for Chan<T> {
125 fn send_deferred(&self, val: T) {
126 let &Chan { x: ref c } = self;
127 c.send_deferred(val)
128 }
129
130 fn try_send_deferred(&self, val: T) -> bool {
131 let &Chan { x: ref c } = self;
132 c.try_send_deferred(val)
133 }
134 }
135
136 impl<T: Send> GenericPort<T> for Port<T> {
137 fn recv(&self) -> T {
138 let &Port { x: ref p } = self;
139 p.recv()
140 }
141
142 fn try_recv(&self) -> Option<T> {
143 let &Port { x: ref p } = self;
144 p.try_recv()
145 }
146 }
147
148 impl<T: Send> Peekable<T> for Port<T> {
149 fn peek(&self) -> bool {
150 let &Port { x: ref p } = self;
151 p.peek()
152 }
153 }
154
155
156 pub struct SharedChan<T> { x: rtcomm::SharedChan<T> }
157
158 impl<T: Send> SharedChan<T> {
159 pub fn new(c: Chan<T>) -> SharedChan<T> {
160 let Chan { x: c } = c;
161 SharedChan { x: rtcomm::SharedChan::new(c) }
162 }
163 }
164
165 impl<T: Send> GenericChan<T> for SharedChan<T> {
166 fn send(&self, val: T) {
167 let &SharedChan { x: ref c } = self;
168 c.send(val)
169 }
170 }
171
172 impl<T: Send> GenericSmartChan<T> for SharedChan<T> {
173 fn try_send(&self, val: T) -> bool {
174 let &SharedChan { x: ref c } = self;
175 c.try_send(val)
176 }
177 }
178
179 impl<T: Send> SendDeferred<T> for SharedChan<T> {
180 fn send_deferred(&self, val: T) {
181 let &SharedChan { x: ref c } = self;
182 c.send_deferred(val)
183 }
184
185 fn try_send_deferred(&self, val: T) -> bool {
186 let &SharedChan { x: ref c } = self;
187 c.try_send_deferred(val)
188 }
189 }
190
191 impl<T> Clone for SharedChan<T> {
192 fn clone(&self) -> SharedChan<T> {
193 let &SharedChan { x: ref c } = self;
194 SharedChan { x: c.clone() }
195 }
196 }
197
198 pub struct SharedPort<T> { x: rtcomm::SharedPort<T> }
199
200 impl<T: Send> SharedPort<T> {
201 pub fn new(p: Port<T>) -> SharedPort<T> {
202 let Port { x: p } = p;
203 SharedPort { x: rtcomm::SharedPort::new(p) }
204 }
205 }
206
207 impl<T: Send> GenericPort<T> for SharedPort<T> {
208 fn recv(&self) -> T {
209 let &SharedPort { x: ref p } = self;
210 p.recv()
211 }
212
213 fn try_recv(&self) -> Option<T> {
214 let &SharedPort { x: ref p } = self;
215 p.try_recv()
216 }
217 }
218
219 impl<T> Clone for SharedPort<T> {
220 fn clone(&self) -> SharedPort<T> {
221 let &SharedPort { x: ref p } = self;
222 SharedPort { x: p.clone() }
223 }
224 }
libstd/comm.rs:155:1-155:1 -struct- definition:
pub struct SharedChan<T> { x: rtcomm::SharedChan<T> }
references:-179: impl<T: Send> SendDeferred<T> for SharedChan<T> {
161: SharedChan { x: rtcomm::SharedChan::new(c) }
181: let &SharedChan { x: ref c } = self;
192: fn clone(&self) -> SharedChan<T> {
193: let &SharedChan { x: ref c } = self;
174: let &SharedChan { x: ref c } = self;
159: pub fn new(c: Chan<T>) -> SharedChan<T> {
167: let &SharedChan { x: ref c } = self;
191: impl<T> Clone for SharedChan<T> {
186: let &SharedChan { x: ref c } = self;
172: impl<T: Send> GenericSmartChan<T> for SharedChan<T> {
158: impl<T: Send> SharedChan<T> {
165: impl<T: Send> GenericChan<T> for SharedChan<T> {
194: SharedChan { x: c.clone() }
libstd/comm.rs:55:1-55:1 -fn- definition:
pub fn oneshot<T: Send>() -> (PortOne<T>, ChanOne<T>) {
references:-libstd/select.rs:
57: let (p,c) = comm::oneshot();
libstd/unstable/sync.rs:
124: let (p1,c1) = comm::oneshot(); // ()
125: let (p2,c2) = comm::oneshot(); // bool
libstd/task/spawn.rs:
631: let (thread_port, thread_chan) = oneshot();
libstd/comm.rs:52:1-52:1 -struct- definition:
pub struct PortOne<T> { x: rtcomm::PortOne<T> }
references:-58: (PortOne { x: p }, ChanOne { x: c })
98: let PortOne { x: p } = self;
93: let PortOne { x: p } = self;
91: impl<T: Send> PortOne<T> {
103: impl<T: Send> Peekable<T> for PortOne<T> {
56: pub fn oneshot<T: Send>() -> (PortOne<T>, ChanOne<T>) {
105: let &PortOne { x: ref p } = self;
libstd/unstable/sync.rs:
40: unwrapper: AtomicOption<(comm::ChanOne<()>, comm::PortOne<bool>)>,
libstd/comm.rs:63:1-63:1 -fn- definition:
pub fn stream<T: Send>() -> (Port<T>, Chan<T>) {
references:-libstd/run.rs:
210: let (p, ch) = stream();
libstd/unstable/mod.rs:
44: let (port, chan) = comm::stream();
libstd/task/mod.rs:
400: let (po, ch) = stream::<T>();
289: let (notify_pipe_po, notify_pipe_ch) = stream::<TaskResult>();
libstd/comm.rs:53:48-53:48 -struct- definition:
pub struct PortOne<T> { x: rtcomm::PortOne<T> }
pub struct ChanOne<T> { x: rtcomm::ChanOne<T> }
references:-58: (PortOne { x: p }, ChanOne { x: c })
81: let ChanOne { x: c } = self;
56: pub fn oneshot<T: Send>() -> (PortOne<T>, ChanOne<T>) {
71: let ChanOne { x: c } = self;
69: impl<T: Send> ChanOne<T> {
86: let ChanOne{ x: c } = self;
76: let ChanOne { x: c } = self;
libstd/unstable/sync.rs:
40: unwrapper: AtomicOption<(comm::ChanOne<()>, comm::PortOne<bool>)>,
libstd/comm.rs:23:56-23:56 -trait- definition:
/// A trait for things that can send multiple messages.
pub trait GenericChan<T> {
references:-110: impl<T: Send> GenericChan<T> for Chan<T> {
165: impl<T: Send> GenericChan<T> for SharedChan<T> {
libstd/rt/io/comm_adapters.rs:
33: impl<C: GenericChan<~[u8]>> Writer for ChanWriter<C> {
57: impl<W: Writer> GenericChan<~[u8]> for WriterChan<W> {
29: impl<C: GenericChan<~[u8]>> ChanWriter<C> {
libstd/rt/comm.rs:
695: impl<T: Send> GenericChan<T> for MegaPipe<T> {
603: impl<T: Send> GenericChan<T> for SharedChan<T> {
472: impl<T: Send> GenericChan<T> for Chan<T> {
libstd/comm.rs:36:59-36:59 -trait- definition:
/// A trait for things that can receive multiple messages.
pub trait GenericPort<T> {
references:-136: impl<T: Send> GenericPort<T> for Port<T> {
207: impl<T: Send> GenericPort<T> for SharedPort<T> {
libstd/rt/io/comm_adapters.rs:
21: impl<P: GenericPort<~[u8]>> Reader for PortReader<P> {
17: impl<P: GenericPort<~[u8]>> PortReader<P> {
45: impl<R: Reader> GenericPort<~[u8]> for ReaderPort<R> {
libstd/rt/comm.rs:
707: impl<T: Send> GenericPort<T> for MegaPipe<T> {
493: impl<T> GenericPort<T> for Port<T> {
648: impl<T: Send> GenericPort<T> for SharedPort<T> {
libstd/comm.rs:61:42-61:42 -struct- definition:
pub struct Port<T> { x: rtcomm::Port<T> }
pub struct Chan<T> { x: rtcomm::Chan<T> }
references:-112: let &Chan { x: ref c } = self;
126: let &Chan { x: ref c } = self;
64: pub fn stream<T: Send>() -> (Port<T>, Chan<T>) {
124: impl<T: Send> SendDeferred<T> for Chan<T> {
117: impl<T: Send> GenericSmartChan<T> for Chan<T> {
66: (Port { x: p }, Chan { x: c })
160: let Chan { x: c } = c;
119: let &Chan { x: ref c } = self;
131: let &Chan { x: ref c } = self;
159: pub fn new(c: Chan<T>) -> SharedChan<T> {
110: impl<T: Send> GenericChan<T> for Chan<T> {
libstd/task/mod.rs:
151: notify_chan: Option<Chan<TaskResult>>,
libstd/task/spawn.rs:
367: notify_chan: Chan<TaskResult>,
378: fn AutoNotify(chan: Chan<TaskResult>) -> AutoNotify {
libstd/comm.rs:60:1-60:1 -struct- definition:
pub struct Port<T> { x: rtcomm::Port<T> }
references:-143: let &Port { x: ref p } = self;
148: impl<T: Send> Peekable<T> for Port<T> {
64: pub fn stream<T: Send>() -> (Port<T>, Chan<T>) {
201: pub fn new(p: Port<T>) -> SharedPort<T> {
150: let &Port { x: ref p } = self;
138: let &Port { x: ref p } = self;
66: (Port { x: p }, Chan { x: c })
136: impl<T: Send> GenericPort<T> for Port<T> {
202: let Port { x: p } = p;
libstd/task/mod.rs:
278: pub fn future_result(&mut self, blk: &fn(v: Port<TaskResult>)) {
libstd/comm.rs:30:14-30:14 -trait- definition:
/// is closed
pub trait GenericSmartChan<T> {
references:-117: impl<T: Send> GenericSmartChan<T> for Chan<T> {
172: impl<T: Send> GenericSmartChan<T> for SharedChan<T> {
libstd/rt/comm.rs:
609: impl<T: Send> GenericSmartChan<T> for SharedChan<T> {
478: impl<T: Send> GenericSmartChan<T> for Chan<T> {
701: impl<T: Send> GenericSmartChan<T> for MegaPipe<T> {
libstd/comm.rs:197:1-197:1 -struct- definition:
pub struct SharedPort<T> { x: rtcomm::SharedPort<T> }
references:-222: SharedPort { x: p.clone() }
214: let &SharedPort { x: ref p } = self;
201: pub fn new(p: Port<T>) -> SharedPort<T> {
203: SharedPort { x: rtcomm::SharedPort::new(p) }
219: impl<T> Clone for SharedPort<T> {
209: let &SharedPort { x: ref p } = self;
200: impl<T: Send> SharedPort<T> {
220: fn clone(&self) -> SharedPort<T> {
207: impl<T: Send> GenericPort<T> for SharedPort<T> {
221: let &SharedPort { x: ref p } = self;
libstd/comm.rs:47:26-47:26 -trait- definition:
/// Ports that can `peek`
pub trait Peekable<T> {
references:-103: impl<T: Send> Peekable<T> for PortOne<T> {
148: impl<T: Send> Peekable<T> for Port<T> {
libstd/rt/comm.rs:
516: impl<T> Peekable<T> for Port<T> {
361: impl<T> Peekable<T> for PortOne<T> {