mirror of
https://github.com/LemmyNet/lemmy
synced 2024-11-10 06:54:12 +00:00
chore: simplify collect function
This commit is contained in:
parent
b60e511493
commit
34631a9f13
1 changed files with 2 additions and 3 deletions
|
@ -106,14 +106,13 @@ async fn collect_bytes_until_limit(
|
||||||
) -> Result<Vec<u8>, LemmyError> {
|
) -> Result<Vec<u8>, LemmyError> {
|
||||||
let mut stream = response.bytes_stream();
|
let mut stream = response.bytes_stream();
|
||||||
let mut bytes = Vec::with_capacity(requested_bytes);
|
let mut bytes = Vec::with_capacity(requested_bytes);
|
||||||
let mut total_bytes = 0;
|
|
||||||
while let Some(chunk) = stream.next().await {
|
while let Some(chunk) = stream.next().await {
|
||||||
let chunk = chunk.map_err(LemmyError::from)?;
|
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
|
// we may go over the requested size here but the important part is we don't keep aggregating
|
||||||
// more chunks than needed
|
// more chunks than needed
|
||||||
bytes.extend_from_slice(&chunk);
|
bytes.extend_from_slice(&chunk);
|
||||||
if total_bytes >= requested_bytes {
|
if bytes.len() >= requested_bytes {
|
||||||
|
bytes.truncate(requested_bytes);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue