diff --git a/futures-util/src/io/buf_reader.rs b/futures-util/src/io/buf_reader.rs index deff1044a..30611bd8d 100644 --- a/futures-util/src/io/buf_reader.rs +++ b/futures-util/src/io/buf_reader.rs @@ -52,7 +52,9 @@ impl BufReader { let buffer = vec![0; capacity]; Self { inner, buffer: buffer.into_boxed_slice(), pos: 0, cap: 0 } } +} +impl BufReader { delegate_access_inner!(inner, R, ()); /// Returns a reference to the internally buffered data. diff --git a/futures-util/src/io/buf_writer.rs b/futures-util/src/io/buf_writer.rs index e0037262b..0c689a53f 100644 --- a/futures-util/src/io/buf_writer.rs +++ b/futures-util/src/io/buf_writer.rs @@ -79,6 +79,26 @@ impl BufWriter { Poll::Ready(ret) } + /// Write directly using `inner`, bypassing buffering + pub(super) fn inner_poll_write( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &[u8], + ) -> Poll> { + self.project().inner.poll_write(cx, buf) + } + + /// Write directly using `inner`, bypassing buffering + pub(super) fn inner_poll_write_vectored( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + bufs: &[IoSlice<'_>], + ) -> Poll> { + self.project().inner.poll_write_vectored(cx, bufs) + } +} + +impl BufWriter { delegate_access_inner!(inner, W, ()); /// Returns a reference to the internally buffered data. @@ -97,24 +117,6 @@ impl BufWriter { self.buf.capacity() - self.buf.len() } - /// Write a byte slice directly into buffer - /// - /// Will truncate the number of bytes written to `spare_capacity()` so you want to - /// calculate the size of your slice to avoid losing bytes - /// - /// Based on `std::io::BufWriter` - pub(super) fn write_to_buf(self: Pin<&mut Self>, buf: &[u8]) -> usize { - let available = self.spare_capacity(); - let amt_to_buffer = available.min(buf.len()); - - // SAFETY: `amt_to_buffer` is <= buffer's spare capacity by construction. - unsafe { - self.write_to_buffer_unchecked(&buf[..amt_to_buffer]); - } - - amt_to_buffer - } - /// Write byte slice directly into `self.buf` /// /// Based on `std::io::BufWriter` @@ -132,22 +134,22 @@ impl BufWriter { } } - /// Write directly using `inner`, bypassing buffering - pub(super) fn inner_poll_write( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &[u8], - ) -> Poll> { - self.project().inner.poll_write(cx, buf) - } + /// Write a byte slice directly into buffer + /// + /// Will truncate the number of bytes written to `spare_capacity()` so you want to + /// calculate the size of your slice to avoid losing bytes + /// + /// Based on `std::io::BufWriter` + pub(super) fn write_to_buf(self: Pin<&mut Self>, buf: &[u8]) -> usize { + let available = self.spare_capacity(); + let amt_to_buffer = available.min(buf.len()); - /// Write directly using `inner`, bypassing buffering - pub(super) fn inner_poll_write_vectored( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - bufs: &[IoSlice<'_>], - ) -> Poll> { - self.project().inner.poll_write_vectored(cx, bufs) + // SAFETY: `amt_to_buffer` is <= buffer's spare capacity by construction. + unsafe { + self.write_to_buffer_unchecked(&buf[..amt_to_buffer]); + } + + amt_to_buffer } }