diff --git a/futures-util/src/io/buf_reader.rs b/futures-util/src/io/buf_reader.rs index deff1044ac..e0acdc9950 100644 --- a/futures-util/src/io/buf_reader.rs +++ b/futures-util/src/io/buf_reader.rs @@ -39,20 +39,7 @@ pin_project! { } } -impl BufReader { - /// Creates a new `BufReader` with a default buffer capacity. The default is currently 8 KB, - /// but may change in the future. - pub fn new(inner: R) -> Self { - Self::with_capacity(DEFAULT_BUF_SIZE, inner) - } - - /// Creates a new `BufReader` with the specified buffer capacity. - pub fn with_capacity(capacity: usize, inner: R) -> Self { - // TODO: consider using Box<[u8]>::new_uninit_slice once it stabilized - 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. @@ -71,6 +58,21 @@ impl BufReader { } } +impl BufReader { + /// Creates a new `BufReader` with a default buffer capacity. The default is currently 8 KB, + /// but may change in the future. + pub fn new(inner: R) -> Self { + Self::with_capacity(DEFAULT_BUF_SIZE, inner) + } + + /// Creates a new `BufReader` with the specified buffer capacity. + pub fn with_capacity(capacity: usize, inner: R) -> Self { + // TODO: consider using Box<[u8]>::new_uninit_slice once it stabilized + let buffer = vec![0; capacity]; + Self { inner, buffer: buffer.into_boxed_slice(), pos: 0, cap: 0 } + } +} + impl BufReader { /// Seeks relative to the current position. If the new position lies within the buffer, /// the buffer will not be flushed, allowing for more efficient seeks. diff --git a/futures-util/src/io/buf_writer.rs b/futures-util/src/io/buf_writer.rs index e0037262b9..83ddcceadf 100644 --- a/futures-util/src/io/buf_writer.rs +++ b/futures-util/src/io/buf_writer.rs @@ -39,6 +39,61 @@ pin_project! { } } +impl BufWriter { + delegate_access_inner!(inner, W, ()); + + /// Returns a reference to the internally buffered data. + pub fn buffer(&self) -> &[u8] { + &self.buf + } + + /// Capacity of `buf`. how many chars can be held in buffer + pub(super) fn capacity(&self) -> usize { + self.buf.capacity() + } + + /// Remaining number of bytes to reach `buf` 's capacity + #[inline] + pub(super) fn spare_capacity(&self) -> usize { + self.buf.capacity() - self.buf.len() + } + + /// Write byte slice directly into `self.buf` + /// + /// Based on `std::io::BufWriter` + #[inline] + unsafe fn write_to_buffer_unchecked(self: Pin<&mut Self>, buf: &[u8]) { + debug_assert!(buf.len() <= self.spare_capacity()); + let this = self.project(); + let old_len = this.buf.len(); + let buf_len = buf.len(); + let src = buf.as_ptr(); + unsafe { + let dst = this.buf.as_mut_ptr().add(old_len); + ptr::copy_nonoverlapping(src, dst, buf_len); + this.buf.set_len(old_len + 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 + } +} + impl BufWriter { /// Creates a new `BufWriter` with a default buffer capacity. The default is currently 8 KB, /// but may change in the future. @@ -79,59 +134,6 @@ impl BufWriter { Poll::Ready(ret) } - delegate_access_inner!(inner, W, ()); - - /// Returns a reference to the internally buffered data. - pub fn buffer(&self) -> &[u8] { - &self.buf - } - - /// Capacity of `buf`. how many chars can be held in buffer - pub(super) fn capacity(&self) -> usize { - self.buf.capacity() - } - - /// Remaining number of bytes to reach `buf` 's capacity - #[inline] - pub(super) fn spare_capacity(&self) -> usize { - 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` - #[inline] - unsafe fn write_to_buffer_unchecked(self: Pin<&mut Self>, buf: &[u8]) { - debug_assert!(buf.len() <= self.spare_capacity()); - let this = self.project(); - let old_len = this.buf.len(); - let buf_len = buf.len(); - let src = buf.as_ptr(); - unsafe { - let dst = this.buf.as_mut_ptr().add(old_len); - ptr::copy_nonoverlapping(src, dst, buf_len); - this.buf.set_len(old_len + buf_len); - } - } - /// Write directly using `inner`, bypassing buffering pub(super) fn inner_poll_write( self: Pin<&mut Self>,