]> git.lizzy.rs Git - rust.git/blob - src/libstd/comm.rs
Find the cratemap at runtime on windows.
[rust.git] / src / 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 }