From 047ea9372f1e8897e1618224fa3ff1a0325cece7 Mon Sep 17 00:00:00 2001 From: Quentin Le Sceller Date: Tue, 4 Feb 2020 11:17:46 -0500 Subject: [PATCH] Remove cloning and various formating/efficiency improvements (#321) --- config/src/comments.rs | 10 +++++----- config/src/config.rs | 23 ++++++++++------------- config/src/types.rs | 4 ++-- controller/src/display.rs | 6 +++--- libwallet/src/slate.rs | 2 +- util/src/ov3.rs | 2 +- 6 files changed, 22 insertions(+), 25 deletions(-) diff --git a/config/src/comments.rs b/config/src/comments.rs index cb7756658..9950cf0b4 100644 --- a/config/src/comments.rs +++ b/config/src/comments.rs @@ -236,10 +236,10 @@ fn comments() -> HashMap { } fn get_key(line: &str) -> String { - if line.contains("[") && line.contains("]") { + if line.contains('[') && line.contains(']') { return line.to_owned(); - } else if line.contains("=") { - return line.split("=").collect::>()[0].trim().to_owned(); + } else if line.contains('=') { + return line.split('=').collect::>()[0].trim().to_owned(); } else { return "NOT_FOUND".to_owned(); } @@ -247,7 +247,7 @@ fn get_key(line: &str) -> String { pub fn insert_comments(orig: String) -> String { let comments = comments(); - let lines: Vec<&str> = orig.split("\n").collect(); + let lines: Vec<&str> = orig.split('\n').collect(); let mut out_lines = vec![]; for l in lines { let key = get_key(l); @@ -261,5 +261,5 @@ pub fn insert_comments(orig: String) -> String { for l in out_lines { ret_val.push_str(&l); } - ret_val.to_owned() + ret_val } diff --git a/config/src/config.rs b/config/src/config.rs index 731ba7c57..e0af8176b 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -32,15 +32,15 @@ use crate::types::{TorConfig, WalletConfig}; use crate::util::logger::LoggingConfig; /// Wallet configuration file name -pub const WALLET_CONFIG_FILE_NAME: &'static str = "grin-wallet.toml"; -const WALLET_LOG_FILE_NAME: &'static str = "grin-wallet.log"; -const GRIN_HOME: &'static str = ".grin"; +pub const WALLET_CONFIG_FILE_NAME: &str = "grin-wallet.toml"; +const WALLET_LOG_FILE_NAME: &str = "grin-wallet.log"; +const GRIN_HOME: &str = ".grin"; /// Wallet data directory -pub const GRIN_WALLET_DIR: &'static str = "wallet_data"; +pub const GRIN_WALLET_DIR: &str = "wallet_data"; /// Node API secret -pub const API_SECRET_FILE_NAME: &'static str = ".api_secret"; +pub const API_SECRET_FILE_NAME: &str = ".api_secret"; /// Owner API secret -pub const OWNER_API_SECRET_FILE_NAME: &'static str = ".owner_api_secret"; +pub const OWNER_API_SECRET_FILE_NAME: &str = ".owner_api_secret"; fn get_grin_path( chain_type: &global::ChainTypes, @@ -123,7 +123,7 @@ fn check_api_secret_file( Some(p) => p, None => get_grin_path(chain_type, false)?, }; - let mut api_secret_path = grin_path.clone(); + let mut api_secret_path = grin_path; api_secret_path.push(file_name); if !api_secret_path.exists() { init_api_secret(&api_secret_path) @@ -150,7 +150,7 @@ pub fn initial_setup_wallet( (path, GlobalWalletConfig::new(p.to_str().unwrap())?) } else { // Check if grin dir exists - let grin_path = match data_path.clone() { + let grin_path = match data_path { Some(p) => p, None => get_grin_path(chain_type, create_path)?, }; @@ -265,7 +265,7 @@ impl GlobalWalletConfig { .unwrap() .clone(), ), - String::from(format!("{}", e)), + format!("{}", e), )); } } @@ -311,10 +311,7 @@ impl GlobalWalletConfig { match encoded { Ok(enc) => return Ok(enc), Err(e) => { - return Err(ConfigError::SerializationError(String::from(format!( - "{}", - e - )))); + return Err(ConfigError::SerializationError(format!("{}", e))); } } } diff --git a/config/src/types.rs b/config/src/types.rs index 911a5852c..af601defe 100644 --- a/config/src/types.rs +++ b/config/src/types.rs @@ -93,7 +93,7 @@ impl WalletConfig { /// Use value from config file, defaulting to sensible value if missing. pub fn owner_api_listen_port(&self) -> u16 { self.owner_api_listen_port - .unwrap_or(WalletConfig::default_owner_api_listen_port()) + .unwrap_or_else(WalletConfig::default_owner_api_listen_port) } /// Owner API listen address @@ -166,7 +166,7 @@ impl From for ConfigError { fn from(error: io::Error) -> ConfigError { ConfigError::FileIOError( String::from(""), - String::from(format!("Error loading config file: {}", error)), + format!("Error loading config file: {}", error), ) } } diff --git a/controller/src/display.rs b/controller/src/display.rs index 35f71a1b8..d22fe6146 100644 --- a/controller/src/display.rs +++ b/controller/src/display.rs @@ -451,7 +451,7 @@ pub fn tx_messages(tx: &TxLogEntry, dark_background_color_scheme: bool) -> Resul let msgs = match tx.messages.clone() { None => { - writeln!(t, "{}", "None").unwrap(); + writeln!(t, "None").unwrap(); t.reset().unwrap(); return Ok(()); } @@ -459,7 +459,7 @@ pub fn tx_messages(tx: &TxLogEntry, dark_background_color_scheme: bool) -> Resul }; if msgs.messages.is_empty() { - writeln!(t, "{}", "None").unwrap(); + writeln!(t, "None").unwrap(); t.reset().unwrap(); return Ok(()); } @@ -529,7 +529,7 @@ pub fn payment_proof(tx: &TxLogEntry) -> Result<(), Error> { let pp = match &tx.payment_proof { None => { - writeln!(t, "{}", "None").unwrap(); + writeln!(t, "None").unwrap(); t.reset().unwrap(); return Ok(()); } diff --git a/libwallet/src/slate.rs b/libwallet/src/slate.rs index 1e62457ad..ae49f6c9b 100644 --- a/libwallet/src/slate.rs +++ b/libwallet/src/slate.rs @@ -129,7 +129,7 @@ impl ParticipantMessageData { impl fmt::Display for ParticipantMessageData { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - writeln!(f, "")?; + writeln!(f)?; write!(f, "Participant ID {} ", self.id)?; if self.id == 0 { writeln!(f, "(Sender)")?; diff --git a/util/src/ov3.rs b/util/src/ov3.rs index ee108bcd3..250a49b52 100644 --- a/util/src/ov3.rs +++ b/util/src/ov3.rs @@ -57,7 +57,7 @@ impl OnionV3Address { } }; let d_pub_key: DalekPublicKey = (&d_skey).into(); - Ok(OnionV3Address(d_pub_key.as_bytes().clone())) + Ok(OnionV3Address(*d_pub_key.as_bytes())) } /// return dalek public key