I got some code that grabs an item which is them added to a vec.
The important part isn't that it adds them, but that i can gather all the items up in the end.
Because the item is a Result<Option<..>> i need to check that, as i only want the Ok(Some(..)).
Currently i have two ways i have tried that works.
The first is is slower but reacts to errors faster, the other is faster but doesn't check the states until afterwards.
The ideal would be to get the both of both worlds, but not sure if it's possible.
I have done it in a hacky way with match and filter_map chaining but it ends up slower, not surprising.
Here's basically how the code looks:
// example 1 (gather and check)
let mv = std::sync::Mutex::new(&mut folder.children);
sub_paths
.par_iter()
.map(|f| folder.path.join(f))
.try_for_each(|c| -> Result<()> {
if let Some(f) = get_folder(c, supports_long_path)? {
mv.lock()
.map_err(|e| {
custom_windows_error(
ERROR_LOCK_FAILED,
&format!("mutex lock failed: {}", e),
)
})?
.push(f);
}
Ok(())
})?;
// example 2 (gather in bulk, check in bulk )
for l in sub_paths
.par_iter()
.map(|f| get_folder(folder.path.join(f), supports_long_path))
.collect::<Vec<Result<Option<...>>>>()
{
if let Some(f) = l? {
folder.children.push(f);
}
}
Or perhaps there's some other way to tackle this that i didn't think of as well.
Please give it a look and throw any suggestions at me:)