Skip to content
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions src/libsync/comm/duplex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,32 @@ Higher level communication abstractions.

*/

#![allow(missing_doc)]

use core::prelude::*;

use comm;
use comm::{Sender, Receiver, channel};

/// An extension of `pipes::stream` that allows both sending and receiving.
/// An extension of `pipes::stream` that allows both sending and receiving
/// data over two channels
pub struct DuplexStream<S, R> {
tx: Sender<S>,
rx: Receiver<R>,
}

/// Creates a bidirectional stream.
///
/// # Example
/// ```
/// use std::comm;
///
/// let (left, right) = comm::duplex();
///
/// left.send(("ABC").to_string());
/// right.send(123);
///
/// assert!(left.recv() == 123);
/// assert!(right.recv() == "ABC".to_string());
/// ```
pub fn duplex<S: Send, R: Send>() -> (DuplexStream<S, R>, DuplexStream<R, S>) {
let (tx1, rx1) = channel();
let (tx2, rx2) = channel();
Expand All @@ -37,18 +49,44 @@ pub fn duplex<S: Send, R: Send>() -> (DuplexStream<S, R>, DuplexStream<R, S>) {

// Allow these methods to be used without import:
impl<S:Send,R:Send> DuplexStream<S, R> {
/// Send data over the channel.
pub fn send(&self, x: S) {
self.tx.send(x)
}

/// Optionally send data to the channel.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I were unsure what send_opt did, this documentation doesn't really help me much because it just takes the 8-letter method name and expands it to a bit of english.

If we're adding documentation to these methods, then this should document the semantics of why it exists, what's optional about it, etc. This also applies to the documentation below as well.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Examples (preferably both the Ok and Err cases) would help.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an example and will try to adapt documentation from both Sender and Receiver for DuplexStream.
I am totally agree with you that just putting a sentence is not meaningful. :)

///
/// # Example
/// ```
/// use std::comm;
/// let (left, right) = comm::duplex();
/// left.send("ABC".to_string());
/// spawn(proc() {
/// right.send(123);
/// assert!(right.recv() == "ABC".to_string());
/// drop(right);
/// });
/// assert!(left.recv() == 123);
/// // I force this task to wait because the other have to drop.
/// std::io::timer::sleep(1000);
/// assert_eq!(left.send_opt("ABC".to_string()),
/// Err("ABC".to_string()));
/// ```

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is definitely helpful, but I think it still needs to be accompanied with words explaining the semantics of this function. The nonstandard functions below should also receive the same treatment.

Additionally, relying on sleep for correctness is likely to go wrong quickly. You can probably do this in a single-threaded situation without extra tasks.

pub fn send_opt(&self, x: S) -> Result<(), S> {
self.tx.send_opt(x)
}

/// Receive data from the channel.
pub fn recv(&self) -> R {
self.rx.recv()
}

/// Try to receive data from the channel.
pub fn try_recv(&self) -> Result<R, comm::TryRecvError> {
self.rx.try_recv()
}

/// Optionally receive data from the channel.
pub fn recv_opt(&self) -> Result<R, ()> {
self.rx.recv_opt()
}
Expand Down