use axum::{body::BoxBody, response::IntoResponse}; use bytes::Bytes; use futures::StreamExt; use http::Response; use std::pin::Pin; use std::task::{Context, Poll}; use tokio::stream::Stream; struct SizingBody { inner: B, web3_request: RequestMetadata, } impl SizingBody { fn new(inner: B) -> Self { Self { inner, size: 0 } } } impl Stream for SizingBody where B: Stream>> + Unpin, { type Item = Result>; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { match Pin::new(&mut self.inner).poll_next(cx) { Poll::Ready(Some(Ok(chunk))) => { self.size += chunk.len(); Poll::Ready(Some(Ok(chunk))) } Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))), Poll::Ready(None) => { println!("Final response size: {}", self.size); Poll::Ready(None) } Poll::Pending => Poll::Pending, } } }