Skip to content

Commit

Permalink
Use spawn's retval instead of an Arc<Mutex<_>>
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayGarin committed Aug 1, 2020
1 parent af4e0e6 commit 68f87ed
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use anyhow::Result;
use async_std::fs::File;
use async_std::prelude::*;
use async_std::task;
use async_std::sync::{Arc, Mutex};
use freq::{FreqGetter, FreqMap};
use regex::Regex;
use std::path::PathBuf;
Expand All @@ -22,17 +21,15 @@ async fn main() -> Result<()> {
let sym_or_num_re = Regex::new(r"[0-9!@#$%^&*()_+-=]").expect("Bad regex");
let glb = glob("/home/igarin/workspace/**/*.rs").expect("Bad glob");
let mut children = vec![];
let mut maps = Arc::new(Mutex::new(Vec::<(PathBuf, FreqMap)>::new()));

for entry in glb {
match entry {
Ok(path) => {
let getter = FreqGetter::new().filter_regex(sym_or_num_re.clone());
let priv_maps = maps.clone();
let child = task::spawn(async move {
let freq = get_file_freq(path.clone(), getter).await.unwrap();
let mut m = priv_maps.lock().await;
m.push((path, freq));
get_file_freq(path.clone(), getter)
.await
.map(|freq| (path, freq))
});
children.push(child);
}
Expand All @@ -41,13 +38,10 @@ async fn main() -> Result<()> {
}

for child in children {
child.await;
}

for map in maps.lock().await.iter() {
let (path, freq) = child.await?;
println!("{}: (: {}",
map.0.display(),
map.1.get(&'(').unwrap_or(&0));
path.display(),
freq.get(&'(').unwrap_or(&0));
}

Ok(())
Expand Down

0 comments on commit 68f87ed

Please sign in to comment.