File tree Expand file tree Collapse file tree 3 files changed +29
-0
lines changed Expand file tree Collapse file tree 3 files changed +29
-0
lines changed Original file line number Diff line number Diff line change 1+ //! Creates an asynchronous piped reader and writer pair using `tokio.rs`.
2+ //!
3+ //! # Examples
4+ //!
5+ //! ```
6+ //! # async fn run() {
7+ //! use async_pipe;
8+ //! use tokio::prelude::*;
9+ //!
10+ //! let (mut w, mut r) = async_pipe::pipe();
11+ //!
12+ //! tokio::spawn(async move {
13+ //! w.write_all(b"hello world").await.unwrap();
14+ //! });
15+ //!
16+ //! let mut v = Vec::new();
17+ //! r.read_to_end(&mut v).await.unwrap();
18+ //!
19+ //! println!("Received: {:?}", String::from_utf8(v));
20+ //! # }
21+ //!
22+ //! tokio::runtime::Runtime::new().unwrap().block_on(run());
23+ //! ```
24+
125use state:: State ;
226use std:: sync:: { Arc , Mutex } ;
327
@@ -8,6 +32,7 @@ mod reader;
832mod state;
933mod writer;
1034
35+ /// Creates a piped pair of an [`AsyncWrite`](https://docs.rs/tokio/0.2.16/tokio/io/trait.AsyncWrite.html) and an [`AsyncRead`](https://docs.rs/tokio/0.2.15/tokio/io/trait.AsyncRead.html).
1136pub fn pipe ( ) -> ( PipeWriter , PipeReader ) {
1237 let shared_state = Arc :: new ( Mutex :: new ( State {
1338 reader_waker : None ,
Original file line number Diff line number Diff line change @@ -5,11 +5,13 @@ use std::sync::{Arc, Mutex};
55use std:: task:: { Context , Poll } ;
66use tokio:: io:: { self , AsyncRead } ;
77
8+ /// The read half of the pipe which implements [`AsyncRead`](https://docs.rs/tokio/0.2.15/tokio/io/trait.AsyncRead.html).
89pub struct PipeReader {
910 pub ( crate ) state : Arc < Mutex < State > > ,
1011}
1112
1213impl PipeReader {
14+ /// Closes the pipe, any further read will return EOF and any further write will raise an error.
1315 pub fn close ( & self ) -> io:: Result < ( ) > {
1416 match self . state . lock ( ) {
1517 Ok ( mut state) => {
Original file line number Diff line number Diff line change @@ -5,11 +5,13 @@ use std::sync::{Arc, Mutex};
55use std:: task:: { Context , Poll } ;
66use tokio:: io:: { self , AsyncWrite } ;
77
8+ /// The write half of the pipe which implements [`AsyncWrite`](https://docs.rs/tokio/0.2.16/tokio/io/trait.AsyncWrite.html).
89pub struct PipeWriter {
910 pub ( crate ) state : Arc < Mutex < State > > ,
1011}
1112
1213impl PipeWriter {
14+ /// Closes the pipe, any further read will return EOF and any further write will raise an error.
1315 pub fn close ( & self ) -> io:: Result < ( ) > {
1416 match self . state . lock ( ) {
1517 Ok ( mut state) => {
You can’t perform that action at this time.
0 commit comments