nom_bufreader/async_bufreader.rs
1use super::bufreader::DEFAULT_BUF_SIZE;
2use futures::io::{AsyncBufRead, AsyncRead, AsyncSeek, AsyncWrite, IoSliceMut, SeekFrom};
3use futures::ready;
4use futures::task::{Context, Poll};
5use pin_project_lite::pin_project;
6use std::io::{self, Read};
7use std::pin::Pin;
8use std::{cmp, fmt};
9
10pin_project! {
11 /// The `BufReader` struct adds buffering to any reader.
12 ///
13 /// It can be excessively inefficient to work directly with a [`AsyncRead`]
14 /// instance. A `BufReader` performs large, infrequent reads on the underlying
15 /// [`AsyncRead`] and maintains an in-memory buffer of the results.
16 ///
17 /// `BufReader` can improve the speed of programs that make *small* and
18 /// *repeated* read calls to the same file or network socket. It does not
19 /// help when reading very large amounts at once, or reading just one or a few
20 /// times. It also provides no advantage when reading from a source that is
21 /// already in memory, like a `Vec<u8>`.
22 ///
23 /// When the `BufReader` is dropped, the contents of its buffer will be
24 /// discarded. Creating multiple instances of a `BufReader` on the same
25 /// stream can cause data loss.
26 ///
27 /// **Note: this is a fork from `std::io::BufReader` that reads more data in
28 /// `fill_buf` even if there is already some data in the buffer**
29 ///
30 /// [`AsyncRead`]: futures_io::AsyncRead
31 ///
32 // TODO: Examples
33 pub struct BufReader<R> {
34 #[pin]
35 inner: R,
36 buffer: Vec<u8>,
37 pos: usize,
38 cap: usize,
39 }
40}
41
42impl<R: AsyncRead> BufReader<R> {
43 /// Creates a new `BufReader` with a default buffer capacity. The default is currently 8 KB,
44 /// but may change in the future.
45 pub fn new(inner: R) -> Self {
46 Self::with_capacity(DEFAULT_BUF_SIZE, inner)
47 }
48
49 /// Creates a new `BufReader` with the specified buffer capacity.
50 pub fn with_capacity(capacity: usize, inner: R) -> Self {
51 let buffer = vec![0; capacity];
52 Self {
53 inner,
54 buffer,
55 pos: 0,
56 cap: 0,
57 }
58 }
59
60 /// Acquires a reference to the underlying sink or stream that this combinator is
61 /// pulling from.
62 pub fn get_ref(&self) -> &R {
63 &self.inner
64 }
65
66 /// Acquires a mutable reference to the underlying sink or stream that this
67 /// combinator is pulling from.
68 ///
69 /// Note that care must be taken to avoid tampering with the state of the
70 /// sink or stream which may otherwise confuse this combinator.
71 pub fn get_mut(&mut self) -> &mut R {
72 &mut self.inner
73 }
74
75 /// Acquires a pinned mutable reference to the underlying sink or stream that this
76 /// combinator is pulling from.
77 ///
78 /// Note that care must be taken to avoid tampering with the state of the
79 /// sink or stream which may otherwise confuse this combinator.
80 pub fn get_pin_mut(self: core::pin::Pin<&mut Self>) -> core::pin::Pin<&mut R> {
81 self.project().inner
82 }
83
84 /// Consumes this combinator, returning the underlying sink or stream.
85 ///
86 /// Note that this may discard intermediate state of this combinator, so
87 /// care should be taken to avoid losing resources when this is called.
88 pub fn into_inner(self) -> R {
89 self.inner
90 }
91
92 /// Returns a reference to the internally buffered data.
93 ///
94 /// Unlike `fill_buf`, this will not attempt to fill the buffer if it is empty.
95 pub fn buffer(&self) -> &[u8] {
96 &self.buffer[self.pos..self.cap]
97 }
98
99 /// Invalidates all data in the internal buffer.
100 #[inline]
101 fn discard_buffer(self: Pin<&mut Self>) {
102 let this = self.project();
103 *this.pos = 0;
104 *this.cap = 0;
105 }
106}
107
108impl<R: AsyncRead> AsyncRead for BufReader<R> {
109 fn poll_read(
110 mut self: Pin<&mut Self>,
111 cx: &mut Context<'_>,
112 buf: &mut [u8],
113 ) -> Poll<io::Result<usize>> {
114 // If we don't have any buffered data and we're doing a massive read
115 // (larger than our internal buffer), bypass our internal buffer
116 // entirely.
117 if self.pos == self.cap && buf.len() >= self.buffer.len() {
118 let res = ready!(self.as_mut().project().inner.poll_read(cx, buf));
119 self.discard_buffer();
120 return Poll::Ready(res);
121 }
122 let mut rem = ready!(self.as_mut().poll_fill_buf(cx))?;
123 let nread = rem.read(buf)?;
124 self.consume(nread);
125 Poll::Ready(Ok(nread))
126 }
127
128 fn poll_read_vectored(
129 mut self: Pin<&mut Self>,
130 cx: &mut Context<'_>,
131 bufs: &mut [IoSliceMut<'_>],
132 ) -> Poll<io::Result<usize>> {
133 let total_len = bufs.iter().map(|b| b.len()).sum::<usize>();
134 if self.pos == self.cap && total_len >= self.buffer.len() {
135 let res = ready!(self.as_mut().project().inner.poll_read_vectored(cx, bufs));
136 self.discard_buffer();
137 return Poll::Ready(res);
138 }
139 let mut rem = ready!(self.as_mut().poll_fill_buf(cx))?;
140 let nread = rem.read_vectored(bufs)?;
141 self.consume(nread);
142 Poll::Ready(Ok(nread))
143 }
144
145 // we can't skip unconditionally because of the large buffer case in read.
146 #[cfg(feature = "read-initializer")]
147 unsafe fn initializer(&self) -> Initializer {
148 self.inner.initializer()
149 }
150}
151
152impl<R: AsyncRead> AsyncBufRead for BufReader<R> {
153 fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
154 let this = self.project();
155
156 if *this.cap == this.buffer.len() {
157 if *this.pos == 0 {
158 return Poll::Ready(Err(io::Error::new(
159 io::ErrorKind::Interrupted,
160 "buffer completely filled",
161 )));
162 } else {
163 // reset buffer position
164 if *this.cap - *this.pos > 0 {
165 for i in 0..(*this.cap - *this.pos) {
166 this.buffer[i] = this.buffer[*this.pos + i];
167 }
168 }
169 *this.cap = *this.cap - *this.pos;
170 *this.pos = 0;
171 }
172 }
173
174 let read = ready!(this.inner.poll_read(cx, this.buffer))?;
175 *this.cap += read;
176
177 Poll::Ready(Ok(&this.buffer[*this.pos..*this.cap]))
178 }
179
180 fn consume(self: Pin<&mut Self>, amt: usize) {
181 *self.project().pos = cmp::min(self.pos + amt, self.cap);
182 }
183}
184
185impl<R: AsyncWrite> AsyncWrite for BufReader<R> {
186 fn poll_write(
187 self: core::pin::Pin<&mut Self>,
188 cx: &mut core::task::Context<'_>,
189 buf: &[u8],
190 ) -> core::task::Poll<std::io::Result<usize>> {
191 self.project().inner.poll_write(cx, buf)
192 }
193 fn poll_write_vectored(
194 self: core::pin::Pin<&mut Self>,
195 cx: &mut core::task::Context<'_>,
196 bufs: &[std::io::IoSlice<'_>],
197 ) -> core::task::Poll<std::io::Result<usize>> {
198 self.project().inner.poll_write_vectored(cx, bufs)
199 }
200 fn poll_flush(
201 self: core::pin::Pin<&mut Self>,
202 cx: &mut core::task::Context<'_>,
203 ) -> core::task::Poll<std::io::Result<()>> {
204 self.project().inner.poll_flush(cx)
205 }
206 fn poll_close(
207 self: core::pin::Pin<&mut Self>,
208 cx: &mut core::task::Context<'_>,
209 ) -> core::task::Poll<std::io::Result<()>> {
210 self.project().inner.poll_close(cx)
211 }
212}
213
214impl<R: fmt::Debug> fmt::Debug for BufReader<R> {
215 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
216 f.debug_struct("BufReader")
217 .field("reader", &self.inner)
218 .field(
219 "buffer",
220 &format_args!("{}/{}", self.cap - self.pos, self.buffer.len()),
221 )
222 .finish()
223 }
224}
225
226impl<R: AsyncRead + AsyncSeek> AsyncSeek for BufReader<R> {
227 /// Seek to an offset, in bytes, in the underlying reader.
228 ///
229 /// The position used for seeking with `SeekFrom::Current(_)` is the
230 /// position the underlying reader would be at if the `BufReader` had no
231 /// internal buffer.
232 ///
233 /// Seeking always discards the internal buffer, even if the seek position
234 /// would otherwise fall within it. This guarantees that calling
235 /// `.into_inner()` immediately after a seek yields the underlying reader
236 /// at the same position.
237 ///
238 /// See [`AsyncSeek`](futures_io::AsyncSeek) for more details.
239 ///
240 /// Note: In the edge case where you're seeking with `SeekFrom::Current(n)`
241 /// where `n` minus the internal buffer length overflows an `i64`, two
242 /// seeks will be performed instead of one. If the second seek returns
243 /// `Err`, the underlying reader will be left at the same position it would
244 /// have if you called `seek` with `SeekFrom::Current(0)`.
245 fn poll_seek(
246 mut self: Pin<&mut Self>,
247 cx: &mut Context<'_>,
248 pos: SeekFrom,
249 ) -> Poll<io::Result<u64>> {
250 let result: u64;
251 if let SeekFrom::Current(n) = pos {
252 let remainder = (self.cap - self.pos) as i64;
253 // it should be safe to assume that remainder fits within an i64 as the alternative
254 // means we managed to allocate 8 exbibytes and that's absurd.
255 // But it's not out of the realm of possibility for some weird underlying reader to
256 // support seeking by i64::min_value() so we need to handle underflow when subtracting
257 // remainder.
258 if let Some(offset) = n.checked_sub(remainder) {
259 result = ready!(self
260 .as_mut()
261 .project()
262 .inner
263 .poll_seek(cx, SeekFrom::Current(offset)))?;
264 } else {
265 // seek backwards by our remainder, and then by the offset
266 ready!(self
267 .as_mut()
268 .project()
269 .inner
270 .poll_seek(cx, SeekFrom::Current(-remainder)))?;
271 self.as_mut().discard_buffer();
272 result = ready!(self
273 .as_mut()
274 .project()
275 .inner
276 .poll_seek(cx, SeekFrom::Current(n)))?;
277 }
278 } else {
279 // Seeking with Start/End doesn't care about our buffer length.
280 result = ready!(self.as_mut().project().inner.poll_seek(cx, pos))?;
281 }
282 self.discard_buffer();
283 Poll::Ready(Ok(result))
284 }
285}