I am very new to rust (literally learnt it two weeks ago) and I've decided to make a faster node package manager (like npm) as my first project.
On the way, I have encountered many issues which I've resolved in the following manner-
-
There is only one seemingly popular, well maintained & reputed crate for downloading files from the internet, that is
reqwest
. But it does not seem to be having a progress bar for download.So I implemented my own downloader with progress bar logic using
tokio::Command::new("wget").args(["--show-progress", "-q"])
andtokio::BufReader()
on its stderr to read the output of it and display the progress bar withindicatif
-
I could not find any way of running multiple async functions (futures, in my case downloads) in parallel.
So I am relying on ChatGPT code which goes like this-
async fn main() { let stream = futures::stream::iter(0..1000).map(|i| { download().await }); let buffer = stream.buffer_unordered(CONCURRENT_DOWNLOADS_COUNT); while Some(deps) in buffer.next().await { println!("{}", deps); // deps because download function returns a list of dependencies of the package to further download, I have separate logic for managing that } }
It creates 1000 awaited download functions ahead of time & stores them in an array, then pools them concurrently.
-
But the download functions have to the same & for all.
So I have added Arcs, Mutexes & broadcasts (mpmc from tokio) & its working with no errors.
But the problem is in the download function
/* ...wget process created here... */
let reader = BufReader::new(wget.stderr.unwrap());
let mut lines = reader.lines()
while Ok(Some(line)) in lines.next_line().await {
println!("{}", line); // this never executes
/* ...extracting percent complete from
wget progress bar output here... */
}
println!("complete"); // runtime never comes here
As far as i have investigated lines.next_line().await
pauses for ever, it never resolves.
This exact code works outside of stream.iter(0..1000).map(...)
, ex- in a separate file, but when put inside the #2 format, it does not work
I have been trying for days & days together but I have never been able to figure out what exactly is wrong. Please help me fix this issue. I can give my full code if needed. But, please I really need help.