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 2bb0bbd
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 67 deletions.
30 changes: 16 additions & 14 deletions futures-util/src/io/buf_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,7 @@ pin_project! {
}
}

impl<R: AsyncRead> BufReader<R> {
/// 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<R> BufReader<R> {
delegate_access_inner!(inner, R, ());

/// Returns a reference to the internally buffered data.
Expand All @@ -71,6 +58,21 @@ impl<R: AsyncRead> BufReader<R> {
}
}

impl<R: AsyncRead> BufReader<R> {
/// 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<R: AsyncRead + AsyncSeek> BufReader<R> {
/// 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.
Expand Down
108 changes: 55 additions & 53 deletions futures-util/src/io/buf_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,61 @@ pin_project! {
}
}

impl<W> BufWriter<W> {
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<W: AsyncWrite> BufWriter<W> {
/// Creates a new `BufWriter` with a default buffer capacity. The default is currently 8 KB,
/// but may change in the future.
Expand Down Expand Up @@ -79,59 +134,6 @@ impl<W: AsyncWrite> BufWriter<W> {
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>,
Expand Down

0 comments on commit 2bb0bbd

Please sign in to comment.