LCOV - code coverage report
Current view: top level - libs/utils/src - measured_stream.rs (source / functions) Coverage Total Hit
Test: 3eba1babe267649f8cebefc91c236589db030548.info Lines: 0.0 % 66 0
Test Date: 2024-11-22 12:36:12 Functions: 0.0 % 54 0

            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            0 :     pub fn new(stream: S, inc_read_count: R, inc_write_count: W) -> Self {
      21            0 :         Self {
      22            0 :             stream,
      23            0 :             write_count: 0,
      24            0 :             inc_read_count,
      25            0 :             inc_write_count,
      26            0 :         }
      27            0 :     }
      28              : }
      29              : 
      30              : impl<S: AsyncRead + Unpin, R: FnMut(usize), W> AsyncRead for MeasuredStream<S, R, W> {
      31            0 :     fn poll_read(
      32            0 :         self: Pin<&mut Self>,
      33            0 :         context: &mut task::Context<'_>,
      34            0 :         buf: &mut ReadBuf<'_>,
      35            0 :     ) -> task::Poll<io::Result<()>> {
      36            0 :         let this = self.project();
      37            0 :         let filled = buf.filled().len();
      38            0 :         this.stream.poll_read(context, buf).map_ok(|()| {
      39            0 :             let cnt = buf.filled().len() - filled;
      40            0 :             // Increment the read count.
      41            0 :             (this.inc_read_count)(cnt);
      42            0 :         })
      43            0 :     }
      44              : }
      45              : 
      46              : impl<S: AsyncWrite + Unpin, R, W: FnMut(usize)> AsyncWrite for MeasuredStream<S, R, W> {
      47            0 :     fn poll_write(
      48            0 :         self: Pin<&mut Self>,
      49            0 :         context: &mut task::Context<'_>,
      50            0 :         buf: &[u8],
      51            0 :     ) -> task::Poll<io::Result<usize>> {
      52            0 :         let this = self.project();
      53            0 :         this.stream.poll_write(context, buf).map_ok(|cnt| {
      54            0 :             // Increment the write count.
      55            0 :             *this.write_count += cnt;
      56            0 :             cnt
      57            0 :         })
      58            0 :     }
      59              : 
      60            0 :     fn poll_flush(
      61            0 :         self: Pin<&mut Self>,
      62            0 :         context: &mut task::Context<'_>,
      63            0 :     ) -> task::Poll<io::Result<()>> {
      64            0 :         let this = self.project();
      65            0 :         this.stream.poll_flush(context).map_ok(|()| {
      66            0 :             // Call the user provided callback and reset the write count.
      67            0 :             (this.inc_write_count)(*this.write_count);
      68            0 :             *this.write_count = 0;
      69            0 :         })
      70            0 :     }
      71              : 
      72            0 :     fn poll_shutdown(
      73            0 :         self: Pin<&mut Self>,
      74            0 :         context: &mut task::Context<'_>,
      75            0 :     ) -> task::Poll<io::Result<()>> {
      76            0 :         self.project().stream.poll_shutdown(context)
      77            0 :     }
      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            0 :     pub fn new(reader: R) -> Self {
      90            0 :         Self {
      91            0 :             inner: reader,
      92            0 :             byte_count: 0,
      93            0 :         }
      94            0 :     }
      95              : 
      96            0 :     pub fn get_byte_count(&self) -> usize {
      97            0 :         self.byte_count
      98            0 :     }
      99              : }
     100              : 
     101              : impl<R: Read> Read for MeasuredReader<R> {
     102            0 :     fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
     103            0 :         let result = self.inner.read(buf);
     104            0 :         if let Ok(n_bytes) = result {
     105            0 :             self.byte_count += n_bytes
     106            0 :         }
     107            0 :         result
     108            0 :     }
     109              : }
        

Generated by: LCOV version 2.1-beta