@@ -47,15 +47,35 @@ use std::{
47
47
#[ derive( Debug ) ]
48
48
#[ cfg_attr( docsrs, doc( cfg( feature = "io-util" ) ) ) ]
49
49
pub struct DuplexStream {
50
- read : Arc < Mutex < Pipe > > ,
51
- write : Arc < Mutex < Pipe > > ,
50
+ read : Arc < Mutex < SimplexStream > > ,
51
+ write : Arc < Mutex < SimplexStream > > ,
52
52
}
53
53
54
- /// A unidirectional IO over a piece of memory.
54
+ /// A unidirectional pipe to read and write bytes in memory.
55
55
///
56
- /// Data can be written to the pipe, and reading will return that data.
56
+ /// Writing to the `SimplexStream` will allow that data to be read again, it is used as in-memory IO type.
57
+ ///
58
+ /// You can [`split`](crate::io::split()) the `SimplexStream` so you can have a separate read and write handle.
59
+ ///
60
+ /// # Example
61
+ ///
62
+ /// ```
63
+ /// # async fn ex() -> std::io::Result<()> {
64
+ /// # use tokio::io::{AsyncReadExt, AsyncWriteExt, split};
65
+ /// let (mut receiver, mut sender) = split(tokio::io::simplex(64));
66
+ ///
67
+ /// sender.write_all(b"ping").await?;
68
+ ///
69
+ /// let mut buf = [0u8; 4];
70
+ /// receiver.read_exact(&mut buf).await?;
71
+ /// assert_eq!(&buf, b"ping");
72
+ ///
73
+ /// # Ok(())
74
+ /// # }
75
+ /// ```
57
76
#[ derive( Debug ) ]
58
- struct Pipe {
77
+ #[ cfg_attr( docsrs, doc( cfg( feature = "io-util" ) ) ) ]
78
+ pub struct SimplexStream {
59
79
/// The buffer storing the bytes written, also read from.
60
80
///
61
81
/// Using a `BytesMut` because it has efficient `Buf` and `BufMut`
@@ -83,8 +103,8 @@ struct Pipe {
83
103
/// written to a side before the write returns `Poll::Pending`.
84
104
#[ cfg_attr( docsrs, doc( cfg( feature = "io-util" ) ) ) ]
85
105
pub fn duplex ( max_buf_size : usize ) -> ( DuplexStream , DuplexStream ) {
86
- let one = Arc :: new ( Mutex :: new ( Pipe :: new ( max_buf_size) ) ) ;
87
- let two = Arc :: new ( Mutex :: new ( Pipe :: new ( max_buf_size) ) ) ;
106
+ let one = Arc :: new ( Mutex :: new ( simplex ( max_buf_size) ) ) ;
107
+ let two = Arc :: new ( Mutex :: new ( simplex ( max_buf_size) ) ) ;
88
108
89
109
(
90
110
DuplexStream {
@@ -161,19 +181,24 @@ impl Drop for DuplexStream {
161
181
}
162
182
}
163
183
164
- // ===== impl Pipe =====
184
+ // ===== impl SimplexStream =====
165
185
166
- impl Pipe {
167
- fn new ( max_buf_size : usize ) -> Self {
168
- Pipe {
169
- buffer : BytesMut :: new ( ) ,
170
- is_closed : false ,
171
- max_buf_size,
172
- read_waker : None ,
173
- write_waker : None ,
174
- }
186
+ /// Creates unidirectional buffer that acts like pair of connected single dicrection sockets.
187
+ ///
188
+ /// The `max_buf_size` argument is the maximum amount of bytes that can be
189
+ /// written to a buffer before the it returns `Poll::Pending`.
190
+ #[ cfg_attr( docsrs, doc( cfg( feature = "io-util" ) ) ) ]
191
+ pub fn simplex ( max_buf_size : usize ) -> SimplexStream {
192
+ SimplexStream {
193
+ buffer : BytesMut :: new ( ) ,
194
+ is_closed : false ,
195
+ max_buf_size,
196
+ read_waker : None ,
197
+ write_waker : None ,
175
198
}
199
+ }
176
200
201
+ impl SimplexStream {
177
202
fn close_write ( & mut self ) {
178
203
self . is_closed = true ;
179
204
// needs to notify any readers that no more data will come
@@ -269,7 +294,7 @@ impl Pipe {
269
294
}
270
295
}
271
296
272
- impl AsyncRead for Pipe {
297
+ impl AsyncRead for SimplexStream {
273
298
cfg_coop ! {
274
299
fn poll_read(
275
300
self : Pin <& mut Self >,
@@ -299,7 +324,7 @@ impl AsyncRead for Pipe {
299
324
}
300
325
}
301
326
302
- impl AsyncWrite for Pipe {
327
+ impl AsyncWrite for SimplexStream {
303
328
cfg_coop ! {
304
329
fn poll_write(
305
330
self : Pin <& mut Self >,
0 commit comments