-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
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
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6b7fdcd
Improve documentation on duplex.rs
darnuria ecccc78
fixup! Typo.
darnuria 00a761d
Add example to DuplexeStream.send_opt.
darnuria 9f3ad8d
fixup! Adding example on try_recv and refactoring code on try_send
darnuria d6768c7
Add documentation derived from Sender.send.
darnuria File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,13 +21,27 @@ 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(); | ||
|
|
@@ -37,18 +51,27 @@ 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) | ||
| } | ||
|
|
||
| /// Send optionnaly data to the channel. | ||
| 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() | ||
| } | ||
|
|
||
| /// Receive optionnaly data from the channel. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
| pub fn recv_opt(&self) -> Result<R, ()> { | ||
| self.rx.recv_opt() | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
misspelling, and I'd say "Optionally send data to the channel."