Line data Source code
1 : use pin_project_lite::pin_project;
2 : use std::io::Read;
3 : use std::pin::Pin;
4 : use std::{io, task};
5 : use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
6 :
7 : pin_project! {
8 : /// This stream tracks all writes and calls user provided
9 : /// callback when the underlying stream is flushed.
10 : pub struct MeasuredStream<S, R, W> {
11 : #[pin]
12 : stream: S,
13 : write_count: usize,
14 : inc_read_count: R,
15 : inc_write_count: W,
16 : }
17 : }
18 :
19 : impl<S, R, W> MeasuredStream<S, R, W> {
20 3372 : pub fn new(stream: S, inc_read_count: R, inc_write_count: W) -> Self {
21 3372 : Self {
22 3372 : stream,
23 3372 : write_count: 0,
24 3372 : inc_read_count,
25 3372 : inc_write_count,
26 3372 : }
27 3372 : }
28 : }
29 :
30 : impl<S: AsyncRead + Unpin, R: FnMut(usize), W> AsyncRead for MeasuredStream<S, R, W> {
31 9513201 : fn poll_read(
32 9513201 : self: Pin<&mut Self>,
33 9513201 : context: &mut task::Context<'_>,
34 9513201 : buf: &mut ReadBuf<'_>,
35 9513201 : ) -> task::Poll<io::Result<()>> {
36 9513201 : let this = self.project();
37 9513201 : let filled = buf.filled().len();
38 9513201 : this.stream.poll_read(context, buf).map_ok(|()| {
39 2928859 : let cnt = buf.filled().len() - filled;
40 2928859 : // Increment the read count.
41 2928859 : (this.inc_read_count)(cnt);
42 9513201 : })
43 9513201 : }
44 : }
45 :
46 : impl<S: AsyncWrite + Unpin, R, W: FnMut(usize)> AsyncWrite for MeasuredStream<S, R, W> {
47 2506362 : fn poll_write(
48 2506362 : self: Pin<&mut Self>,
49 2506362 : context: &mut task::Context<'_>,
50 2506362 : buf: &[u8],
51 2506362 : ) -> task::Poll<io::Result<usize>> {
52 2506362 : let this = self.project();
53 2506362 : this.stream.poll_write(context, buf).map_ok(|cnt| {
54 2482469 : // Increment the write count.
55 2482469 : *this.write_count += cnt;
56 2482469 : cnt
57 2506362 : })
58 2506362 : }
59 :
60 2483346 : fn poll_flush(
61 2483346 : self: Pin<&mut Self>,
62 2483346 : context: &mut task::Context<'_>,
63 2483346 : ) -> task::Poll<io::Result<()>> {
64 2483346 : let this = self.project();
65 2483346 : this.stream.poll_flush(context).map_ok(|()| {
66 2483346 : // Call the user provided callback and reset the write count.
67 2483346 : (this.inc_write_count)(*this.write_count);
68 2483346 : *this.write_count = 0;
69 2483346 : })
70 2483346 : }
71 :
72 2697 : fn poll_shutdown(
73 2697 : self: Pin<&mut Self>,
74 2697 : context: &mut task::Context<'_>,
75 2697 : ) -> task::Poll<io::Result<()>> {
76 2697 : self.project().stream.poll_shutdown(context)
77 2697 : }
78 : }
79 :
80 : /// Wrapper for a reader that counts bytes read.
81 : ///
82 : /// Similar to MeasuredStream but it's one way and it's sync
83 : pub struct MeasuredReader<R: Read> {
84 : inner: R,
85 : byte_count: usize,
86 : }
87 :
88 : impl<R: Read> MeasuredReader<R> {
89 575 : pub fn new(reader: R) -> Self {
90 575 : Self {
91 575 : inner: reader,
92 575 : byte_count: 0,
93 575 : }
94 575 : }
95 :
96 575 : pub fn get_byte_count(&self) -> usize {
97 575 : self.byte_count
98 575 : }
99 : }
100 :
101 : impl<R: Read> Read for MeasuredReader<R> {
102 867823 : fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
103 867823 : let result = self.inner.read(buf);
104 867823 : if let Ok(n_bytes) = result {
105 867823 : self.byte_count += n_bytes
106 0 : }
107 867823 : result
108 867823 : }
109 : }
|