|
1 | 1 | //! In-process memory IO types.
|
2 | 2 |
|
3 |
| -use crate::io::{AsyncRead, AsyncWrite, ReadBuf}; |
| 3 | +use crate::io::{split, AsyncRead, AsyncWrite, ReadBuf, ReadHalf, WriteHalf}; |
4 | 4 | use crate::loom::sync::Mutex;
|
5 | 5 |
|
6 | 6 | use bytes::{Buf, BytesMut};
|
@@ -47,15 +47,31 @@ 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 | +/// # Example |
| 57 | +/// |
| 58 | +/// ``` |
| 59 | +/// # async fn ex() -> std::io::Result<()> { |
| 60 | +/// # use tokio::io::{AsyncReadExt, AsyncWriteExt}; |
| 61 | +/// let (mut receiver, mut sender) = tokio::io::simplex(64); |
| 62 | +/// |
| 63 | +/// sender.write_all(b"ping").await?; |
| 64 | +/// |
| 65 | +/// let mut buf = [0u8; 4]; |
| 66 | +/// receiver.read_exact(&mut buf).await?; |
| 67 | +/// assert_eq!(&buf, b"ping"); |
| 68 | +/// |
| 69 | +/// # Ok(()) |
| 70 | +/// # } |
| 71 | +/// ``` |
57 | 72 | #[derive(Debug)]
|
58 |
| -struct Pipe { |
| 73 | +#[cfg_attr(docsrs, doc(cfg(feature = "io-util")))] |
| 74 | +pub struct SimplexStream { |
59 | 75 | /// The buffer storing the bytes written, also read from.
|
60 | 76 | ///
|
61 | 77 | /// Using a `BytesMut` because it has efficient `Buf` and `BufMut`
|
@@ -83,8 +99,10 @@ struct Pipe {
|
83 | 99 | /// written to a side before the write returns `Poll::Pending`.
|
84 | 100 | #[cfg_attr(docsrs, doc(cfg(feature = "io-util")))]
|
85 | 101 | 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))); |
| 102 | + let (reader, writer) = simplex(max_buf_size); |
| 103 | + let one = Arc::new(Mutex::new(reader.unsplit(writer))); |
| 104 | + let (reader, writer) = simplex(max_buf_size); |
| 105 | + let two = Arc::new(Mutex::new(reader.unsplit(writer))); |
88 | 106 |
|
89 | 107 | (
|
90 | 108 | DuplexStream {
|
@@ -161,19 +179,24 @@ impl Drop for DuplexStream {
|
161 | 179 | }
|
162 | 180 | }
|
163 | 181 |
|
164 |
| -// ===== impl Pipe ===== |
| 182 | +// ===== impl SimplexStream ===== |
165 | 183 |
|
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 |
| - } |
175 |
| - } |
| 184 | +/// Creates unidirectional buffer that acts like a pipe. |
| 185 | +/// |
| 186 | +/// The `max_buf_size` argument is the maximum amount of bytes that can be |
| 187 | +/// written to a buffer before the it returns `Poll::Pending`. |
| 188 | +#[cfg_attr(docsrs, doc(cfg(feature = "io-util")))] |
| 189 | +pub fn simplex(max_buf_size: usize) -> (ReadHalf<SimplexStream>, WriteHalf<SimplexStream>) { |
| 190 | + split(SimplexStream { |
| 191 | + buffer: BytesMut::new(), |
| 192 | + is_closed: false, |
| 193 | + max_buf_size, |
| 194 | + read_waker: None, |
| 195 | + write_waker: None, |
| 196 | + }) |
| 197 | +} |
176 | 198 |
|
| 199 | +impl SimplexStream { |
177 | 200 | fn close_write(&mut self) {
|
178 | 201 | self.is_closed = true;
|
179 | 202 | // needs to notify any readers that no more data will come
|
@@ -269,7 +292,7 @@ impl Pipe {
|
269 | 292 | }
|
270 | 293 | }
|
271 | 294 |
|
272 |
| -impl AsyncRead for Pipe { |
| 295 | +impl AsyncRead for SimplexStream { |
273 | 296 | cfg_coop! {
|
274 | 297 | fn poll_read(
|
275 | 298 | self: Pin<&mut Self>,
|
@@ -299,7 +322,7 @@ impl AsyncRead for Pipe {
|
299 | 322 | }
|
300 | 323 | }
|
301 | 324 |
|
302 |
| -impl AsyncWrite for Pipe { |
| 325 | +impl AsyncWrite for SimplexStream { |
303 | 326 | cfg_coop! {
|
304 | 327 | fn poll_write(
|
305 | 328 | self: Pin<&mut Self>,
|
|
0 commit comments