blob: f292b871f1e13191ae20de41c3a7a24df2e9b440 [file] [log] [blame]
Joel Galenson7a983022021-05-19 15:35:43 -07001use super::DEFAULT_BUF_SIZE;
Haibo Huang5bf87962021-02-09 17:18:40 -08002use futures_core::ready;
Jason Macnakc417d3b2020-04-06 10:30:28 -07003use futures_core::task::{Context, Poll};
Haibo Huang52627c82020-05-08 19:26:17 -07004use futures_io::{AsyncBufRead, AsyncRead, AsyncSeek, AsyncWrite, IoSlice, SeekFrom};
Haibo Huang5bf87962021-02-09 17:18:40 -08005use pin_project_lite::pin_project;
Jason Macnakc417d3b2020-04-06 10:30:28 -07006use std::fmt;
7use std::io::{self, Write};
8use std::pin::Pin;
Jason Macnakc417d3b2020-04-06 10:30:28 -07009
Haibo Huang5bf87962021-02-09 17:18:40 -080010pin_project! {
11 /// Wraps a writer and buffers its output.
12 ///
13 /// It can be excessively inefficient to work directly with something that
14 /// implements [`AsyncWrite`]. A `BufWriter` keeps an in-memory buffer of data and
15 /// writes it to an underlying writer in large, infrequent batches.
16 ///
17 /// `BufWriter` can improve the speed of programs that make *small* and
18 /// *repeated* write calls to the same file or network socket. It does not
19 /// help when writing very large amounts at once, or writing just one or a few
20 /// times. It also provides no advantage when writing to a destination that is
21 /// in memory, like a `Vec<u8>`.
22 ///
23 /// When the `BufWriter` is dropped, the contents of its buffer will be
24 /// discarded. Creating multiple instances of a `BufWriter` on the same
25 /// stream can cause data loss. If you need to write out the contents of its
26 /// buffer, you must manually call flush before the writer is dropped.
27 ///
28 /// [`AsyncWrite`]: futures_io::AsyncWrite
29 /// [`flush`]: super::AsyncWriteExt::flush
30 ///
31 // TODO: Examples
32 pub struct BufWriter<W> {
33 #[pin]
34 inner: W,
35 buf: Vec<u8>,
36 written: usize,
37 }
Jason Macnakc417d3b2020-04-06 10:30:28 -070038}
39
Jason Macnakc417d3b2020-04-06 10:30:28 -070040impl<W: AsyncWrite> BufWriter<W> {
41 /// Creates a new `BufWriter` with a default buffer capacity. The default is currently 8 KB,
42 /// but may change in the future.
43 pub fn new(inner: W) -> Self {
44 Self::with_capacity(DEFAULT_BUF_SIZE, inner)
45 }
46
47 /// Creates a new `BufWriter` with the specified buffer capacity.
48 pub fn with_capacity(cap: usize, inner: W) -> Self {
Joel Galenson7a983022021-05-19 15:35:43 -070049 Self { inner, buf: Vec::with_capacity(cap), written: 0 }
Jason Macnakc417d3b2020-04-06 10:30:28 -070050 }
51
52 fn flush_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Chih-Hung Hsiehd45e96e2020-10-25 23:16:22 -070053 let mut this = self.project();
Jason Macnakc417d3b2020-04-06 10:30:28 -070054
Chih-Hung Hsiehd45e96e2020-10-25 23:16:22 -070055 let len = this.buf.len();
Jason Macnakc417d3b2020-04-06 10:30:28 -070056 let mut ret = Ok(());
Chih-Hung Hsiehd45e96e2020-10-25 23:16:22 -070057 while *this.written < len {
58 match ready!(this.inner.as_mut().poll_write(cx, &this.buf[*this.written..])) {
Jason Macnakc417d3b2020-04-06 10:30:28 -070059 Ok(0) => {
60 ret = Err(io::Error::new(
61 io::ErrorKind::WriteZero,
62 "failed to write the buffered data",
63 ));
64 break;
65 }
Chih-Hung Hsiehd45e96e2020-10-25 23:16:22 -070066 Ok(n) => *this.written += n,
Jason Macnakc417d3b2020-04-06 10:30:28 -070067 Err(e) => {
68 ret = Err(e);
69 break;
70 }
71 }
72 }
Chih-Hung Hsiehd45e96e2020-10-25 23:16:22 -070073 if *this.written > 0 {
74 this.buf.drain(..*this.written);
Jason Macnakc417d3b2020-04-06 10:30:28 -070075 }
Chih-Hung Hsiehd45e96e2020-10-25 23:16:22 -070076 *this.written = 0;
Jason Macnakc417d3b2020-04-06 10:30:28 -070077 Poll::Ready(ret)
78 }
79
Haibo Huang52627c82020-05-08 19:26:17 -070080 delegate_access_inner!(inner, W, ());
Jason Macnakc417d3b2020-04-06 10:30:28 -070081
82 /// Returns a reference to the internally buffered data.
83 pub fn buffer(&self) -> &[u8] {
84 &self.buf
85 }
86}
87
88impl<W: AsyncWrite> AsyncWrite for BufWriter<W> {
89 fn poll_write(
90 mut self: Pin<&mut Self>,
91 cx: &mut Context<'_>,
92 buf: &[u8],
93 ) -> Poll<io::Result<usize>> {
94 if self.buf.len() + buf.len() > self.buf.capacity() {
95 ready!(self.as_mut().flush_buf(cx))?;
96 }
97 if buf.len() >= self.buf.capacity() {
Haibo Huang52627c82020-05-08 19:26:17 -070098 self.project().inner.poll_write(cx, buf)
Jason Macnakc417d3b2020-04-06 10:30:28 -070099 } else {
Haibo Huang52627c82020-05-08 19:26:17 -0700100 Poll::Ready(self.project().buf.write(buf))
Jason Macnakc417d3b2020-04-06 10:30:28 -0700101 }
102 }
103
104 fn poll_write_vectored(
105 mut self: Pin<&mut Self>,
106 cx: &mut Context<'_>,
107 bufs: &[IoSlice<'_>],
108 ) -> Poll<io::Result<usize>> {
109 let total_len = bufs.iter().map(|b| b.len()).sum::<usize>();
110 if self.buf.len() + total_len > self.buf.capacity() {
111 ready!(self.as_mut().flush_buf(cx))?;
112 }
113 if total_len >= self.buf.capacity() {
Haibo Huang52627c82020-05-08 19:26:17 -0700114 self.project().inner.poll_write_vectored(cx, bufs)
Jason Macnakc417d3b2020-04-06 10:30:28 -0700115 } else {
Haibo Huang52627c82020-05-08 19:26:17 -0700116 Poll::Ready(self.project().buf.write_vectored(bufs))
Jason Macnakc417d3b2020-04-06 10:30:28 -0700117 }
118 }
119
120 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
121 ready!(self.as_mut().flush_buf(cx))?;
Haibo Huang52627c82020-05-08 19:26:17 -0700122 self.project().inner.poll_flush(cx)
Jason Macnakc417d3b2020-04-06 10:30:28 -0700123 }
124
125 fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
126 ready!(self.as_mut().flush_buf(cx))?;
Haibo Huang52627c82020-05-08 19:26:17 -0700127 self.project().inner.poll_close(cx)
Jason Macnakc417d3b2020-04-06 10:30:28 -0700128 }
129}
130
131impl<W: AsyncRead> AsyncRead for BufWriter<W> {
Haibo Huang52627c82020-05-08 19:26:17 -0700132 delegate_async_read!(inner);
Jason Macnakc417d3b2020-04-06 10:30:28 -0700133}
134
135impl<W: AsyncBufRead> AsyncBufRead for BufWriter<W> {
Haibo Huang52627c82020-05-08 19:26:17 -0700136 delegate_async_buf_read!(inner);
Jason Macnakc417d3b2020-04-06 10:30:28 -0700137}
138
139impl<W: fmt::Debug> fmt::Debug for BufWriter<W> {
140 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141 f.debug_struct("BufWriter")
142 .field("writer", &self.inner)
143 .field("buffer", &format_args!("{}/{}", self.buf.len(), self.buf.capacity()))
144 .field("written", &self.written)
145 .finish()
146 }
147}
148
149impl<W: AsyncWrite + AsyncSeek> AsyncSeek for BufWriter<W> {
150 /// Seek to the offset, in bytes, in the underlying writer.
151 ///
152 /// Seeking always writes out the internal buffer before seeking.
153 fn poll_seek(
154 mut self: Pin<&mut Self>,
155 cx: &mut Context<'_>,
156 pos: SeekFrom,
157 ) -> Poll<io::Result<u64>> {
158 ready!(self.as_mut().flush_buf(cx))?;
Haibo Huang52627c82020-05-08 19:26:17 -0700159 self.project().inner.poll_seek(cx, pos)
Jason Macnakc417d3b2020-04-06 10:30:28 -0700160 }
161}