* channel() - #[unstable]. This will likely remain forever * sync_channel(n: int) - #[unstable with comment]. Concerns have ben raised about the usage of the term "synchronous channel" because that generally only applies to the case where n == 0. If n > 0 then these channels are often referred to as buffered channels. * Sender::send(), SyncSender::send(), Receiver::recv() - #[experimental]. These functions directly violate the general guideline of not providing a failing and non-failing variant. These functions were explicitly selected for being excused from this guideline, but recent discussions have cast doubt on that decision. These functions are #[experimental] for now until a decision is made as they are candidates for removal. * Sender::send_opt(), SyncSender::send_opt(), Receiver::recv_opt() - #[unstable with a comment]. If the above no-`_opt` functions are removed, these functions will be renamed to the non-`_opt` variants. * SyncSender::try_send(), Receiver::try_recv() - #[unstable with a comment]. These return types of these functions to not follow general conventions. They are consistent with the rest of the api, but not with the rest of the libraries. Until their return types are nailed down, these functions are #[unstable]. * Receiver::iter() - #[unstable]. This will likely remain forever. * std::com::select - #[experimental]. The functionality is likely to remain in some form forever, but it is highly unlikely to remain in its current form. It is unknown how much breakage this will cause if and when the api is redesigned, so the entire module and its components are all experimental. * DuplexStream - #[deprecated]. This type is not composable with other channels in terms of selection or other expected locations. It can also not be used with ChanWriter and ChanReader, for example. Due to it being only lightly used, and easily replaced with two channels, this type is being deprecated and slated for removal. * Clone for {,Sync}Sender - #[unstable]. This will likely remain forever.
77 lines
2.0 KiB
Rust
77 lines
2.0 KiB
Rust
// Copyright 2012-2013 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.
|
|
|
|
/*!
|
|
|
|
Higher level communication abstractions.
|
|
|
|
*/
|
|
|
|
#![allow(missing_doc)]
|
|
#![deprecated = "This type is replaced by having a pair of channels. This type \
|
|
is not fully composable with other channels in terms of \
|
|
or possible semantics on a duplex stream. It will be removed \
|
|
soon"]
|
|
|
|
use core::prelude::*;
|
|
|
|
use comm;
|
|
use comm::{Sender, Receiver, channel};
|
|
|
|
/// An extension of `pipes::stream` that allows both sending and receiving.
|
|
pub struct DuplexStream<S, R> {
|
|
tx: Sender<S>,
|
|
rx: Receiver<R>,
|
|
}
|
|
|
|
/// Creates a bidirectional stream.
|
|
pub fn duplex<S: Send, R: Send>() -> (DuplexStream<S, R>, DuplexStream<R, S>) {
|
|
let (tx1, rx1) = channel();
|
|
let (tx2, rx2) = channel();
|
|
(DuplexStream { tx: tx1, rx: rx2 },
|
|
DuplexStream { tx: tx2, rx: rx1 })
|
|
}
|
|
|
|
// Allow these methods to be used without import:
|
|
impl<S:Send,R:Send> DuplexStream<S, R> {
|
|
pub fn send(&self, x: S) {
|
|
self.tx.send(x)
|
|
}
|
|
pub fn send_opt(&self, x: S) -> Result<(), S> {
|
|
self.tx.send_opt(x)
|
|
}
|
|
pub fn recv(&self) -> R {
|
|
self.rx.recv()
|
|
}
|
|
pub fn try_recv(&self) -> Result<R, comm::TryRecvError> {
|
|
self.rx.try_recv()
|
|
}
|
|
pub fn recv_opt(&self) -> Result<R, ()> {
|
|
self.rx.recv_opt()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use std::prelude::*;
|
|
use comm::{duplex};
|
|
|
|
#[test]
|
|
pub fn duplex_stream_1() {
|
|
let (left, right) = duplex();
|
|
|
|
left.send("abc".to_string());
|
|
right.send(123i);
|
|
|
|
assert!(left.recv() == 123);
|
|
assert!(right.recv() == "abc".to_string());
|
|
}
|
|
}
|