introduce remaining field in Payload

This commit is contained in:
Filip Bachul 2023-05-10 18:55:22 +02:00
parent 3064ea6495
commit 25209a3590

View File

@ -11,6 +11,7 @@ use crate::error::MeilisearchHttpError;
pub struct Payload { pub struct Payload {
payload: Decompress<dev::Payload>, payload: Decompress<dev::Payload>,
limit: usize, limit: usize,
remaining: usize,
} }
pub struct PayloadConfig { pub struct PayloadConfig {
@ -43,6 +44,7 @@ impl FromRequest for Payload {
ready(Ok(Payload { ready(Ok(Payload {
payload: Decompress::from_headers(payload.take(), req.headers()), payload: Decompress::from_headers(payload.take(), req.headers()),
limit, limit,
remaining: limit,
})) }))
} }
} }
@ -54,9 +56,9 @@ impl Stream for Payload {
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match Pin::new(&mut self.payload).poll_next(cx) { match Pin::new(&mut self.payload).poll_next(cx) {
Poll::Ready(Some(result)) => match result { Poll::Ready(Some(result)) => match result {
Ok(bytes) => match self.limit.checked_sub(bytes.len()) { Ok(bytes) => match self.remaining.checked_sub(bytes.len()) {
Some(new_limit) => { Some(new_limit) => {
self.limit = new_limit; self.remaining = new_limit;
Poll::Ready(Some(Ok(bytes))) Poll::Ready(Some(Ok(bytes)))
} }
None => { None => {