From 634bd00ab55f6025d5b44abfff17cfdcb2d404ec Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Thu, 7 May 2020 07:20:29 -0700 Subject: [PATCH] Refactor fold to map Summary: This is to make parallelising this in the next step easier. Reviewed By: jknoxville Differential Revision: D21405192 fbshipit-source-id: b1f2841526a65fcbf545feb69f258874c2134603 --- packer/src/main.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/packer/src/main.rs b/packer/src/main.rs index 931dded15..ab69e2b37 100644 --- a/packer/src/main.rs +++ b/packer/src/main.rs @@ -42,8 +42,8 @@ fn pack( .get(platform) .ok_or_else(|| error::Error::MissingPlatformDefinition(platform.clone()))?; packtype_paths - .iter() - .try_fold(vec![], |mut acc, (pack_type, pack_files)| { + .into_iter() + .map(|(pack_type, pack_files)| { print!( "Packing for platform {:?} type {:?} ... ", platform, pack_type @@ -63,9 +63,9 @@ fn pack( tar.finish()?; println!("done."); - acc.push((*pack_type, output_path)); - Ok(acc) + Ok((*pack_type, output_path)) }) + .collect() } fn pack_platform( @@ -192,14 +192,23 @@ fn gen_manifest(archive_paths: &[(PackType, path::PathBuf)]) -> Result Result> { - archive_paths.into_iter().try_fold( - BTreeMap::new(), - |mut acc: BTreeMap, (pack_type, path)| { + let res = archive_paths + .into_iter() + .map(|(pack_type, path)| { let reader = BufReader::new(File::open(path)?); - acc.insert(*pack_type, sha256_digest(reader)?); - Ok(acc) - }, - ) + let hash = sha256_digest(reader)?; + Ok((*pack_type, hash)) + }) + .collect::>>()? + .into_iter() + .fold( + BTreeMap::new(), + |mut acc: BTreeMap, (pack_type, hash_sum)| { + acc.insert(pack_type, hash_sum); + acc + }, + ); + Ok(res) } #[cfg(test)]