Skip to content

Commit f145046

Browse files
committed
Make access inner and other non constructor methods of futures::io::{BufReader,BufWriter} not require
inner trait bound
1 parent 48b58c0 commit f145046

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

futures-util/src/io/buf_reader.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ impl<R: AsyncRead> BufReader<R> {
5252
let buffer = vec![0; capacity];
5353
Self { inner, buffer: buffer.into_boxed_slice(), pos: 0, cap: 0 }
5454
}
55+
}
5556

57+
impl<R> BufReader<R> {
5658
delegate_access_inner!(inner, R, ());
5759

5860
/// Returns a reference to the internally buffered data.

futures-util/src/io/buf_writer.rs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ impl<W: AsyncWrite> BufWriter<W> {
7979
Poll::Ready(ret)
8080
}
8181

82+
/// Write directly using `inner`, bypassing buffering
83+
pub(super) fn inner_poll_write(
84+
self: Pin<&mut Self>,
85+
cx: &mut Context<'_>,
86+
buf: &[u8],
87+
) -> Poll<io::Result<usize>> {
88+
self.project().inner.poll_write(cx, buf)
89+
}
90+
91+
/// Write directly using `inner`, bypassing buffering
92+
pub(super) fn inner_poll_write_vectored(
93+
self: Pin<&mut Self>,
94+
cx: &mut Context<'_>,
95+
bufs: &[IoSlice<'_>],
96+
) -> Poll<io::Result<usize>> {
97+
self.project().inner.poll_write_vectored(cx, bufs)
98+
}
99+
}
100+
101+
impl<W> BufWriter<W> {
82102
delegate_access_inner!(inner, W, ());
83103

84104
/// Returns a reference to the internally buffered data.
@@ -97,24 +117,6 @@ impl<W: AsyncWrite> BufWriter<W> {
97117
self.buf.capacity() - self.buf.len()
98118
}
99119

100-
/// Write a byte slice directly into buffer
101-
///
102-
/// Will truncate the number of bytes written to `spare_capacity()` so you want to
103-
/// calculate the size of your slice to avoid losing bytes
104-
///
105-
/// Based on `std::io::BufWriter`
106-
pub(super) fn write_to_buf(self: Pin<&mut Self>, buf: &[u8]) -> usize {
107-
let available = self.spare_capacity();
108-
let amt_to_buffer = available.min(buf.len());
109-
110-
// SAFETY: `amt_to_buffer` is <= buffer's spare capacity by construction.
111-
unsafe {
112-
self.write_to_buffer_unchecked(&buf[..amt_to_buffer]);
113-
}
114-
115-
amt_to_buffer
116-
}
117-
118120
/// Write byte slice directly into `self.buf`
119121
///
120122
/// Based on `std::io::BufWriter`
@@ -132,22 +134,22 @@ impl<W: AsyncWrite> BufWriter<W> {
132134
}
133135
}
134136

135-
/// Write directly using `inner`, bypassing buffering
136-
pub(super) fn inner_poll_write(
137-
self: Pin<&mut Self>,
138-
cx: &mut Context<'_>,
139-
buf: &[u8],
140-
) -> Poll<io::Result<usize>> {
141-
self.project().inner.poll_write(cx, buf)
142-
}
137+
/// Write a byte slice directly into buffer
138+
///
139+
/// Will truncate the number of bytes written to `spare_capacity()` so you want to
140+
/// calculate the size of your slice to avoid losing bytes
141+
///
142+
/// Based on `std::io::BufWriter`
143+
pub(super) fn write_to_buf(self: Pin<&mut Self>, buf: &[u8]) -> usize {
144+
let available = self.spare_capacity();
145+
let amt_to_buffer = available.min(buf.len());
143146

144-
/// Write directly using `inner`, bypassing buffering
145-
pub(super) fn inner_poll_write_vectored(
146-
self: Pin<&mut Self>,
147-
cx: &mut Context<'_>,
148-
bufs: &[IoSlice<'_>],
149-
) -> Poll<io::Result<usize>> {
150-
self.project().inner.poll_write_vectored(cx, bufs)
147+
// SAFETY: `amt_to_buffer` is <= buffer's spare capacity by construction.
148+
unsafe {
149+
self.write_to_buffer_unchecked(&buf[..amt_to_buffer]);
150+
}
151+
152+
amt_to_buffer
151153
}
152154
}
153155

0 commit comments

Comments
 (0)