From fe378a6121ce80741e9f55285b991003e6dc880a Mon Sep 17 00:00:00 2001 From: xiaoguang Date: Fri, 29 Dec 2023 17:05:15 +0800 Subject: [PATCH] chore: optimization cargo clippy check problem --- token-core/tcx-common/src/hash.rs | 5 +---- token-core/tcx-constants/src/btc_fork_network.rs | 4 ++-- token-core/tcx-constants/src/coin_info.rs | 2 +- token-core/tcx-filecoin/src/address.rs | 4 ++-- token-core/tcx-keystore/src/keystore/hd.rs | 10 +++++----- token-core/tcx-substrate/src/keystore.rs | 6 +++--- 6 files changed, 14 insertions(+), 17 deletions(-) diff --git a/token-core/tcx-common/src/hash.rs b/token-core/tcx-common/src/hash.rs index 6b647237..4880f109 100644 --- a/token-core/tcx-common/src/hash.rs +++ b/token-core/tcx-common/src/hash.rs @@ -6,10 +6,7 @@ pub type Hash160 = [u8; 20]; pub fn merkle_hash(data: &[u8]) -> Hash256 { assert!(!data.is_empty(), "data should not be empty"); - let mut hashes = data - .chunks(1024) - .map(|chunk| sha256d(chunk)) - .collect::>(); + let mut hashes = data.chunks(1024).map(sha256d).collect::>(); let mut len = hashes.len(); let mut data = [0u8; 64]; diff --git a/token-core/tcx-constants/src/btc_fork_network.rs b/token-core/tcx-constants/src/btc_fork_network.rs index c795975c..9cbddd1c 100644 --- a/token-core/tcx-constants/src/btc_fork_network.rs +++ b/token-core/tcx-constants/src/btc_fork_network.rs @@ -196,7 +196,7 @@ pub fn network_from_param( .filter(|x| x.coin.eq(&chain_type.to_uppercase())) .filter(|x| x.network.eq(&network.to_uppercase())) .filter(|x| x.seg_wit.eq(&seg_wit.to_uppercase())) - .map(|x| x.clone()) + .cloned() .collect::>(); ret.pop() } @@ -206,7 +206,7 @@ pub fn network_form_hrp(hrp: &str) -> Option { let mut ret: Vec = networks .iter() .filter(|x| x.hrp.eq(hrp)) - .map(|x| x.clone()) + .cloned() .collect::>(); ret.pop() } diff --git a/token-core/tcx-constants/src/coin_info.rs b/token-core/tcx-constants/src/coin_info.rs index 38d2261c..549f5ad6 100644 --- a/token-core/tcx-constants/src/coin_info.rs +++ b/token-core/tcx-constants/src/coin_info.rs @@ -250,7 +250,7 @@ pub fn coin_info_from_param( && (x.seg_wit.as_str() == seg_wit || seg_wit.is_empty()) && (x.curve.as_str() == curve || curve.is_empty()) }) - .map(|x| x.clone()) + .cloned() .collect::>(); if coins.is_empty() { diff --git a/token-core/tcx-filecoin/src/address.rs b/token-core/tcx-filecoin/src/address.rs index 5438d631..a82ac352 100644 --- a/token-core/tcx-filecoin/src/address.rs +++ b/token-core/tcx-filecoin/src/address.rs @@ -15,7 +15,7 @@ const TESTNET_PREFIX: &str = "t"; #[derive(Clone, Copy)] pub enum Protocol { Secp256k1 = 1, - BLS = 3, + Bls = 3, } #[derive(PartialEq, Eq, Clone)] @@ -49,7 +49,7 @@ impl Address for FilecoinAddress { checksum = Self::checksum(&[vec![protocol as u8], payload.to_vec()].concat()); } TypedPublicKey::BLS(pk) => { - protocol = Protocol::BLS; + protocol = Protocol::Bls; payload = pk.to_bytes(); checksum = Self::checksum(&[vec![protocol as u8], payload.to_vec()].concat()); diff --git a/token-core/tcx-keystore/src/keystore/hd.rs b/token-core/tcx-keystore/src/keystore/hd.rs index 6783f461..49692d04 100644 --- a/token-core/tcx-keystore/src/keystore/hd.rs +++ b/token-core/tcx-keystore/src/keystore/hd.rs @@ -5,7 +5,7 @@ use uuid::Uuid; use super::{transform_mnemonic_error, Account, Address, Error, Metadata, Result, Store}; -use std::collections::HashMap; +use std::collections::{hash_map::Entry, HashMap}; use tcx_common::ToHex; use tcx_constants::{CoinInfo, CurveType}; @@ -37,12 +37,12 @@ impl Cache { F: FnOnce() -> Result, { let cache_key = Cache::get_cache_key(key, curve); - if self.keys.contains_key(&cache_key) { - Ok(self.keys[&cache_key].clone()) - } else { + if let Entry::Vacant(e) = self.keys.entry(cache_key.clone()) { let k = f()?; - self.keys.insert(cache_key, k.clone()); + e.insert(k.clone()); Ok(k) + } else { + Ok(self.keys[&cache_key].clone()) } } } diff --git a/token-core/tcx-substrate/src/keystore.rs b/token-core/tcx-substrate/src/keystore.rs index d62943d2..684ebef3 100644 --- a/token-core/tcx-substrate/src/keystore.rs +++ b/token-core/tcx-substrate/src/keystore.rs @@ -299,9 +299,9 @@ fn gen_nonce() -> [u8; 24] { let mut rng = rand::thread_rng(); let mut nonce = [0u8; 24]; - for idx in 0..24 { - nonce[idx] = rng.gen::() - } + (0..nonce.len()).for_each(|idx| { + nonce[idx] = rng.gen::(); + }); nonce }