1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use futures_lite::{io::BufReader, ready, AsyncBufRead, AsyncRead, AsyncWrite};
use std::{
    future::Future,
    io::{ErrorKind, Result},
    pin::Pin,
    task::{Context, Poll},
};

/// copy from the `reader` to the `writer`, yielding back to the runtime every `loops_per_yield`
///
/// # Errors
///
/// This returns any io error encountered in reading or writing
pub async fn copy<R, W>(reader: R, writer: W, loops_per_yield: usize) -> Result<u64>
where
    R: AsyncRead + Unpin,
    W: AsyncWrite + Unpin,
{
    struct CopyFuture<R, W> {
        reader: BufReader<R>,
        writer: W,
        amt: u64,
        loops_per_yield: usize,
    }

    impl<R, W> Future for CopyFuture<R, W>
    where
        R: AsyncRead + Unpin,
        W: AsyncWrite + Unpin,
    {
        type Output = Result<u64>;

        fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
            for loop_number in 0..self.loops_per_yield {
                log::trace!("copy loop number: {loop_number}");
                let CopyFuture {
                    reader,
                    writer,
                    amt,
                    ..
                } = &mut *self;

                let writer = Pin::new(writer);
                let mut reader = Pin::new(reader);
                let buffer = ready!(reader.as_mut().poll_fill_buf(cx))?;
                if buffer.is_empty() {
                    ready!(writer.poll_flush(cx))?;
                    return Poll::Ready(Ok(self.amt));
                }

                let i = ready!(writer.poll_write(cx, buffer))?;
                if i == 0 {
                    return Poll::Ready(Err(ErrorKind::WriteZero.into()));
                }
                *amt += i as u64;
                reader.consume(i);
            }

            cx.waker().wake_by_ref();
            Poll::Pending
        }
    }

    let future = CopyFuture {
        reader: BufReader::new(reader),
        writer,
        amt: 0,
        loops_per_yield,
    };
    future.await
}