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.
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.
15 #[allow(missing_doc)];
20 pub use rt::comm::SendDeferred;
21 use rtcomm = rt::comm;
23 /// A trait for things that can send multiple messages.
24 pub trait GenericChan<T> {
29 /// Things that can send multiple messages and can detect when the receiver
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;
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.
41 /** Receives a message, or returns `none` if
42 the connection is closed or closes.
44 fn try_recv(&self) -> Option<T>;
47 /// Ports that can `peek`
48 pub trait Peekable<T> {
49 /// Returns true if a message is available
50 fn peek(&self) -> bool;
53 pub struct PortOne<T> { x: rtcomm::PortOne<T> }
54 pub struct ChanOne<T> { x: rtcomm::ChanOne<T> }
56 pub fn oneshot<T: Send>() -> (PortOne<T>, ChanOne<T>) {
57 let (p, c) = rtcomm::oneshot();
58 (PortOne { x: p }, ChanOne { x: c })
61 pub struct Port<T> { x: rtcomm::Port<T> }
62 pub struct Chan<T> { x: rtcomm::Chan<T> }
64 pub fn stream<T: Send>() -> (Port<T>, Chan<T>) {
65 let (p, c) = rtcomm::stream();
66 (Port { x: p }, Chan { x: c })
69 impl<T: Send> ChanOne<T> {
70 pub fn send(self, val: T) {
71 let ChanOne { x: c } = self;
75 pub fn try_send(self, val: T) -> bool {
76 let ChanOne { x: c } = self;
80 pub fn send_deferred(self, val: T) {
81 let ChanOne { x: c } = self;
85 pub fn try_send_deferred(self, val: T) -> bool {
86 let ChanOne{ x: c } = self;
87 c.try_send_deferred(val)
91 impl<T: Send> PortOne<T> {
92 pub fn recv(self) -> T {
93 let PortOne { x: p } = self;
97 pub fn try_recv(self) -> Option<T> {
98 let PortOne { x: p } = self;
103 impl<T: Send> Peekable<T> for PortOne<T> {
104 fn peek(&self) -> bool {
105 let &PortOne { x: ref p } = self;
110 impl<T: Send> GenericChan<T> for Chan<T> {
111 fn send(&self, val: T) {
112 let &Chan { x: ref c } = self;
117 impl<T: Send> GenericSmartChan<T> for Chan<T> {
118 fn try_send(&self, val: T) -> bool {
119 let &Chan { x: ref c } = self;
124 impl<T: Send> SendDeferred<T> for Chan<T> {
125 fn send_deferred(&self, val: T) {
126 let &Chan { x: ref c } = self;
130 fn try_send_deferred(&self, val: T) -> bool {
131 let &Chan { x: ref c } = self;
132 c.try_send_deferred(val)
136 impl<T: Send> GenericPort<T> for Port<T> {
137 fn recv(&self) -> T {
138 let &Port { x: ref p } = self;
142 fn try_recv(&self) -> Option<T> {
143 let &Port { x: ref p } = self;
148 impl<T: Send> Peekable<T> for Port<T> {
149 fn peek(&self) -> bool {
150 let &Port { x: ref p } = self;
156 pub struct SharedChan<T> { x: rtcomm::SharedChan<T> }
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) }
165 impl<T: Send> GenericChan<T> for SharedChan<T> {
166 fn send(&self, val: T) {
167 let &SharedChan { x: ref c } = self;
172 impl<T: Send> GenericSmartChan<T> for SharedChan<T> {
173 fn try_send(&self, val: T) -> bool {
174 let &SharedChan { x: ref c } = self;
179 impl<T: Send> SendDeferred<T> for SharedChan<T> {
180 fn send_deferred(&self, val: T) {
181 let &SharedChan { x: ref c } = self;
185 fn try_send_deferred(&self, val: T) -> bool {
186 let &SharedChan { x: ref c } = self;
187 c.try_send_deferred(val)
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() }
198 pub struct SharedPort<T> { x: rtcomm::SharedPort<T> }
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) }
207 impl<T: Send> GenericPort<T> for SharedPort<T> {
208 fn recv(&self) -> T {
209 let &SharedPort { x: ref p } = self;
213 fn try_recv(&self) -> Option<T> {
214 let &SharedPort { x: ref p } = self;
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() }