Skip to content

Commit 2bb0bbd

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 2bb0bbd

File tree

2 files changed

+71
-67
lines changed

2 files changed

+71
-67
lines changed

futures-util/src/io/buf_reader.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,7 @@ pin_project! {
3939
}
4040
}
4141

42-
impl<R: AsyncRead> BufReader<R> {
43-
/// Creates a new `BufReader` with a default buffer capacity. The default is currently 8 KB,
44-
/// but may change in the future.
45-
pub fn new(inner: R) -> Self {
46-
Self::with_capacity(DEFAULT_BUF_SIZE, inner)
47-
}
48-
49-
/// Creates a new `BufReader` with the specified buffer capacity.
50-
pub fn with_capacity(capacity: usize, inner: R) -> Self {
51-
// TODO: consider using Box<[u8]>::new_uninit_slice once it stabilized
52-
let buffer = vec![0; capacity];
53-
Self { inner, buffer: buffer.into_boxed_slice(), pos: 0, cap: 0 }
54-
}
55-
42+
impl<R> BufReader<R> {
5643
delegate_access_inner!(inner, R, ());
5744

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

61+
impl<R: AsyncRead> BufReader<R> {
62+
/// Creates a new `BufReader` with a default buffer capacity. The default is currently 8 KB,
63+
/// but may change in the future.
64+
pub fn new(inner: R) -> Self {
65+
Self::with_capacity(DEFAULT_BUF_SIZE, inner)
66+
}
67+
68+
/// Creates a new `BufReader` with the specified buffer capacity.
69+
pub fn with_capacity(capacity: usize, inner: R) -> Self {
70+
// TODO: consider using Box<[u8]>::new_uninit_slice once it stabilized
71+
let buffer = vec![0; capacity];
72+
Self { inner, buffer: buffer.into_boxed_slice(), pos: 0, cap: 0 }
73+
}
74+
}
75+
7476
impl<R: AsyncRead + AsyncSeek> BufReader<R> {
7577
/// Seeks relative to the current position. If the new position lies within the buffer,
7678
/// the buffer will not be flushed, allowing for more efficient seeks.

futures-util/src/io/buf_writer.rs

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,61 @@ pin_project! {
3939
}
4040
}
4141

42+
impl<W> BufWriter<W> {
43+
delegate_access_inner!(inner, W, ());
44+
45+
/// Returns a reference to the internally buffered data.
46+
pub fn buffer(&self) -> &[u8] {
47+
&self.buf
48+
}
49+
50+
/// Capacity of `buf`. how many chars can be held in buffer
51+
pub(super) fn capacity(&self) -> usize {
52+
self.buf.capacity()
53+
}
54+
55+
/// Remaining number of bytes to reach `buf` 's capacity
56+
#[inline]
57+
pub(super) fn spare_capacity(&self) -> usize {
58+
self.buf.capacity() - self.buf.len()
59+
}
60+
61+
/// Write byte slice directly into `self.buf`
62+
///
63+
/// Based on `std::io::BufWriter`
64+
#[inline]
65+
unsafe fn write_to_buffer_unchecked(self: Pin<&mut Self>, buf: &[u8]) {
66+
debug_assert!(buf.len() <= self.spare_capacity());
67+
let this = self.project();
68+
let old_len = this.buf.len();
69+
let buf_len = buf.len();
70+
let src = buf.as_ptr();
71+
unsafe {
72+
let dst = this.buf.as_mut_ptr().add(old_len);
73+
ptr::copy_nonoverlapping(src, dst, buf_len);
74+
this.buf.set_len(old_len + buf_len);
75+
}
76+
}
77+
78+
/// Write a byte slice directly into buffer
79+
///
80+
/// Will truncate the number of bytes written to `spare_capacity()` so you want to
81+
/// calculate the size of your slice to avoid losing bytes
82+
///
83+
/// Based on `std::io::BufWriter`
84+
pub(super) fn write_to_buf(self: Pin<&mut Self>, buf: &[u8]) -> usize {
85+
let available = self.spare_capacity();
86+
let amt_to_buffer = available.min(buf.len());
87+
88+
// SAFETY: `amt_to_buffer` is <= buffer's spare capacity by construction.
89+
unsafe {
90+
self.write_to_buffer_unchecked(&buf[..amt_to_buffer]);
91+
}
92+
93+
amt_to_buffer
94+
}
95+
}
96+
4297
impl<W: AsyncWrite> BufWriter<W> {
4398
/// Creates a new `BufWriter` with a default buffer capacity. The default is currently 8 KB,
4499
/// but may change in the future.
@@ -79,59 +134,6 @@ impl<W: AsyncWrite> BufWriter<W> {
79134
Poll::Ready(ret)
80135
}
81136

82-
delegate_access_inner!(inner, W, ());
83-
84-
/// Returns a reference to the internally buffered data.
85-
pub fn buffer(&self) -> &[u8] {
86-
&self.buf
87-
}
88-
89-
/// Capacity of `buf`. how many chars can be held in buffer
90-
pub(super) fn capacity(&self) -> usize {
91-
self.buf.capacity()
92-
}
93-
94-
/// Remaining number of bytes to reach `buf` 's capacity
95-
#[inline]
96-
pub(super) fn spare_capacity(&self) -> usize {
97-
self.buf.capacity() - self.buf.len()
98-
}
99-
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-
118-
/// Write byte slice directly into `self.buf`
119-
///
120-
/// Based on `std::io::BufWriter`
121-
#[inline]
122-
unsafe fn write_to_buffer_unchecked(self: Pin<&mut Self>, buf: &[u8]) {
123-
debug_assert!(buf.len() <= self.spare_capacity());
124-
let this = self.project();
125-
let old_len = this.buf.len();
126-
let buf_len = buf.len();
127-
let src = buf.as_ptr();
128-
unsafe {
129-
let dst = this.buf.as_mut_ptr().add(old_len);
130-
ptr::copy_nonoverlapping(src, dst, buf_len);
131-
this.buf.set_len(old_len + buf_len);
132-
}
133-
}
134-
135137
/// Write directly using `inner`, bypassing buffering
136138
pub(super) fn inner_poll_write(
137139
self: Pin<&mut Self>,

0 commit comments

Comments
 (0)