2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2012-08-14 16:17:27 -05:00
|
|
|
/*!
|
2013-05-17 12:45:09 -05:00
|
|
|
Message passing
|
|
|
|
*/
|
2012-08-14 16:17:27 -05:00
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
#[allow(missing_doc)];
|
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
use clone::Clone;
|
2013-06-05 19:56:24 -05:00
|
|
|
use kinds::Send;
|
2013-08-01 01:12:20 -05:00
|
|
|
use option::Option;
|
2013-08-01 20:57:15 -05:00
|
|
|
pub use rt::comm::SendDeferred;
|
2013-05-17 12:45:09 -05:00
|
|
|
use rtcomm = rt::comm;
|
2012-08-14 16:17:27 -05:00
|
|
|
|
2013-05-17 12:45:09 -05:00
|
|
|
/// 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;
|
|
|
|
}
|
2012-08-14 16:17:27 -05:00
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
pub struct PortOne<T> { x: rtcomm::PortOne<T> }
|
|
|
|
pub struct ChanOne<T> { x: rtcomm::ChanOne<T> }
|
2013-05-17 12:45:09 -05:00
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
pub fn oneshot<T: Send>() -> (PortOne<T>, ChanOne<T>) {
|
|
|
|
let (p, c) = rtcomm::oneshot();
|
|
|
|
(PortOne { x: p }, ChanOne { x: c })
|
2013-08-01 20:57:15 -05:00
|
|
|
}
|
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
pub struct Port<T> { x: rtcomm::Port<T> }
|
|
|
|
pub struct Chan<T> { x: rtcomm::Chan<T> }
|
2013-05-17 12:45:09 -05:00
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
pub fn stream<T: Send>() -> (Port<T>, Chan<T>) {
|
|
|
|
let (p, c) = rtcomm::stream();
|
|
|
|
(Port { x: p }, Chan { x: c })
|
2013-05-17 12:45:09 -05:00
|
|
|
}
|
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
pub struct SharedChan<T> { x: rtcomm::SharedChan<T> }
|
2013-05-17 12:45:09 -05:00
|
|
|
|
2013-06-05 19:56:24 -05:00
|
|
|
impl<T: Send> SharedChan<T> {
|
2013-05-17 12:45:09 -05:00
|
|
|
pub fn new(c: Chan<T>) -> SharedChan<T> {
|
2013-08-01 01:12:20 -05:00
|
|
|
let Chan { x: c } = c;
|
|
|
|
SharedChan { x: rtcomm::SharedChan::new(c) }
|
2013-05-17 12:45:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
impl<T: Send> ChanOne<T> {
|
|
|
|
pub fn send(self, val: T) {
|
|
|
|
let ChanOne { x: c } = self;
|
|
|
|
c.send(val)
|
2012-08-14 16:17:27 -05:00
|
|
|
}
|
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
pub fn try_send(self, val: T) -> bool {
|
|
|
|
let ChanOne { x: c } = self;
|
|
|
|
c.try_send(val)
|
2013-05-17 12:45:09 -05:00
|
|
|
}
|
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
pub fn send_deferred(self, val: T) {
|
|
|
|
let ChanOne { x: c } = self;
|
|
|
|
c.send_deferred(val)
|
2012-08-14 16:17:27 -05:00
|
|
|
}
|
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
pub fn try_send_deferred(self, val: T) -> bool {
|
|
|
|
let ChanOne{ x: c } = self;
|
|
|
|
c.try_send_deferred(val)
|
|
|
|
}
|
2013-05-17 12:45:09 -05:00
|
|
|
}
|
|
|
|
|
2013-06-05 19:56:24 -05:00
|
|
|
impl<T: Send> PortOne<T> {
|
2013-05-17 12:45:09 -05:00
|
|
|
pub fn recv(self) -> T {
|
2013-08-01 01:12:20 -05:00
|
|
|
let PortOne { x: p } = self;
|
|
|
|
p.recv()
|
2012-08-14 16:17:27 -05:00
|
|
|
}
|
|
|
|
|
2013-05-17 12:45:09 -05:00
|
|
|
pub fn try_recv(self) -> Option<T> {
|
2013-08-01 01:12:20 -05:00
|
|
|
let PortOne { x: p } = self;
|
|
|
|
p.try_recv()
|
2012-08-14 16:17:27 -05:00
|
|
|
}
|
2012-11-25 16:09:53 -06:00
|
|
|
}
|
2012-08-14 16:17:27 -05:00
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
impl<T: Send> Peekable<T> for PortOne<T> {
|
|
|
|
fn peek(&self) -> bool {
|
|
|
|
let &PortOne { x: ref p } = self;
|
|
|
|
p.peek()
|
2013-08-01 20:57:15 -05:00
|
|
|
}
|
2013-05-17 12:45:09 -05:00
|
|
|
}
|
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
impl<T: Send> GenericChan<T> for Chan<T> {
|
|
|
|
fn send(&self, val: T) {
|
|
|
|
let &Chan { x: ref c } = self;
|
|
|
|
c.send(val)
|
2013-05-17 12:45:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
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)
|
2013-05-17 12:45:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
impl<T: Send> SendDeferred<T> for Chan<T> {
|
|
|
|
fn send_deferred(&self, val: T) {
|
|
|
|
let &Chan { x: ref c } = self;
|
|
|
|
c.send_deferred(val)
|
2013-05-17 12:45:09 -05:00
|
|
|
}
|
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
fn try_send_deferred(&self, val: T) -> bool {
|
|
|
|
let &Chan { x: ref c } = self;
|
|
|
|
c.try_send_deferred(val)
|
2013-05-17 12:45:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
impl<T: Send> GenericPort<T> for Port<T> {
|
|
|
|
fn recv(&self) -> T {
|
|
|
|
let &Port { x: ref p } = self;
|
|
|
|
p.recv()
|
2013-05-17 12:45:09 -05:00
|
|
|
}
|
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
fn try_recv(&self) -> Option<T> {
|
|
|
|
let &Port { x: ref p } = self;
|
|
|
|
p.try_recv()
|
2013-05-17 12:45:09 -05:00
|
|
|
}
|
2013-08-01 01:12:20 -05:00
|
|
|
}
|
2013-05-17 12:45:09 -05:00
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
impl<T: Send> Peekable<T> for Port<T> {
|
|
|
|
fn peek(&self) -> bool {
|
|
|
|
let &Port { x: ref p } = self;
|
|
|
|
p.peek()
|
2013-05-17 12:45:09 -05:00
|
|
|
}
|
2013-08-01 01:12:20 -05:00
|
|
|
}
|
2013-05-17 12:45:09 -05:00
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
impl<T: Send> GenericChan<T> for SharedChan<T> {
|
|
|
|
fn send(&self, val: T) {
|
|
|
|
let &SharedChan { x: ref c } = self;
|
|
|
|
c.send(val)
|
2013-05-17 12:45:09 -05:00
|
|
|
}
|
2013-08-01 01:12:20 -05:00
|
|
|
}
|
2013-05-17 12:45:09 -05:00
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
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)
|
2013-05-17 12:45:09 -05:00
|
|
|
}
|
2013-08-01 01:12:20 -05:00
|
|
|
}
|
2013-05-17 12:45:09 -05:00
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
impl<T: Send> SendDeferred<T> for SharedChan<T> {
|
|
|
|
fn send_deferred(&self, val: T) {
|
|
|
|
let &SharedChan { x: ref c } = self;
|
|
|
|
c.send_deferred(val)
|
2013-05-17 12:45:09 -05:00
|
|
|
}
|
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
fn try_send_deferred(&self, val: T) -> bool {
|
|
|
|
let &SharedChan { x: ref c } = self;
|
|
|
|
c.try_send_deferred(val)
|
2013-05-17 12:45:09 -05:00
|
|
|
}
|
2012-08-14 16:17:27 -05:00
|
|
|
}
|
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
impl<T> Clone for SharedChan<T> {
|
|
|
|
fn clone(&self) -> SharedChan<T> {
|
|
|
|
let &SharedChan { x: ref c } = self;
|
|
|
|
SharedChan { x: c.clone() }
|
2012-08-14 16:17:27 -05:00
|
|
|
}
|
|
|
|
}
|