Read a binary file into bytes iterator or stream


I wanted to read a binary file into a stream so that I can pass it to an existing function foo.

pub async fn foo(input_stream: &mut (impl Stream<Item = Vec<u8>> + Unpin))

I'm trying to read the file into an iterator, and then use stream::iter() to adapt it into a stream, but I cannot find a method to read the file into an iterator:

lines() did not work because it's not a text file, and bytes() iterator returns a single byte at a time. Did I miss something obvious?

Btw, I'm trying to do it using std. (We're not using tokio for async).

You can use read_until

Why do you want to perform blocking file read within the async code? Usually async runtimes provide its own async fs apis.

hmm, read_until seems not to provide an iterator or stream interface, as far as I can see.

It's okay for that part of the program. Yes, the function foo is async and that's okay. The async runtime used is smol and I didn't find what I wanted in smol::fs . Will check again.

I don't know about smol, but Tokio has the feature in the LinesStream type. Otherwise you could try the async-stream crate.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.