use std::{ pin::Pin, task::{Context, Poll}, }; #[derive(Debug)] pub struct Limited { io: Io, limit: usize, } impl Limited { pub(crate) fn new(io: Io, limit: usize) -> Limited { Limited { io, limit } } } impl tokio::io::AsyncWrite for Limited { fn poll_write( mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll> { let limit = self.limit; Pin::new(&mut self.io).poll_write(cx, &buf[..std::cmp::min(limit, buf.len())]) } fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { Pin::new(&mut self.io).poll_flush(cx) } fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { Pin::new(&mut self.io).poll_shutdown(cx) } }