Skip to content

Commit 9002b41

Browse files
Nemo157cramertj
authored andcommitted
Redefine AsyncReadExt::copy_into on top of AsyncBufReadExt::copy_buf_into
1 parent 818072d commit 9002b41

File tree

2 files changed

+22
-66
lines changed

2 files changed

+22
-66
lines changed

futures-util/src/io/copy_into.rs

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,32 @@ use futures_core::task::{Context, Poll};
33
use futures_io::{AsyncRead, AsyncWrite};
44
use std::io;
55
use std::pin::Pin;
6+
use super::{BufReader, CopyBufInto};
7+
use pin_utils::unsafe_pinned;
68

79
/// Future for the [`copy_into`](super::AsyncReadExt::copy_into) method.
810
#[derive(Debug)]
911
#[must_use = "futures do nothing unless you `.await` or poll them"]
10-
pub struct CopyInto<'a, R: ?Sized + Unpin, W: ?Sized + Unpin> {
11-
reader: &'a mut R,
12-
read_done: bool,
13-
writer: &'a mut W,
14-
pos: usize,
15-
cap: usize,
16-
amt: u64,
17-
buf: Box<[u8]>,
12+
pub struct CopyInto<R: AsyncRead, W> {
13+
inner: CopyBufInto<BufReader<R>, W>,
1814
}
1915

20-
impl<R: ?Sized + Unpin, W: ?Sized + Unpin> Unpin for CopyInto<'_, R, W> {}
16+
impl<R: AsyncRead, W> Unpin for CopyInto<R, W> where CopyBufInto<BufReader<R>, W>: Unpin {}
2117

22-
impl<'a, R: ?Sized + Unpin, W: ?Sized + Unpin> CopyInto<'a, R, W> {
23-
pub(super) fn new(reader: &'a mut R, writer: &'a mut W) -> Self {
18+
impl<R: AsyncRead, W> CopyInto<R, W> {
19+
unsafe_pinned!(inner: CopyBufInto<BufReader<R>, W>);
20+
21+
pub(super) fn new(reader: R, writer: W) -> Self {
2422
CopyInto {
25-
reader,
26-
read_done: false,
27-
writer,
28-
amt: 0,
29-
pos: 0,
30-
cap: 0,
31-
buf: Box::new([0; 2048]),
23+
inner: CopyBufInto::new(BufReader::new(reader), writer),
3224
}
3325
}
3426
}
3527

36-
impl<R, W> Future for CopyInto<'_, R, W>
37-
where R: AsyncRead + ?Sized + Unpin,
38-
W: AsyncWrite + ?Sized + Unpin,
39-
{
28+
impl<R: AsyncRead, W: AsyncWrite> Future for CopyInto<R, W> {
4029
type Output = io::Result<u64>;
4130

42-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
43-
let this = &mut *self;
44-
loop {
45-
// If our buffer is empty, then we need to read some data to
46-
// continue.
47-
if this.pos == this.cap && !this.read_done {
48-
let n = ready!(Pin::new(&mut this.reader).poll_read(cx, &mut this.buf))?;
49-
if n == 0 {
50-
this.read_done = true;
51-
} else {
52-
this.pos = 0;
53-
this.cap = n;
54-
}
55-
}
56-
57-
// If our buffer has some data, let's write it out!
58-
while this.pos < this.cap {
59-
let i = ready!(Pin::new(&mut this.writer).poll_write(cx, &this.buf[this.pos..this.cap]))?;
60-
if i == 0 {
61-
return Poll::Ready(Err(io::ErrorKind::WriteZero.into()))
62-
} else {
63-
this.pos += i;
64-
this.amt += i as u64;
65-
}
66-
}
67-
68-
// If we've written al the data and we've seen EOF, flush out the
69-
// data and finish the transfer.
70-
// done with the entire transfer.
71-
if this.pos == this.cap && this.read_done {
72-
ready!(Pin::new(&mut this.writer).poll_flush(cx))?;
73-
return Poll::Ready(Ok(this.amt));
74-
}
75-
}
31+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
32+
self.inner().poll(cx)
7633
}
7734
}

futures-util/src/io/mod.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,29 +93,28 @@ pub trait AsyncReadExt: AsyncRead {
9393
///
9494
/// On success the number of bytes is returned.
9595
///
96+
/// Note that this method consumes `writer` but does not close it, you will likely want to pass
97+
/// it by reference as shown in the example.
98+
///
9699
/// # Examples
97100
///
98101
/// ```
99102
/// #![feature(async_await)]
100103
/// # futures::executor::block_on(async {
101-
/// use futures::io::AsyncReadExt;
104+
/// use futures::io::{AsyncReadExt, AsyncWriteExt};
102105
/// use std::io::Cursor;
103106
///
104-
/// let mut reader = Cursor::new([1, 2, 3, 4]);
107+
/// let reader = Cursor::new([1, 2, 3, 4]);
105108
/// let mut writer = Cursor::new([0u8; 5]);
106109
///
107110
/// let bytes = reader.copy_into(&mut writer).await?;
111+
/// writer.close().await?;
108112
///
109113
/// assert_eq!(bytes, 4);
110114
/// assert_eq!(writer.into_inner(), [1, 2, 3, 4, 0]);
111115
/// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap();
112116
/// ```
113-
fn copy_into<'a, W>(
114-
&'a mut self,
115-
writer: &'a mut W,
116-
) -> CopyInto<'a, Self, W>
117-
where Self: Unpin, W: AsyncWrite + Unpin,
118-
{
117+
fn copy_into<W: AsyncWrite>(self, writer: W) -> CopyInto<Self, W> where Self: Sized {
119118
CopyInto::new(self, writer)
120119
}
121120

@@ -256,12 +255,12 @@ pub trait AsyncReadExt: AsyncRead {
256255
/// // seek position. This may or may not be true for other types that
257256
/// // implement both `AsyncRead` and `AsyncWrite`.
258257
///
259-
/// let mut reader = Cursor::new([1, 2, 3, 4]);
258+
/// let reader = Cursor::new([1, 2, 3, 4]);
260259
/// let mut buffer = Cursor::new([0, 0, 0, 0, 5, 6, 7, 8]);
261260
/// let mut writer = Cursor::new([0u8; 5]);
262261
///
263262
/// {
264-
/// let (mut buffer_reader, mut buffer_writer) = (&mut buffer).split();
263+
/// let (buffer_reader, mut buffer_writer) = (&mut buffer).split();
265264
/// reader.copy_into(&mut buffer_writer).await?;
266265
/// buffer_reader.copy_into(&mut writer).await?;
267266
/// }

0 commit comments

Comments
 (0)