Skip to content

Improve documentation on duplex.rs #15182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all 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
76 changes: 73 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,76 @@ 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> {
/// Sends a value along this duplex to be received by the corresponding
/// receiver.
///
/// Rust duplexes are infinitely buffered so this method will never block.
///
/// # Failure
///
/// This function will fail if the other end of the duplex has hung up.
/// This means that if the corresponding receiver has fallen out of scope,
/// this function will trigger a fail message saying that a message is being
/// sent on a closed duplex.
///
/// Note that if this function does not fail, it does not mean that the data
/// will be successfully received. All sends are placed into a queue, so it
/// is possible for a send to succeed (the other end is alive), but then the
/// other end could immediately disconnect.
///
/// The purpose of this functionality is to propagate failure among tasks.
/// If failure is not desired, then consider using the send_opt method.
pub fn send(&self, x: S) {
self.tx.send(x)
}

/// Optionally send data to the channel.
Copy link
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
Member

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
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());
/// right.send(123);
/// assert!(right.recv() == "ABC".to_string());
/// drop(right);
/// assert!(left.recv() == 123);
/// assert_eq!(left.send_opt("ABC".to_string()), Err("ABC".to_string()));
/// ```
Copy link
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.
///
/// # Example
/// ```
/// use std::comm;
///
/// let (left, right) = comm::duplex();
/// let a = "ABC".to_string();
/// let b:u32 = 123;
///
/// left.send(a.clone());
/// assert_eq!(right.recv(), a);
/// right.send(b);
/// assert_eq!(left.recv(), b);
/// // Here the channel is empty so it return an error.
/// assert_eq!(left.try_recv(), Err(comm::Empty));
/// ```
}
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