refactor(tvix/store/fs): simpllify read

We can just use take(size) to restrict reading to that as a max.

Change-Id: I0fbda74e4fb98ffeababae86a325233416029acf
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10072
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
This commit is contained in:
Florian Klink 2023-11-19 00:56:46 +02:00 committed by clbot
parent eda5d4da37
commit 8beb38b50c

View file

@ -26,7 +26,7 @@ use std::{
time::Duration, time::Duration,
}; };
use tokio::{ use tokio::{
io::{AsyncBufReadExt, AsyncSeekExt}, io::{AsyncReadExt, AsyncSeekExt},
sync::mpsc, sync::mpsc,
}; };
use tracing::{debug, info_span, instrument, warn}; use tracing::{debug, info_span, instrument, warn};
@ -614,24 +614,8 @@ impl FileSystem for TvixStoreFs {
let mut buf: Vec<u8> = Vec::with_capacity(size as usize); let mut buf: Vec<u8> = Vec::with_capacity(size as usize);
while (buf.len() as u64) < size as u64 {
let int_buf = blob_reader.fill_buf().await?;
// copy things from the internal buffer into buf to fill it till up until size // copy things from the internal buffer into buf to fill it till up until size
tokio::io::copy(&mut blob_reader.as_mut().take(size as u64), &mut buf).await?;
// an empty buffer signals we reached EOF.
if int_buf.is_empty() {
break;
}
// calculate how many bytes we can read from int_buf.
// It's either all of int_buf, or the number of bytes missing in buf to reach size.
let len_to_copy = std::cmp::min(int_buf.len(), size as usize - buf.len());
// copy these bytes into our buffer
buf.extend_from_slice(&int_buf[..len_to_copy]);
// and consume them in the buffered reader.
blob_reader.consume(len_to_copy);
}
Ok(buf) Ok(buf)
}); });