Skip to content

Commit cf7e93f

Browse files
committed
auto merge of #9198 : FlaPer87/rust/shared-port, r=cmr
SharedPort implementation was missing in std::comm. Since this module also wraps SharedChan, it makes sense to have SharedPort defined there as well.
2 parents 5c4f65e + 9357f8f commit cf7e93f

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

src/libstd/comm.rs

+38-9
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,6 @@ pub fn stream<T: Send>() -> (Port<T>, Chan<T>) {
6666
(Port { x: p }, Chan { x: c })
6767
}
6868

69-
pub struct SharedChan<T> { x: rtcomm::SharedChan<T> }
70-
71-
impl<T: Send> SharedChan<T> {
72-
pub fn new(c: Chan<T>) -> SharedChan<T> {
73-
let Chan { x: c } = c;
74-
SharedChan { x: rtcomm::SharedChan::new(c) }
75-
}
76-
}
77-
7869
impl<T: Send> ChanOne<T> {
7970
pub fn send(self, val: T) {
8071
let ChanOne { x: c } = self;
@@ -161,6 +152,16 @@ impl<T: Send> Peekable<T> for Port<T> {
161152
}
162153
}
163154

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+
164165
impl<T: Send> GenericChan<T> for SharedChan<T> {
165166
fn send(&self, val: T) {
166167
let &SharedChan { x: ref c } = self;
@@ -193,3 +194,31 @@ impl<T> Clone for SharedChan<T> {
193194
SharedChan { x: c.clone() }
194195
}
195196
}
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+
}

0 commit comments

Comments
 (0)