Creating a Stream> from Tokio File
⚓ Rust 📅 2026-06-04 👤 surdeus 👁️ 1I'm attempting to create a streaming reqwest::Body from a file on my filesystem and I'm using Tokio, so it's async.
I'm getting reference issues as I have to borrow the buffer as mutable in my core stream loop and immutable in the body of the loop to read out of it.
Example code:
async fn file(path: PathBuf) -> Result<impl Stream<Item=Result<Bytes, anyhow::Error>>, anyhow::Error> {
let stream = try_stream! {
let mut f = tokio::fs::File::open(file).await.unwrap();
let mut buf = BytesMut::with_capacity(8 * 1024);
while let bytes_read = f.read(buf.as_mut()).await? {
if bytes_read == 0 { break }
yield Bytes::from(&buf[..bytes_read]);
}
}
Ok(stream)
}
I don't see a way to make buf non-mutable, as the read function needs a mutable reference, and I must be able to read from it in order to create a Bytes from it.
How should I go about doing this? Are there any examples out there for this? This seems like a common thing to do and I'm not seeing a path forward. I suppose I could wrap it all in a Mutex or a RwLock but that feels like overkill here.
2 posts - 1 participant
🏷️ Rust_feed