From 34631a9f139ab538e47bac3d43eb97fd0eb9c31e Mon Sep 17 00:00:00 2001 From: phiresky Date: Mon, 5 Aug 2024 12:35:21 +0200 Subject: [PATCH] chore: simplify collect function --- crates/api_common/src/request.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/api_common/src/request.rs b/crates/api_common/src/request.rs index 01927d5d9..de6ba4f39 100644 --- a/crates/api_common/src/request.rs +++ b/crates/api_common/src/request.rs @@ -106,14 +106,13 @@ async fn collect_bytes_until_limit( ) -> Result, LemmyError> { let mut stream = response.bytes_stream(); let mut bytes = Vec::with_capacity(requested_bytes); - let mut total_bytes = 0; while let Some(chunk) = stream.next().await { let chunk = chunk.map_err(LemmyError::from)?; - total_bytes += chunk.len(); // we may go over the requested size here but the important part is we don't keep aggregating // more chunks than needed bytes.extend_from_slice(&chunk); - if total_bytes >= requested_bytes { + if bytes.len() >= requested_bytes { + bytes.truncate(requested_bytes); break; } }