Skip to content

Commit

Permalink
Make access inner and other non constructor methods of futures::io::{…
Browse files Browse the repository at this point in the history
…BufReader,BufWriter} not require

inner trait bound
  • Loading branch information
ethe committed Mar 31, 2024
1 parent 48b58c0 commit f145046
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
2 changes: 2 additions & 0 deletions futures-util/src/io/buf_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ impl<R: AsyncRead> BufReader<R> {
let buffer = vec![0; capacity];
Self { inner, buffer: buffer.into_boxed_slice(), pos: 0, cap: 0 }
}
}

impl<R> BufReader<R> {
delegate_access_inner!(inner, R, ());

/// Returns a reference to the internally buffered data.
Expand Down
68 changes: 35 additions & 33 deletions futures-util/src/io/buf_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ impl<W: AsyncWrite> BufWriter<W> {
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<io::Result<usize>> {
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<io::Result<usize>> {
self.project().inner.poll_write_vectored(cx, bufs)
}
}

impl<W> BufWriter<W> {
delegate_access_inner!(inner, W, ());

/// Returns a reference to the internally buffered data.
Expand All @@ -97,24 +117,6 @@ impl<W: AsyncWrite> BufWriter<W> {
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`
Expand All @@ -132,22 +134,22 @@ impl<W: AsyncWrite> BufWriter<W> {
}
}

/// Write directly using `inner`, bypassing buffering
pub(super) fn inner_poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
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<io::Result<usize>> {
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
}
}

Expand Down

0 comments on commit f145046

Please sign in to comment.