From dcdbdd4bcc1baf5a0125ddd4f0b5221bbbd2f42e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= <4142+huitseeker@users.noreply.github.com> Date: Wed, 29 Jan 2020 09:20:57 -0500 Subject: [PATCH] Some simple Option / Result / iterator pattern simplifications (#3205) --- api/src/handlers/chain_api.rs | 5 +---- api/src/types.rs | 8 ++++---- api/src/web.rs | 5 +---- core/src/libtx/proof.rs | 19 +++++++++---------- keychain/src/extkey_bip32.rs | 19 +++++++++---------- pool/src/pool.rs | 6 +----- src/bin/tui/mining.rs | 7 ++++--- store/src/leaf_set.rs | 3 +-- store/src/types.rs | 5 +---- util/src/file.rs | 19 ++++++++----------- 10 files changed, 39 insertions(+), 57 deletions(-) diff --git a/api/src/handlers/chain_api.rs b/api/src/handlers/chain_api.rs index 377c040037..6b291a8a9d 100644 --- a/api/src/handlers/chain_api.rs +++ b/api/src/handlers/chain_api.rs @@ -463,10 +463,7 @@ impl KernelHandler { height, mmr_index, }); - match kernel { - Some(kernel) => Ok(kernel), - None => Err(ErrorKind::NotFound.into()), - } + kernel.ok_or_else(|| ErrorKind::NotFound.into()) } } diff --git a/api/src/types.rs b/api/src/types.rs index 4afdeed81b..7b554253e8 100644 --- a/api/src/types.rs +++ b/api/src/types.rs @@ -334,10 +334,10 @@ impl OutputPrintable { } pub fn range_proof(&self) -> Result { - let proof_str = match self.proof.clone() { - Some(p) => p, - None => return Err(ser::Error::HexError(format!("output range_proof missing"))), - }; + let proof_str = self + .proof + .clone() + .ok_or_else(|| ser::Error::HexError(format!("output range_proof missing")))?; let p_vec = util::from_hex(proof_str) .map_err(|_| ser::Error::HexError(format!("invalid output range_proof")))?; diff --git a/api/src/web.rs b/api/src/web.rs index c6852ac595..a2c2c695b6 100644 --- a/api/src/web.rs +++ b/api/src/web.rs @@ -105,10 +105,7 @@ impl QueryParams { } pub fn get(&self, name: &str) -> Option<&String> { - match self.params.get(name) { - None => None, - Some(v) => v.first(), - } + self.params.get(name).and_then(|v| v.first()) } } diff --git a/core/src/libtx/proof.rs b/core/src/libtx/proof.rs index 43a408942d..f5a451b53c 100644 --- a/core/src/libtx/proof.rs +++ b/core/src/libtx/proof.rs @@ -65,10 +65,7 @@ pub fn verify( extra_data: Option>, ) -> Result<(), secp::Error> { let result = secp.verify_bullet_proof(commit, proof, extra_data); - match result { - Ok(_) => Ok(()), - Err(e) => Err(e), - } + result.map(|_| ()) } /// Rewind a rangeproof to retrieve the amount, derivation path and switch commitment type @@ -228,9 +225,10 @@ where let id = Identifier::from_serialized_path(depth, &msg[4..]); let commit_exp = self.keychain.commit(amount, &id, &switch)?; - match commit == &commit_exp { - true => Ok(Some((id, switch))), - false => Ok(None), + if commit == &commit_exp { + Ok(Some((id, switch))) + } else { + Ok(None) } } } @@ -338,9 +336,10 @@ where let commit_exp = self .keychain .commit(amount, &id, &SwitchCommitmentType::Regular)?; - match commit == &commit_exp { - true => Ok(Some((id, SwitchCommitmentType::Regular))), - false => Ok(None), + if commit == &commit_exp { + Ok(Some((id, SwitchCommitmentType::Regular))) + } else { + Ok(None) } } } diff --git a/keychain/src/extkey_bip32.rs b/keychain/src/extkey_bip32.rs index 245554f696..d17d3bedd7 100644 --- a/keychain/src/extkey_bip32.rs +++ b/keychain/src/extkey_bip32.rs @@ -107,15 +107,17 @@ impl BIP32GrinHasher { impl BIP32Hasher for BIP32GrinHasher { fn network_priv(&self) -> [u8; 4] { - match self.is_floo { - true => [0x03, 0x27, 0x3A, 0x10], // fprv - false => [0x03, 0x3C, 0x04, 0xA4], // gprv + if self.is_floo { + [0x03, 0x27, 0x3A, 0x10] + } else { + [0x03, 0x3C, 0x04, 0xA4] } } fn network_pub(&self) -> [u8; 4] { - match self.is_floo { - true => [0x03, 0x27, 0x3E, 0x4B], // fpub - false => [0x03, 0x3C, 0x08, 0xDF], // gpub + if self.is_floo { + [0x03, 0x27, 0x3E, 0x4B] + } else { + [0x03, 0x3C, 0x08, 0xDF] } } fn master_seed() -> [u8; 12] { @@ -380,10 +382,7 @@ impl ExtendedPrivKey { passphrase: &str, is_floo: bool, ) -> Result { - let seed = match mnemonic::to_seed(mnemonic, passphrase) { - Ok(s) => s, - Err(e) => return Err(Error::MnemonicError(e)), - }; + let seed = mnemonic::to_seed(mnemonic, passphrase).map_err(|e| Error::MnemonicError(e))?; let mut hasher = BIP32GrinHasher::new(is_floo); let key = ExtendedPrivKey::new_master(secp, &mut hasher, &seed)?; Ok(key) diff --git a/pool/src/pool.rs b/pool/src/pool.rs index e2469dcaed..f8991e37e9 100644 --- a/pool/src/pool.rs +++ b/pool/src/pool.rs @@ -403,11 +403,7 @@ impl Pool { // Oldest (based on pool insertion time) will then be prioritized. tx_buckets.sort_unstable_by_key(|x| (Reverse(x.fee_to_weight), x.age_idx)); - tx_buckets - .into_iter() - .map(|x| x.raw_txs) - .flatten() - .collect() + tx_buckets.into_iter().flat_map(|x| x.raw_txs).collect() } pub fn find_matching_transactions(&self, kernels: &[TxKernel]) -> Vec { diff --git a/src/bin/tui/mining.rs b/src/bin/tui/mining.rs index e60b781242..7b3ae89479 100644 --- a/src/bin/tui/mining.rs +++ b/src/bin/tui/mining.rs @@ -130,9 +130,10 @@ impl TableViewItem for DiffBlock { fn to_column(&self, column: DiffColumn) -> String { let naive_datetime = NaiveDateTime::from_timestamp(self.time as i64, 0); let datetime: DateTime = DateTime::from_utc(naive_datetime, Utc); - let pow_type = match self.is_secondary { - true => String::from("Secondary"), - false => String::from("Primary"), + let pow_type = if self.is_secondary { + String::from("Secondary") + } else { + String::from("Primary") }; match column { diff --git a/store/src/leaf_set.rs b/store/src/leaf_set.rs index b6b9500209..0fda0d7be9 100644 --- a/store/src/leaf_set.rs +++ b/store/src/leaf_set.rs @@ -96,8 +96,7 @@ impl LeafSet { /// Only applicable for the output MMR. fn unpruned_pre_cutoff(&self, cutoff_pos: u64, prune_list: &PruneList) -> Bitmap { (1..=cutoff_pos) - .filter(|&x| pmmr::is_leaf(x)) - .filter(|&x| !prune_list.is_pruned(x)) + .filter(|&x| pmmr::is_leaf(x) && !prune_list.is_pruned(x)) .map(|x| x as u32) .collect() } diff --git a/store/src/types.rs b/store/src/types.rs index 286a5601c8..75a7604de6 100644 --- a/store/src/types.rs +++ b/store/src/types.rs @@ -107,10 +107,7 @@ where /// Elements can be of variable size (handled internally in the append-only file impl). /// pub fn read(&self, position: u64) -> Option { - match self.file.read_as_elmt(position - 1) { - Ok(x) => Some(x), - Err(_) => None, - } + self.file.read_as_elmt(position - 1).ok() } /// Rewind the backend file to the specified position. diff --git a/util/src/file.rs b/util/src/file.rs index 744beb9cec..c1457699ef 100644 --- a/util/src/file.rs +++ b/util/src/file.rs @@ -70,15 +70,12 @@ fn copy_to(src: &Path, src_type: &fs::FileType, dst: &Path) -> io::Result { /// Retrieve first line from file pub fn get_first_line(file_path: Option) -> Option { - match file_path { - Some(path) => match fs::File::open(path) { - Ok(file) => { - let buf_reader = io::BufReader::new(file); - let mut lines_iter = buf_reader.lines().map(|l| l.unwrap()); - lines_iter.next() - } - Err(_) => None, - }, - None => None, - } + file_path.and_then(|path| match fs::File::open(path) { + Ok(file) => { + let buf_reader = io::BufReader::new(file); + let mut lines_iter = buf_reader.lines().map(|l| l.unwrap()); + lines_iter.next() + } + Err(_) => None, + }) }