File: io_copy.rs

package info (click to toggle)
rust-async-std 1.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,992 kB
  • sloc: sh: 13; makefile: 8
file content (72 lines) | stat: -rw-r--r-- 1,888 bytes parent folder | download | duplicates (2)
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
72
use std::{
    io::Result,
    pin::Pin,
    task::{Context, Poll},
};

struct ReaderThatPanicsAfterEof {
    read_count: usize,
    has_sent_eof: bool,
    max_read: usize,
}

impl async_std::io::Read for ReaderThatPanicsAfterEof {
    fn poll_read(
        mut self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<Result<usize>> {
        if self.has_sent_eof {
            panic!("this should be unreachable because we should not poll after eof (Ready(Ok(0)))")
        } else if self.read_count >= self.max_read {
            self.has_sent_eof = true;
            Poll::Ready(Ok(0))
        } else {
            self.read_count += 1;
            Poll::Ready(Ok(buf.len()))
        }
    }
}

struct WriterThatTakesAWhileToFlush {
    max_flush: usize,
    flush_count: usize,
}

impl async_std::io::Write for WriterThatTakesAWhileToFlush {
    fn poll_write(self: Pin<&mut Self>, _cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> {
        Poll::Ready(Ok(buf.len()))
    }

    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
        self.flush_count += 1;
        if self.flush_count >= self.max_flush {
            Poll::Ready(Ok(()))
        } else {
            cx.waker().wake_by_ref();
            Poll::Pending
        }
    }

    fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<()>> {
        Poll::Ready(Ok(()))
    }
}

#[test]
fn io_copy_does_not_poll_after_eof() {
    async_std::task::block_on(async {
        let mut reader = ReaderThatPanicsAfterEof {
            has_sent_eof: false,
            max_read: 10,
            read_count: 0,
        };

        let mut writer = WriterThatTakesAWhileToFlush {
            flush_count: 0,
            max_flush: 10,
        };

        assert!(async_std::io::copy(&mut reader, &mut writer).await.is_ok());
    })
}